12,749,392 members (34,950 online)
Rate this:
See more:
I am facing problem in building logic ..
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
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 ..
Posted 17-Feb-13 23:35pm
ctype.h1.5K
Updated 18-Feb-13 22:37pm
v9
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 solution.so 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:

## 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

OK .. UPDATED ..
NOW HOW CAN I MANAGE ASTERISKS .. !!!!!!!
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:

## 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? ;))
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 ..
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++)

Top Experts
Last 24hrsThis month
 Graeme_Grant 354 Karthik Bangalore 325 F-ES Sitecore 215 OriginalGriff 175 Afzaal Ahmad Zeeshan 115
 OriginalGriff 3,833 Peter Leow 2,861 ppolymorphe 2,028 Karthik Bangalore 2,025 Richard MacCutchan 1,409