display int array as a 10x10 table

I have my code working except for formatting it to print the array as a 10x10 table
I can't seem to figure out how to make it like that but I had been told that it would be easier copying the 1d array to a 2d array then printing it.
any way though would work
any suggestions on it would be great.

If you are able to copy the array to a 2D array you would be able to print it by printing instead of copying it.
Here is a simple demo, maybe you can adjust it.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void print_array(int array[], const size_t size, const size_t col_count)
{
  for (size_t i = 0; i < size; ++i)
  {
    if (i > 0 && i % col_count == 0)
      cout << "\n";

    cout << array[i] << " ";
  }
}

int main()
{
  int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

  print_array(array, 9, 3);

  system("pause");
  return 0;
}
I think this is what you are asking? The trouble is that 0%N is 0. Because of this you have to shift your modulo to get clean printing. If you wanted 5 on a row, that gives you 1,2,3,4,5*
and 5%5 = 0. Then you get 6,7,8,8,10 and 10%5 =0 again. So for a 1-d array printed as a 2-d array you just want:

for(i =1; i <= max; i++)
{
cout << array[i-1]; //add your favorite spaces, commas, or the like
if(i% rowlength == 0)
cout << endl;
}


Last edited on
Topic archived. No new replies allowed.