Displaying more rows

Sep 19, 2014 at 5:19pm
Hi. I am a new programming student.
Recently I have been facing problems using for loop.
My assignment is that if I input 3 then a square of 3 x 3 will be displayed in this way:

&&&
&&&
&&&

Square of n x n will be shown if user inputs n.

Now I am able to display &&&, but I don't know how to display more rows.
I know it is something about looping one more time.
But I have no idea about how to write the second for loop.
I mean I don't get it.
The second for loop should result in the same display as the first row.
But the things inside the second for loop should be something else.
Should I declare another variable?
Can anybody give me some hints?

my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
    int size_square = 0;
    char symbol = '&';
    cout << "Enter the size of the square please.  ";
    cin >> size_square;


    for (int count = 0;count < size_square;count++)
    {
        for (//something should be written here)
        {
            //something should also be written here
            cout << endl;
            cout << symbol;
        }

    }
}
Last edited on Sep 20, 2014 at 2:05am
Sep 19, 2014 at 5:25pm
The inner loop should generate the row of ampersands. The outer loop should change to the next line by outputting a newline. Since it's meant to be a square, both loops will need to loop for size_square times (the inner loop will be executed repeatedly by the outer loop, of course).
Last edited on Sep 19, 2014 at 5:27pm
Sep 19, 2014 at 5:29pm
You have the right idea with the nested loops. You just need to output the symbol from the inner loop.

1
2
3
4
5
6
7
8
9
  //  Loop for each needed row
  for (int row=0; row<size_square; row++)
  {  //  Loop for each needed column within the row
      for (int col=0; col<size_square; col++)
      {  cout << synbol;
       }
       // All columns displayed, terminate the row
       cout << endl;
  }         


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on Sep 19, 2014 at 5:31pm
Sep 20, 2014 at 2:10am
Actually when I felt desperate last night, I went to bed.
And then I thought about using row and column to present it, which will be easier for me to read.
It turns out I am kinda right about it.

Thank you very much.
Topic archived. No new replies allowed.