How can I output 2D array (8*8) but in order?
0 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 45 46 47 48 49
50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 64
65 66 67 68 69 70 71 72
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
|
#include <iostream>
using namespace std;
void setarray(int ma[8][8],int size,int jsize)
{
for (int i=0;i<size;i++)
for (int j=0;j<jsize;j++)
ma[i][j] = i+j;
}
void printarray(int ma[8][8],int dim1, int dim2)
{
for (int i=0;i<dim1;i++)
{
for (int j=0;j<dim2;j++)
cout<<ma[i][j]<<" ";
cout<<endl;
}
}
int main()
{
int multiarray[8][8];
setarray(multiarray,8,8);
printarray(multiarray,8,8);
system("pause");
}
|
I cannot find out a solution, help please.
Last edited on
What do you mean by "in order"?
@firedraco
respectively
0 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 45 46 47 48 49
50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 64
65 66 67 68 69 70 71 72
Last edited on
If I have understood what you want then you can use the following code to fill the array
1 2 3 4 5 6 7 8 9
|
int n = 0;
for ( int i=0; i < 8; i++ )
{
for ( int j=0; j < 8; j++, n = ( n == 34 ) ? 45 : n + 1 )
{
ma[i][j] = n;
}
}
|
The output is
0 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 45 46 47 48 49
50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65
66 67 68 69 70 71 72 73
|
I am sorry. I have not seen that 64 is duplicated in your output
58 59 60 61 62 63 64 64
65 66 67 68 69 70 71 72 |
Is it a typo?
Last edited on