Tuesday, July 28, 2009

I need help in C++! (For Loop nexting/incrementing)?

I'm supposed to make this:





*


**


***


**** %26lt;---With this line being the input number by the user.


***


**


*





How do I do this using for loops in C++?





something like...





int main()


{


int n;


int k;





cout%26lt;%26lt;"Enter n:";


cin%26gt;%26gt;n;





for(k=1; k%26lt;=n; k++) %26lt;---Is this right


{


for(what goes here


{


for(what goes here


{


cout%26lt;%26lt;"*"%26lt;%26lt;" "; \%26lt;---Is this right?


}


}


}


cout%26lt;%26lt;endl;


return 0;


}


Am I missing anything else?

I need help in C++! (For Loop nexting/incrementing)?
#include "stdafx.h"


#include %26lt;iostream%26gt;





using namespace std;





int main()


{





int n,k,l,m;


cout%26lt;%26lt;"Enter n:";


cin%26gt;%26gt;n;





for(k=1; k%26lt;=n; k++) // You were right


{


for(l = 1; l %26lt;= k; l++)


{


cout%26lt;%26lt;"*"%26lt;%26lt;" "; // the extra %26lt;%26lt;" " puts a space, for the line to look as you have it in the example, use cout %26lt;%26lt;"*";


}


cout %26lt;%26lt; endl;


} //End increment


for(k=1; k %26lt;= n; k++) // You were right


{


for(l = 1; l %26lt;= k; l++)


{


cout%26lt;%26lt;" "%26lt;%26lt;" "; // the extra %26lt;%26lt;" " puts a space, for the line to look as you have it in the example, use cout %26lt;%26lt;"*";


}


for(m = 1; m %26lt;= (n-k); m++)


{


cout%26lt;%26lt;"*"%26lt;%26lt;" ";


}





cout %26lt;%26lt; endl;


} //End decrement


cin %26gt;%26gt; n; // Remove, this is just to see the pattern at the end


return 0;


} // End main





Of course, there are other ways to skin a cat
Reply:check out http://www.pscode.com for great sample codes.
Reply:int main()


{


int n,k,i;


cout%26lt;%26lt;"Enter n:";


cin%26gt;%26gt;n;


for(k=1; k%26lt;=n; k++) //%26lt;---Is this right


{for(i=1;i++%26lt;=k;) //what goes here


{cout%26lt;%26lt;"* "; // \%26lt;---Is this right?


}


cout%26lt;%26lt;endl;


}


for(k=n; --k) //%26lt;---Is this right


{for(i=k;i++%26lt;=n) //what goes here


{cout%26lt;%26lt;" "; // \%26lt;---Is this right?


}


{for(i=k;i++%26lt;n;) //what goes here


{cout%26lt;%26lt;"* "; // \%26lt;---Is this right?


}


cout%26lt;%26lt;endl;


}


return 0;


} // I dunno, it has been a long time.


// Yahoo suppresses the indentation.

gardenia

No comments:

Post a Comment