JosesFalco
JosesFalco

Reputation: 1

Inputs writing to the wrong position of an array in C

I'm working on an assignment for multiplying two 2d matrices of variable sizes.

I keep having the same issue, and am stuck on how to fix it: When calling for input to define the matrix, the last column of the row doesn't get the input for some reason. Instead the first column of the next row gets written to both spots.

For instance, if you are trying to enter a 2 x 2

1  2
3  4

It will get stored as

1  3
3  4

I've searched around and can't seem to find any similar issues. It does the same thing for any size, all the inputs are good until the last position of the row. If it is the last row the last position stays good.

I'll post the code here, if someone could give me any pointers as to which direction to head to fix this I would appreciate it. (As well as any pointers on cleaning up the rest, its functional but I'm sure it aint pretty to the experienced eye.)

The culprit is somewhere in here I believe

for(j=0; j < Rows_1; ++j){


        for(i=0; i < Columns_1; ++i){
            printf("\nEnter the value for 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 is here

//HW5....Program for Matrix Multiplication....

int main(void){
    //first we will verify that the two matrices can be mulitiplied by comparing rows and columns....
    static int Rows_1, Columns_1, Rows_2, Columns_2; 
    int i, j, k, l, m, n, p, q;
    //char input[4];

    //call for user input to define size of matrix 1 and matrix 2
    printf("Please enter the size of the first matrix you wish to multiply...\n# of Rows=");
    scanf("%d",&Rows_1);
    printf("# of Columns=");
    scanf("%d",&Columns_1);
    printf("Please enter the size the second matrix you wish to multiply...\n# of Rows=");
    scanf("%d",&Rows_2);
    printf("# of Columns=");
    scanf("%d",&Columns_2);

    //defining the size of the matrices using inputted values (-1 because arrays count 0 as a 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 the two matrices are compatible in size to be multiplied


        printf("\nYou are attempting to multiply a %d x %d matrix with a %d x %d matrix",Rows_1, Columns_1, Rows_2, Columns_2);
        printf("\nThe resulting matrix will be %d x %d",Rows_1, Columns_2);


        for(j=0; j < Rows_1; ++j){

            for(i=0; i < Columns_1; ++i){
                printf("\nEnter the value for 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 the value for 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 two matrices entered cannot be multiplied together.");
    }


    ;
    getchar();
    return 0;
}

Thanks for looking!

Upvotes: 0

Views: 52

Answers (2)

AJNeufeld
AJNeufeld

Reputation: 8695

Your problem is here:

//defining the size of the matrices using inputted values (-1 because arrays count 0 as a row.)
int Matrix_1[(Rows_1-1)][(Columns_1-1)],Matrix_2[(Rows_2-1)][(Columns_2-1)];

You should NOT use "-1". Although the arrays count from 0, and they end at "n-1", they must be declared of size "n":

int Matrix_1[Rows_1][Columns_1],Matrix_2[Rows_2][Columns_2];

Upvotes: 3

Spikatrix
Spikatrix

Reputation: 20244

int Matrix_1[(Rows_1-1)][(Columns_1-1)],Matrix_2[(Rows_2-1)][(Columns_2-1)];

should be

int Matrix_1[Rows_1][Columns_1], Matrix_2[Rows_2][Columns_2];

because you want to have two matrices of size Rows_1 x Columns_1 and Rows_2 x Columns_2.

Upvotes: 2

Related Questions