943,880 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1089
  • C++ RSS
Aug 18th, 2009
0

Printing a diamond

Expand Post »
Hello!
I have made a program that prints a diamond shape. But my program should read an odd number in the range 1 to 19 to specify the number of rows in the diamond, then it should display
a diamond of the appropriate side in which it is at present. I am a beginner. I don't know how to do that. Can any one help? I am in a need of it...!
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. void main()
  5. {
  6. int i, j, k;
  7. for(i=1; i<=5; i++)
  8. {
  9. for(j=1; j<=5-i; j++)
  10. cout << " ";
  11. for(k=1; k<=2*i-1; k++)
  12. cout << "*";
  13. cout << endl ;
  14. }
  15. int l,m,n;
  16. for (n = 4; n > 0;n--)
  17. {
  18. for (l = 1; l <= 5- n; l++)
  19. cout<<" ";
  20. for (m = 1; m<= 2 * n- 1; m++)
  21. cout<<"*";
  22. cout<<endl;
  23. }
  24. getch();
  25. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the great is offline Offline
10 posts
since Jul 2009
Aug 18th, 2009
0

Re: Printing a diamond

Here is one of my beginning projects last year :
C++ Syntax (Toggle Plain Text)
  1. const int row = 1;
  2.  
  3. for(int i = 0; i < row; i++)
  4. {
  5. for(int j = row-i; j ; j--)
  6. cout<<" ";
  7.  
  8. for(int k = i*2; k >= 0; k--)
  9. cout<<"*";
  10.  
  11. cout<<endl;
  12. }
  13.  
  14. for(int i = row; i >= 0; i--)
  15. {
  16. for(int j = i; j < row; j++)
  17. cout<<" ";
  18.  
  19. for(int k = i*2; k >= 0; k--)
  20. cout<<"*";
  21.  
  22. cout<<endl;
  23. }
Last edited by firstPerson; Aug 18th, 2009 at 12:49 am.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 18th, 2009
0

Re: Printing a diamond

This might help you
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()//you should use int main not void main
  8. {
  9. int i, j, k, rows;
  10. cout<<"Specify the number of rows (between 1-19) in the dimond: ";
  11. cin>>rows;
  12. cout<<endl;
  13. for(i=1; i<=rows; i++)
  14. {
  15. for(j=1; j<=rows-i; j++)
  16. cout << " ";
  17. for(k=1; k<=2*i-1; k++)
  18. cout << "*";
  19. cout << endl;
  20. }
  21. int l,m,n;
  22. for (n = rows-1; n > 0;n--)
  23. {
  24. for (l = 1; l <= rows- n; l++)
  25. cout<<" ";
  26. for (m = 1; m<= 2 * n- 1; m++)
  27. cout<<"*";
  28. cout<<endl;
  29. }
  30. getch();
  31. return 0;//since i used int main i returned a value
  32. }

Didnt changed much so you can understand it
Reputation Points: 10
Solved Threads: 2
Light Poster
poncho4all is offline Offline
34 posts
since Jul 2009
Aug 18th, 2009
1

Re: Printing a diamond

Well, Its quite simple, Use the same formula that you have in the existing code, But just change all the instances of '5' with a variable.

For Eg:

C++ Syntax (Toggle Plain Text)
  1. for(i=1; i<=5; i++)
This could be something like/
C++ Syntax (Toggle Plain Text)
  1. int len=5;
  2. for (i=1;i<=len;i++)

Now you will then need to further need to take in the value of len from the user,.

Check if it lies between 1 - 19 and then look if it is odd. with the % operator. .

Then if all those conditions are satisfied. continue with the for loops or display an message stating that you have entered a wrong value or . Put a default value for len and continue.

poncho4all Your effort of posting down the code is much appreciated, However This forum tends to provide paths to the solution, But not to the solution itself. Read the rule Here. .
Last edited by Sky Diploma; Aug 18th, 2009 at 3:19 am.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 19th, 2009
0

Re: Printing a diamond

I love the idea but can you please explain this in c for me.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
osei-akoto is offline Offline
12 posts
since Jul 2009
Aug 19th, 2009
0

Re: Printing a diamond

its good that you liked the idea but tell me what do you want to understand?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
the great is offline Offline
10 posts
since Jul 2009
Aug 20th, 2009
0

Re: Printing a diamond

Please i want to know how this can be solved in c.
So i can also play around it.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
osei-akoto is offline Offline
12 posts
since Jul 2009
Aug 20th, 2009
0

Re: Printing a diamond

Well,

To get this program running in C, there are only a few minor editions that have to be done.

Firstly,

<iostream> header must be replaced with the standard 'C' Header file 'stdio.h'

Next consider all cout<< statements as printf statements, Note that cout <<endl is equal to printf("\n");


And cin>> refers to scanf()..

Now since you have a brief Idea. Try experimenting and get to the solution.
And maybe from the next-time, Start a new thread instead of Continuing this one.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Int assigning problem
Next Thread in C++ Forum Timeline: "ReadFile" problem through COM1 port





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC