Nested for-loop creating values that don't make sense in array

Hey all. First time posting to this forum. I have an issue with creating values for an array in a nested for-loop. The output of this code results in garbage and non-sense values being printed to the Serial port. It seems so straight-forward that I can't figure out what is going wrong and where. I tried printing out the individual index values (idx) in each loop iteration, and they are also nonsense which makes sense as to why the values of the array are nonsense. I've tried troubleshooting and can't seem to come up with a good reason for this on my own. I took out all extraneous code to make sure nothing else was happening, so here is what remains and still has the problem occur:

int test[160];

void setup() {
  Serial.begin(9600);
  readMatrix(test);
  delay(1000);
  for(int i = 0; i <160; i++)
  {
    Serial.println(test[i]);
  }
}

void loop() {

}

void readMatrix(int mat[])
{
  int idx; // create index
  for (int i = 0; i < 10; i++){ 
    for (int j = 0; j < 16; j++){ // 10 * 16 loops goes through all 160 indeces
      idx = ((i+1) * (j+1)) - 1; // update index
      //idx = i;
      mat[idx] = idx; // set array value of index equal to the index value
      Serial.println(idx); // print it out
    }
  }
}

Editing the readMatrix() function to the following results in it printing out the values 1 - 160 exactly as intended in the above code:

void readMatrix(int mat[])
{
  int idx; // create index
  for (int i = 0; i < 160; i++){ 
    idx = i;
    mat[idx] = idx; // set array value of index equal to the index value
    Serial.println(idx); // print it out
  }
  
}

Questions I assume might be asked about the code and I will try to answer beforehand:

Why use a function to alter the array?
I need to use this function multiple times in the full code, and it would be far too messy otherwise.

What is the overall purpose of this?
Reading values from a force sensor matrix that is 10 x 16. The way the multiplexers are set up, the nested for loop is the easiest and best option for filling the values of the array with the force readings.

Board?
Arduino Uno. I don't think it's the board because it's working with tons of other scripts I've written for it.

Any help would be greatly appreciated. Also, with this being my first post, if this is the wrong place for this I will move it.

Define "nonsense"?

Your array function is getting a copy of the array which it modifies and throws away. Send a pointer to the first element instead.

If your values are small enough you can use a byte array and save half the space

Values will be between 0-1024 (coming from the analog input).

I don't have the board with me to show you the exact output right now. The values look something like this though:

0
0
0
0
5
0
0
0
10102
0
0
21312

That's something similar to the output.

As for sending a pointer, would that look like this?

*mat(idx) = idx;

No, it would look like &test[0], and just make the argument to the array function to be int *mat

OK, thanks for that. Any idea why the idx values are being screwed up with the nested for-loop as well?

Welcome,

Try

idx = ( i * 16 ) + j;

(fixed)

No, other than your first index is -1, which of course does not exist.

That makes sense. Nothing more frustrating than missing the little things like that after looking at your program for days. Thanks.