Help Please!!!!

I am in C++ introductory class and I am struggling writing 2 different programs, help an either would be very nice!

The first one is a diagonal X such as the following

x
#x
##x
###x
####x

this would be an input of 5, I am able to do this but how would I reverse it and make it look like this

####x
###x
##x
#x
x

this is my setup for the first set of x's

----------------------------------------------------------------------

int main()
{

int numRows(0);

cout << "Enter number of table rows: ";
cin >> numRows;

int row = 1;
while (row <= numRows)
{
int col = 1;
while (col <= row - 1)
{
cout << "# ";
col++;
}
cout << "x" << endl;
row++;
}
}

----------------------------------------------------------------------------

my second problem I hardly even know how to get started on it, I have to write a code for one of these formations

https://gyazo.com/42789ad8765015f62b722f1d88fd07af

honestly my attempts are so bad if someone could just help me with this that would be great!! Thank you for your time :)
Last edited on
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
Last edited on
Here's what you might want to do for your first program:
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
#include <iostream>

using namespace std; 

int main()
{
	int numRows,
		row,
		column;
	
	cout << "\n Enter number of table rows: ";
	cin >> numRows;
	
	int backwardsNumRows = numRows;

	for (row = 1; row <= numRows; row++)
	{
		for (column = 1; column <= (row -1); column ++)
		{
			cout << "#";
		}
	
	cout << "X" << "\n";
	}
	
	cout << "\n";
	
	for (row = 1; row <= numRows; row++)
	{
		for (column = (backwardsNumRows -1); column > 0; column --)
		{
			cout << "#";
		}
	
	cout << "X" << "\n";
	backwardsNumRows--;
	}
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.