Inputs writing to the wrong position of an array in C -
i'm working on assignment multiplying 2 2d matrices of variable sizes.
i keep having same issue, , stuck on how fix it: when calling input define matrix, last column of row doesn't input reason. instead first column of next row gets written both spots.
for instance, if trying enter 2 x 2
1 2 3 4
it stored as
1 3 3 4
i've searched around , can't seem find similar issues. same thing size, inputs until last position of row. if last row last position stays good.
i'll post code here, if give me pointers direction head fix appreciate it. (as pointers on cleaning rest, functional i'm sure aint pretty experienced eye.)
the culprit somewhere in here believe
for(j=0; j < rows_1; ++j){ for(i=0; < columns_1; ++i){ printf("\nenter value row %d column %d=",j+1,i+1); /*fgets(input, sizeof(input)-1, stdin); sscanf("%d", &matrix_1[j][i]);*/ scanf("%d",&matrix_1[j][i]); } }
the whole code here
//hw5....program matrix multiplication.... int main(void){ //first verify 2 matrices can mulitiplied comparing rows , columns.... static int rows_1, columns_1, rows_2, columns_2; int i, j, k, l, m, n, p, q; //char input[4]; //call user input define size of matrix 1 , matrix 2 printf("please enter size of first matrix wish multiply...\n# of rows="); scanf("%d",&rows_1); printf("# of columns="); scanf("%d",&columns_1); printf("please enter size second matrix wish multiply...\n# of rows="); scanf("%d",&rows_2); printf("# of columns="); scanf("%d",&columns_2); //defining size of matrices using inputted values (-1 because arrays count 0 row.) int matrix_1[(rows_1-1)][(columns_1-1)],matrix_2[(rows_2-1)][(columns_2-1)]; if(rows_2==columns_1){ //checking if 2 matrices compatible in size multiplied printf("\nyou attempting multiply %d x %d matrix %d x %d matrix",rows_1, columns_1, rows_2, columns_2); printf("\nthe resulting matrix %d x %d",rows_1, columns_2); for(j=0; j < rows_1; ++j){ for(i=0; < columns_1; ++i){ printf("\nenter value row %d column %d=",j+1,i+1); /*fgets(input, sizeof(input)-1, stdin); sscanf("%d", &matrix_1[j][i]);*/ scanf("%d",&matrix_1[j][i]); } } //printf("%d %d %d %d",matrix_1[0][0],matrix_1[0][1],matrix_1[1][0],matrix_1[1][1]); /* for(k=0; k < rows_2; k++){ for(l=0; l < columns_2; l++){ printf("enter value row %d column %d",k+1,l+1); scanf("%d",&matrix_2[k][l]); } }*/ printf("matrix 1 =\n"); for(p=0; p < rows_1; ++p){ for(q=0; q < columns_1; ++q){ printf("%d ",matrix_1[p][q]); } printf("\n"); } /* printf("matrix 2 =/n"); for(m=0; m < rows_2; m++){ for(n=0; n < columns_2; n++){ printf("%d ",matrix_2[m][n]); } printf("\n"); } */ } else{ printf("\nthe 2 matrices entered cannot multiplied together."); } ; getchar(); return 0; }
thanks looking!
your problem here:
//defining size of matrices using inputted values (-1 because arrays count 0 row.) int matrix_1[(rows_1-1)][(columns_1-1)],matrix_2[(rows_2-1)][(columns_2-1)];
you should not use "-1". although arrays count 0, , end @ "n-1", must declared of size "n":
int matrix_1[rows_1][columns_1],matrix_2[rows_2][columns_2];
Comments
Post a Comment