trouble with array

Hello all, my problem is simple. I'm trying to fill Test[6][6] with numbers starting at 0 and ending with 25(at test[6][6]). I can get the first row easy but when i include a second for loop i seem to kill off everything.

The part that i cant understand is trying to continue from the last number on the first row. Second row should start at 6 and end with 10, third row start at 11 end at 15...

my only source has been from the tutorial so i have no other example. TY for your time.
I know i could just hard code it but seems as massive waste of time and i plan on using a much bigger array for my next project
Last edited on
Hi Legion.
Your test array is a 6x6, so I don't understand what you mean by filling it with 0 through 25, unless you are mistaken about the dimensions. Arrays begin their indexing at 0, so you need to count from 0 to 5 (which is 6 numbers.)

If you want to have a 5x5 array, you can define it as: int test[5][5];

Here is a way to fill a 5x5 array with numbers 0 - 24:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
  int test[5][5];
  int k = 0;
  for (int i = 0; i < 5; ++i) {
    for (int j = 0; j < 5; ++j) {
      test[i][j] = k;
      ++k;
    }
  }
}


~psault
Last edited on
yes sorry i meant 5 actually, i have 1 question, could you please explain the specifics behind k, i dont follow what its purpose is. Thank you for posting the code.
int k is the number that is incrementing from 0 to 24. The ++ operator means basically: k = k + 1 (in this context. It is actually a little more complicated than that.)

As we loop over our matrix, we need to assign k to test[i][j] and then add 1 to k for the next iteration so that we go from 0 - 24.

~psault
Topic archived. No new replies allowed.