Hi. I posted some code with descriptions to help you out. I think it might help if you write it out on paper.
This is my method below:
1) we needed a loop to print out # then X.
2)we know that every row needs to print out #'s equal to the row number it is in.
( Row one we need #. Row two we need ##.)
3)We only need one X behind theese #'s.
At this point I type in code to print out #### based on a row value of 5
in my loop (i<5).
I know that I need to put X outside the loop or else it will print #X#X. But I still need a loop for X.
4)I focus on X and create a loop that runs based on the number of rows I want.
5)Within this loop I make another loop to print #'s before each X.
I add a variable(int ha;) as a counter that will determine the number of #'s.
The variable ha will exist outside both loops.
6)See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
using namespace std;
int getRows();
int main()
{
int rows=5;
// rows = getRows();
int ha=0;//#
//Does loop for the number of rows
// contains ha which increases by 1 every loop
for(int i=0; i<Rows; ++i)
{
//prints # based on value of ha, remember the first run ha=0,
//so we will print out #, 0+1 times.
for(int z=0;z<(ha+1);++z)
{cout<<"#";}
//Prints the X before moving onto next iteration
cout<<"x"<<endl;
//HERE! we increase ha+1 so that on the next iteration of this loop,
// the INNer loop that prints #, will print out 2 #'s, Ha, which will be 1 on the
//next loop+ the 1 we get from z<(ha+1).
ha=ha+1;
}
}
/*int getRows()
{
cout<< "How many Rows? \n";
int R;
cin>> R;
return R;
}*/
|
From here you can do the reverse hopefully.
I commented out the input function and set rows to 5 so that you can focus on the loops