Creating matrices using for-loops in C -
i'm trying create 2 matrices using loops in c.
matrix [18x16], each element a[i,j] = + j, (i=1,...,18; j=1,...,16)
matrix b [16x18], each element b[i,j] = + 2j, (i=1,...,16; j=1,...,18)
according ubuntu (virtual machine) terminal output, i'm missing entire row matrix a, , both matrix , b have incorrect elements @ 2 specific places: a[17,16] , b[15,18], respectfully (please see picture below).
code follows, , appreciated.
#include <stdio.h> int a[18][16]; int b[16][18]; int i, j; int main(void) { // create matrix for(i = 1; < 18; i++){ for(j = 1; j < 16; j++){ a[i][j] = + j; printf("%d ", a[i][j]); } // end inner loop printf("\n"); } // end outer loop printf("\n"); // create b matrix for(i = 1; < 16; i++){ for(j = 1; j < 18; j++){ b[i][j] = + (2*j); printf("%d ", b[i][j]); } // end inner loop printf("\n"); } // end outer loop }
terminal output:
as holsetly pointed out, c 0-based.
so need make following changes.
for(i = 0; < 18; i++){ for(j = 0; j < 16; j++){ a[i][j] = + j + 2; // add 1 i, 1 j printf("%d ", a[i][j]); } // end inner loop printf("\n"); } // end outer loop
similarly, make changes other loop.
edit: second loop, need add 3, i + 2j
proper answer.
Comments
Post a Comment