Click here to Skip to main content
11,924,573 members (62,238 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
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:

. . . . X
. . . X X X
. . X X X X X
. X X X X X X X

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.!!!
Posted 17-Feb-13 23:35pm
Edited 18-Feb-13 22: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 . !!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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 "*";
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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? Wink | ;) )
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:

* *
* * *
* * * *

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151125.3 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100