How to add columns

How do I go about adding columns into this code that solely has rows included?

Should be 4 rows and 4 columns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  void gridSize(int rows, int col)
{
    int element = 7;
    int loop = 0;
   for(int p = 0; p < rows; p++) {
      cout << "--- ";
      if (loop == (rows-1)){
        cout << endl;
        loop = 0;    
      }
        loop++;
   }
   for (int p = 0; p < rows; p++) {
        cout << "|" << "PE" << "| ";
      if (loop == (rows)){
        cout << endl;
        loop = 0;    
      }
        loop++;

    }
}
I'm not sure if this is what you are looking for but this will produce 4 rows and 4 columns. Using a nested for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  
  int rows = 4;
  int col = 4;
  
  for(int i = 1;i <= col; i++)
  {
      cout << i << "   ";
      for(int x = 1; x <= rows; x++)
      {
      cout << x << " ";
      }
      cout << endl;
  }
}


Output.

1
2
3
4
1   1 2 3 4 
2   1 2 3 4 
3   1 2 3 4 
4   1 2 3 4 
It's certainly closer to what I need than what I've got right now! Thank you.
Last edited on
Topic archived. No new replies allowed.