Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework
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
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
ctype.h1.7K
Edited 18-Feb-13 22:37pm
v9
Comments
E.F. Nijboer at 18-Feb-13 5:12am
   
Why did you declare count?
Usman Hunjra at 18-Feb-13 5:17am
   
for no specific reason ..
Abhishek Pant at 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 at 18-Feb-13 12:20pm
   
Got It ..
Question Updated Sir . !!
Rate this: bad
good
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.
  Permalink  
Comments
Usman Hunjra at 18-Feb-13 5:25am
   
OK .. UPDATED ..
NOW HOW CAN I MANAGE ASTERISKS .. !!!!!!!
Orjan Westin at 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
good
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.
  Permalink  
Comments
Usman Hunjra at 18-Feb-13 11:09am
   
I think its' a good practice for controlling loops.. :)
Usman Hunjra at 18-Feb-13 11:10am
   
More over I've Updated My Question ..
Thanks For Your Consideration ..
Stefan_Lang at 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
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
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