| | |
Printing a diamond
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 10
Reputation:
Solved Threads: 0
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...!
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)
#include <iostream.h> #include <conio.h> void main() { int i, j, k; for(i=1; i<=5; i++) { for(j=1; j<=5-i; j++) cout << " "; for(k=1; k<=2*i-1; k++) cout << "*"; cout << endl ; } int l,m,n; for (n = 4; n > 0;n--) { for (l = 1; l <= 5- n; l++) cout<<" "; for (m = 1; m<= 2 * n- 1; m++) cout<<"*"; cout<<endl; } getch(); }
Here is one of my beginning projects last year :
C++ Syntax (Toggle Plain Text)
const int row = 1; for(int i = 0; i < row; i++) { for(int j = row-i; j ; j--) cout<<" "; for(int k = i*2; k >= 0; k--) cout<<"*"; cout<<endl; } for(int i = row; i >= 0; i--) { for(int j = i; j < row; j++) cout<<" "; for(int k = i*2; k >= 0; k--) cout<<"*"; cout<<endl; }
Last edited by firstPerson; Aug 17th, 2009 at 11:49 pm.
Go ahead and try to solve this euler problem, if u can. Check with me for hints and answers. Good Luck Solved by:tux4life, "your name here"
•
•
Join Date: Jul 2009
Posts: 34
Reputation:
Solved Threads: 2
This might help you
Didnt changed much so you can understand it
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> using namespace std; int main()//you should use int main not void main { int i, j, k, rows; cout<<"Specify the number of rows (between 1-19) in the dimond: "; cin>>rows; cout<<endl; for(i=1; i<=rows; i++) { for(j=1; j<=rows-i; j++) cout << " "; for(k=1; k<=2*i-1; k++) cout << "*"; cout << endl; } int l,m,n; for (n = rows-1; n > 0;n--) { for (l = 1; l <= rows- n; l++) cout<<" "; for (m = 1; m<= 2 * n- 1; m++) cout<<"*"; cout<<endl; } getch(); return 0;//since i used int main i returned a value }
Didnt changed much so you can understand it
Do {me} While (you can)
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:
This could be something like/
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
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. .
For Eg:
C++ Syntax (Toggle Plain Text)
for(i=1; i<=5; i++)
C++ Syntax (Toggle Plain Text)
int len=5; 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 2:19 am.
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.
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.
![]() |
Similar Threads
- urgent need for source code of online flight reservation system coded in JSP (JSP)
- Diamond asterisk (C++)
- All I need is a simple yes or no answer (C++)
- Windows 2000 Adv Server and "Printing Subsystem" (Windows NT / 2000 / XP)
- WinXP, RH9, Samba, and Printing (*nix Software)
- Printing pictures (Windows NT / 2000 / XP)
- Linux printing questions (*nix Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Int assigning problem
- Next Thread: "ReadFile" problem through COM1 port
Views: 748 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm api array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream form forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int integer lazy linker list loop math matrix member memory network newbie news number object objects opengl operator output parameter pointer pointers problem program programming project qt random read recursion recursive reference return search server sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets






