???
for(j=0;j<4;j++)matris[0][j] = j + 1; //Initiate
(1)
The first array should be initiated.
1 2 3 4
for(j=0;j<4;j++)matris[0][j] = j; //Initiate
(2)
0 1 2 3
This result is different than the result you must have. Then (1) is correct
Start :
1 2
|
for(i=0;i<5;i++){
if(i != 0){ //Skip level 0 (Because it's initiated)
| |
for(j=0;j<4;j++)matris[i][j] = matris[i - 1][j]; //Copy the previous value from previous array
If i > 0, then copy the previous value from previous array.
for(j=0;j<4;j++)matris[i][j] *= j + 1; }//Power up
Every element in this column array (i) will be multiplied by row index (j). Do not forget "+ 1", this causes the first element will always be a value : 0
So ,test the code (matris[1][j] (i = 1))
Edit : Fixed missing output
1 2 3 4
matris[1][0] = 1 * (0 + 1) = 1 * 1 = 1
matris[1][1] = 2 * (1 + 1) = 2 * 2 = 4
matris[1][2] = 3 * (2 + 1) = 3 * 3 = 9
matris[1][3] = 4 * (3 + 1) = 4 * 4 = 16
Result : 1 4 9 16
i++;
Copy previous values (i = 2):
matris[2][0] = matris[2 - 1][0] = matris[1][0] = 1
matris[2][1] = matris[2 - 1][1] = matris[1][1] = 4
matris[2][2] = matris[2 - 1][2] = matris[1][2] = 9
matris[2][3] = matris[2 - 1][3] = matris[1][3] = 16
(matris[2][j] (i = 2))
1 4 9 16
matris[2][0] = 1 * (0 + 1) = 1 * 1 = 1
matris[2][1] = 4 * (1 + 1) = 4 * 2 = 8
matris[2][2] = 9 * (2 + 1) = 9 * 3 = 27
matris[2][3] = 16 * (3 + 1) = 16 * 4 = 64
And continue i++....
Do you understand? You should improve your code executing. If you need help, simply test the debug source (with variable logging)
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
|
#include<stdio.h>
int main(){
int i=1,j=1;
int matris [5][4];
for(j=0;j<4;j++)matris[0][j] = j + 1; //Initiate
for(i=0;i<5;i++){
if(i != 0){ //Skip level 0 (Because it's initiated)
for(j=0;j<4;j++){
printf("Before : %d\n", matris[i][j]);
matris[i][j] = matris[i - 1][j];
printf("After : %d\n", matris[i][j]);}
for(j=0;j<4;j++){
printf("Multiply : matris[%d][%d] (%d) *= j (%d) + 1 = %d\n", i, j, matris[i][j], j, matris[i][j] * (j + 1));
matris[i][j] *= j + 1;}
}//Power up
}
for(i=0;i<5;i++){
for(j=0;j<4;j++){
printf("%d\t",matris[i][j]);
}
printf("\n");
}}
return 0;
}
| |
Hope this helps.