455,514 Members | 1,728 Online
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

# DIAMOND SHAPE

 P: n/a I need to write a program which takes a number as an input and prints a diamond of # and \$. The number of rows in the shape is equal to the number entered by the user. My program should display the shape for both even and odd value of size. For example if user enters number 7 the program should print following shape. # #\$# #\$#\$# #\$#\$#\$# #\$#\$# #\$# # If user enters a value 6, the program prints diamond in following shape. # #\$# #\$#\$# #\$#\$# #\$# # I have tried very hard and this is the best i could come up with #include using namespace std; int main() { int rows=0; cout<<"Enter the number of rows of the diamond"<>rows; int space=0; int space1=0; int space2=0; int space3=0; int space4=1; int a=0; int b=0; space1=rows/2; space2=space1; space3=space1; a=rows-2; space=space1+a; int count=0; int count1=0; int count2=0; int count3=0; int count4=0; int count5=0; int count6=0; int char1=1; int char2=0; b=a-1; char2=space1+b; while(count1
11 Replies

 P: n/a coinjo wrote: I need to write a program which takes a number as an input and prints a diamond of # and \$. The number of rows in the shape is equal to the number entered by the user. My program should display the shape for both even and odd value of size. For example if user enters number 7 the program should print following shape. [snipped ASCII art] I have tried very hard and this is the best i could come up with #include using namespace std; int main() { int rows=0; cout<<"Enter the number of rows of the diamond"<>rows; int space=0; int space1=0; int space2=0; int space3=0; int space4=1; int a=0; int b=0; [snipped rest of code] You should use less variables and think about your look conditions a little more. If you can, pick up a copy of "Accelerated C++" by A. Koenig and B.E.Moo in a local bookstore. One of the first chapters attacks a problem similars to yours. It won't give you an exact solution for your problem, but it should push you onto the right track. Cheers, Andre Nov 10 '05 #2

 P: n/a in*****@gmail.com wrote: You should use less variables and think about your look conditions a little more. Of course I meant "loop conditions". The program can be done with about 2 for loops, that make no assumptions on the height of the diamond shape. Tip: Math is your friend ;) Cheers, Andre Nov 10 '05 #3

 P: n/a #include using namespace std; int main() { int rows=0; cout<<"Enter the number of rows of the diamond"<>rows; int space=0; int space1=0; int space2=0; int space3=0; int space4=1; int temp=0; space1=rows/2; space2=space1; space3=space1; int count=-1; int count1=0; int count2=0; int count3=0; int count4=0; int count5=0; int count6=0; int char1=1; int char2=0; while(rows>=2) { temp=rows/2; count=count+1; rows=temp; } char2=space1+count; while(count1<=space1) { count2=0; count3=0; while(count2

 P: n/a #include using namespace std; int main() { int rows=0; cout<<"Enter the number of rows of the diamond"<>rows; int space=0; int space1=0; int space2=0; int space3=0; int space4=1; int temp=3; space1=rows/2; space2=space1; space3=space1; int count=0; int count1=0; int count2=0; int count3=0; int count4=0; int count5=0; int count6=0; int char1=1; int char2=0; if(rows>3) { while(temp!=rows) { temp=temp+2; count=count+1; } } char2=space1+count; while(count1<=space1) { count2=0; count3=0; while(count2

 P: n/a coinjo wrote: I have FINALLY come up with this code and i think it is OK for odd values. Is it correct? Dude, your code is EVIL... Take a look at Neil's code and compare. The simpler your code, the better. Programming is not only about getting the program to do what you want, but also to get it to do it in a sensible and preferably simple way. Try structuring the problem at hand, that is the first step in mastering an algorithm. Arne -- [--- PGP key FD05BED7 --- http://www.root42.de/ ---] Nov 10 '05 #7

 P: n/a On 10 Nov 2005 11:46:41 -0800, "coinjo" wrote: #includeusing namespace std;int main(){int rows=0;cout<<"Enter the number of rows of the diamond"<>rows;int space=0;int space1=0;int space2=0;int space3=0;int space4=1;int temp=3; [snip] Didn't your teacher tell you anything about indenting? -- Bob Hairgrove No**********@Home.com Nov 10 '05 #8

 P: n/a coinjo wrote: I need to write a program which takes a number as an input and prints a diamond of # and \$. The number of rows in the shape is equal to the number entered by the user. My program should display the shape for both even and odd value of size. For example if user enters number 7 the program should print following shape. # #\$# #\$#\$# #\$#\$#\$# #\$#\$# #\$# # If user enters a value 6, the program prints diamond in following shape. # #\$# #\$#\$# #\$#\$# #\$# # I have tried very hard and this is the best i could come up with Your code is dreadful (and some of the other solutions proposed on this thread aren't great either). I hope you can see that if someone wants to, say, print a diamond of size 20, then your program will fail hopelessly. You will save time by thinking of a correct algorithm before you even sit down at your computer. For example: - for row number N, I need _____ spaces and _____ hashes A little bit of thought will reveal that the number of spaces needed is (N - 1) / 2, discarding any remainder, and the number of hashes is N (for the first half), and TOTAL + 1 - N (for the second half). I recommend you use a function for actually drawing the line: void draw_line( int num_spaces, int num_hashes ) { // I'll leave you to fill in this bit } Then the main loop of your code is very simple: int n; int halfway = (MAX + 1) / 2; for (n = 1; n <= halfway; ++n) draw_line( (n - 1) / 2, n ); for (; n <= MAX; ++n) draw_line( (n - 1) / 2, MAX + 1 - n ); Comprende? Nov 10 '05 #9

 P: n/a Old Wolf wrote: I recommend you use a function for actually drawing the line: void draw_line( int num_spaces, int num_hashes ) { // I'll leave you to fill in this bit } Then the main loop of your code is very simple: int n; int halfway = (MAX + 1) / 2; for (n = 1; n <= halfway; ++n) draw_line( (n - 1) / 2, n ); for (; n <= MAX; ++n) draw_line( (n - 1) / 2, MAX + 1 - n ); Comprende? I agree with your overall sentiment, but I don't like the fact that you break out the loop into two parts. It's easy enough to make this one look without using if, or anything else. The end result can be a very clean two piece loop: for ( row=0... ) for ( col=0... ) Cheers, Andre Nov 10 '05 #10

 P: n/a On 2005-11-10, Old Wolf wrote: coinjo wrote: I need to write a program which takes a number as an input and prints a diamond of # and \$. The number of rows in the shape is equal to the number entered by the user. My program should display the shape for both even and odd value of size. For example if user enters number 7 the program should print following shape. # #\$# #\$#\$# #\$#\$#\$# #\$#\$# #\$# # If user enters a value 6, the program prints diamond in following shape. # #\$# #\$#\$# #\$#\$# #\$# # I have tried very hard and this is the best i could come up with Your code is dreadful (and some of the other solutions proposed on this thread aren't great either). I hope you can see that if someone wants to, say, print a diamond of size 20, then your program will fail hopelessly. You will save time by thinking of a correct algorithm before you even sit down at your computer. For example: - for row number N, I need _____ spaces and _____ hashes A little bit of thought will reveal that the number of spaces needed is (N - 1) / 2, discarding any remainder, and the number of hashes is N (for the first half), and TOTAL + 1 - N (for the second half). A good algorithm can be quite hard to think of. ;) How high is your diamond? 5 # #\$ #\$# #\$ # -- Neil Cerutti Nov 10 '05 #11

 P: n/a Neil Cerutti wrote: Old Wolf wrote: A little bit of thought will reveal that the number of spaces needed is (N - 1) / 2, discarding any remainder, and the number of hashes is N (for the first half), and TOTAL + 1 - N (for the second half). A good algorithm can be quite hard to think of. ;) Ouch, bad brain fade. Let's go for (MAX+1)/2 - num_hashes. This problem is also apt for recursion: void draw_diamond(int n_spaces, int n_hashes, int odd) { draw_line(n_spaces, n_hashes); if (n_spaces > 0) draw_diamond(n_spaces - 1, n_hashes + 1, odd); if (n_spaces > 0 || !odd) draw_line(n_spaces, n_hashes); } //... draw_diamond( (MAX-1)/2, 1, MAX % 2 ); Nov 11 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.