Tuesday, July 14, 2009

..........C++ code?

How would I get it to do display the following patterns using the FOR statement?








*********


********


*******


******


so on…





*********


********


*******


******


so on…





Such as:


*


**


***


****


*****


so on…





I have tried using the WHILE for several hours. Unknowing I had to use the FOR all along.





#include %26lt;iostream%26gt;





using std::cout;


using std::cin;


using std::endl;





[code]


int main()


{


//declare variables


const char asterk = '*';


int a = 9;


int b = 0;





while (a %26gt; 0)


{


b = a;


while (b %26gt; 0)


{


cout %26lt;%26lt; asterk;


b = b - 1;


}


a = a - 1;


cout %26lt;%26lt; endl;


}


//end while





return 0;


} //end of main function

..........C++ code?
//Decreasing


int main()


{


//declare variables


const char asterk = '*';


int a = 0;


int b = 0;





For(a = 9;a %26gt; 0 ; a--)


{


For(b=a;b %26gt; 0 ; b--)


{


cout %26lt;%26lt; asterk;


}


cout %26lt;%26lt; endl;


}


//end while





return 0;


} //end of main function





//Increasing


int main()


{


//declare variables


const char asterk = '*';


int a = 0;


int b = 0;





for(a = 1; a %26lt; 9;a++)


{


for(b = a; b %26gt; 0 ;b--)


{


cout %26lt;%26lt; asterk;


}


cout %26lt;%26lt; endl;


}


//end while





return 0;


} //end of main function
Reply:assume the max possible number of * on a line is n.





1) descreasing:





for(int i=n; i%26gt;0; i--)


for(int j=0; j%26lt;i; j++)


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


cout%26lt;%26lt;endln;





2) increasing:





for (int i=0; i%26lt;n; i++)


for (int j=0; j%26lt;i; j++)


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


cout%26lt;%26lt;endln;


No comments:

Post a Comment