| | |
Printing a diamond
Please support our C++ advertiser: Intel Parallel Studio Home
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 18th, 2009 at 12:49 am.
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?•
•
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 3: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
- Diamond asterisk (C++)
- All I need is a simple yes or no answer (C++)
- urgent need for source code of online flight reservation system coded in JSP (JSP)
- 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
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






