In my course i recieved a question stating : Write a program that prints the following diamond shape, you may use output statements that print a single *, a single space or a single carrige return. Maximise your use of For...Next statements and repition statements while minimising the number of output statements.
That was the question and i was confused for the last few days as to how i was going to go about the program but today i came up with this idea i just want to know how practical it is and if there is a better solution?
My Idea: Was to create a program that had 1 counter controlled If...Next statements one counting up and through each time the condition is true 1 is added to the counter and a * is added to the attribute diamondShape then a console.writeline statement to prepare and present the diamond?
This Console Application code satisfies the requirement, the diamond shape is attached as file 'star.txt', it contains only */space/linebreak characters and only one single of these chars is outputted at a time in the For-Next loop below.
VB.NET:
Sub Main()
Dim starchars As String = My.Computer.FileSystem.ReadAllText("star.txt")
For Each c As Char In starchars
Console.Write(c)
Next
Console.ReadKey()
End Sub
Hi there thanks, i was on the right track after all i was paniking for nothing lol i had a txt file with my diamond but i couldnt get it to show up that peice of code helped me realise how to get it to find it, thanks heaps
have a good day
cheers,
Jessica
In my course i recieved a question stating : Write a program that prints the following diamond shape, you may use output statements that print a single *, a single space or a single carrige return. Maximise your use of For...Next statements and repition statements while minimising the number of output statements.
WHen they say "minimise use of output statements.. do they mean they dont want you to call output often(e.g. putting it in a loop designed to iterate 100 times will call output 100 times) or do they mean they dont want to see:
Either way, you can solve this requirement by using a stringbuilder and Append / AppendLine rather than Out.Write.. then, at the last moment, you can write the whole thing once:
Console.Out.WriteLine(myStringBuilder.ToString())
That should solve the requirement whichever way it is to be taken!
My Idea: Was to create a program that had 1 counter controlled If...Next statements one counting up and through each time the condition is true 1 is added to the counter and a * is added to the attribute diamondShape then a console.writeline statement to prepare and present the diamond?
If the diamond is to be 5 wide at its widest point, we start with 3 spaces (5 - 2 = 3 ok, thats a good rule), ans the number of spaces is going to decrease by one each line. Also we always start with 1 star and that increases by 2 each time
So I wrote a loop that printed the first half of the diamond (it asked the user how many stars width at the widest point. The number had to be odd) increasing the number of stars by 2 and decreasing the spaces by one each time. The termination point for the loop was that it should do the iteration where the number of stars printed = the number of stars at the widest point the user had asked for
something like:
VB.NET:
For starsThisTime = 1 To starsUserAskedFor
...code to print goes here
Next
And then I wrote another loop to do the bottom half
Other handy tips:
This code will make a string of length 100, all filled with letter A:
New String("A"c, 100)
So if you had e.g. 5 stars to print, this is a string of 5 stars:
New String("*"c, 5)
Remember to design your program with a pencil first.. Dont try to write code straight away
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.