I wanted to know if I could get some help creating a square matrix. I know how to create the matrix, but I need to populate the matrix. I am having trouble following the logic to create the following matrix:
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
Here is what I have in my code thus far: EDIT* I have changed the else statement from arr[i][j] += 1 to arr[i][j-1] + 1
void computeMatrix(int rows, int cols, int sqMatrix[][cols]){
int i,j;
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
if(i == 0 && j == 0){
arr[i][j] = 0;
}
else{
arr[i][j] = arr[i][j-1] + 1; //previously arr[i][j] += 1
}
}
}
}
The issue I am having is that this code makes the following matrix:
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 35
I am not sure what type of logic to implement to get the matrix correct. I have a general idea, but I am not sure how to implement it to the code. I know that as we go down the rows, we increase by 1, and as we go through the columns, we increase by 1 as well.
Any help I could get will be greatly appreciated!