Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am facing problem in building logic ..
Please Help Tomorrow Is My Paper, I've to complete this A S A P.
There are more problems to be completed before the exam ...

I want to draw pyramid of X's..
for instance user enter 9 as an input the output should be:

<br />
. . . . X<br />
. . . X X X<br />
. . X X X X X<br />
. X X X X X X X<br />
X X X X X X X X X<br />

Here Is My Code But It Skips The Last Line Where It Should Print 9 X's .. :/ ?? :/

void main ()
	 int length;
	 cout << " STARS?: ";
	 cin >> length;
	 int temp = length/2;
	 for (int i = 0; i < temp; i++)
		 for (int j = temp - i; j > 0; j--)
			 cout << ". ";
		 for (int k = 0; k < 2*i+1; k++)
			 cout << "* ";
		 cout << '\n';

I don't want complete code ..
your suggestions will be appreciated.!!!
Updated 18-Feb-13 21:37pm
E.F. Nijboer 18-Feb-13 5:12am    
Why did you declare count?
Usman Hunjra 18-Feb-13 5:17am    
for no specific reason ..
Abhishek Pant 18-Feb-13 12:15pm    
I can't believe that you cant ask a question properly at once. :)Do not post your comments as a either delete your solution[3] or update it with an appropriate solution using 'improve solution'
Usman Hunjra 18-Feb-13 12:20pm    
Got It ..
Question Updated Sir . !!

1. In your outer loop, you increment i instead of row.
2. space and stars are undeclared and uninitialised.
3. The inner while loop is either never run, or never stops (once entered, there's no way to change the loop condition).

Fix those problems, so your code compiles and runs and does something, then step through it to verify your logic.
Share this answer
Usman Hunjra 18-Feb-13 5:25am    
Orjan Westin 18-Feb-13 6:01am    
//Calculate number of spaces to print out.
int space = ...
while (col++ < spaces)
cout " ";
// Calculate number of stars to print
int stars = ...
while (col++ < (spaces+stars))
cout "*";
cout "\n";

cout "*";
Based on your updated code (v4), change the following:

1. outer for loop head line change i++ to ++row
2. inner for loop head line change col < length to col < row
3. remove the while loop (I' m not even sure what you needed it for)
4. instead of cout << ". "; write cout << "* "; (you did want asterisks, no? ;))
5. Move the line "cout << '\n';" outside and behind the inner for loop

you didn't specify what form that pyramid shoud be, but with the changes above you should get sth like that:

*<br />
* *<br />
* * *<br />
* * * *<br />

Note that steps 2 and 3 change your program logic significantly, and I don't know if the results match your intention. But then you didn't state what the spaces are good for, or why you think you need a third nested loop when you are effectively addressing characters in a two-dimensional matrix. I don't see where you get that third dimension from.
Share this answer
Usman Hunjra 18-Feb-13 11:09am    
I think its' a good practice for controlling loops.. :)
Usman Hunjra 18-Feb-13 11:10am    
More over I've Updated My Question ..
Thanks For Your Consideration ..
Stefan_Lang 19-Feb-13 3:42am    
Looking at your current version (9), all you need to change is have your outer loop run one more time: if the user enters 9, temp will be assigned to 4, but since you want 5 lines of output you need that loop to run from 0 to (and including) 4.

Canging the head line of that loop to the following should help:

for (int i=0; i<= temp; i++)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900