Feedback on idea welcome

NerdyGurl

Member
Joined
Apr 19, 2007
Messages
7
Location
Auckland, NZ
Programming Experience
Beginner
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?

All feedback, ideas are welcome
Cheers,
Jessica
 
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:
Expand Collapse Copy
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
 

Attachments

Hi thanks :)

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.
Just one thing puzzles me...

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:

Console.Out.Write...
Console.Out.Write...
Console.Out.Write...
Console.Out.Write...
Console.Out.Write...

In your code?

Hmm

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?

All feedback, ideas are welcome
Cheers,
Jessica
When I had to do this at uni I used 2 loops because we had to. I could have done it with one

Here is the theory:

VB.NET:
Expand Collapse Copy
[FONT=Courier New] *[/FONT]
[FONT=Courier New]***[/FONT]
[FONT=Courier New]*****[/FONT]
[FONT=Courier New]***[/FONT]
[FONT=Courier New] *[/FONT]

This is made up with:

VB.NET:
Expand Collapse Copy
[FONT=Courier New] *      3 spaces, 1 star[/FONT]
[FONT=Courier New]***     2 spaces, 3 stars[/FONT]
[FONT=Courier New]*****    1 space , 5 stars[/FONT]
[FONT=Courier New][FONT=Courier New]***     2 spaces, 3 stars[/FONT]
[/FONT][FONT=Courier New][FONT=Courier New] *      3 spaces, 1 star[/FONT]
[/FONT]

You can see the maths:

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:
Expand Collapse Copy
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
 
Back
Top