Home > Blockchain >  Matrix Multiplication in C - Problem with inputs
Matrix Multiplication in C - Problem with inputs

Time:12-08

I've written a program which carries out matrix multiplication using functions. The function which i presume is wrong is as follows:

void obtainMatrixElems(int mtrx[][10], int row_elems, int col_elems){
    printf("Kindly enter matrix elements: \n");

    for(int x = 0; x < row_elems; x  ){
        for(int y = 0; y < col_elems; y  ){
            printf("Enter element at position %d,%d: \n", x 1, y 1);
            scanf("&d", &mtrx[x][y]);
        }
    }
}

When I run the program, I am met with this output:

Kindly enter the number of row/s for the first matrix: 
3
Kindly enter the number of column/s for the first matrix: 
3
Kindly enter the number of row/s for the first matrix: 
3
Kindly enter the number of column/s for the second matrix: 
3
Kindly enter matrix elements: 
Enter element at position 1,1: 
Enter element at position 1,2: 
Enter element at position 1,3: 
Enter element at position 2,1: 
Enter element at position 2,2: 
Enter element at position 2,3: 
Enter element at position 3,1: 
Enter element at position 3,2: 
Enter element at position 3,3: 
Kindly enter matrix elements: 
Enter element at position 1,1: 
Enter element at position 1,2: 
Enter element at position 1,3: 
Enter element at position 2,1: 
Enter element at position 2,2: 
Enter element at position 2,3: 
Enter element at position 3,1: 
Enter element at position 3,2: 
Enter element at position 3,3: 

RESULTANT MATRIX:
000
000
000

Process finished with exit code 0

There seems to be a problem with the loop in function obtainMatrixElems - although I could be wrong. I'm quite lost as to what it may be so any help would be greatly appreciated.

Tried carrying out matrix multiplication in C using functions - function which obtains matrix elements loops twice and does not except any inputs. Program terminates after with code 0.

CodePudding user response:

There is an error in your call to scanf It should read

scanf("%d", &mtrx[x][y]);

Also take care with hitting the Enter Key after each input. To be safe you should catch it. Use something like

scanf(" %d", &mtrx[x][x]);

CodePudding user response:

It seems you are missing a " " at multAns[x][y] = matrix1[x][y] * matrix2[x][y];.

It should rather be:

// Also note the change variables used for referencing cells ...
multAns[x][y]  = matrix1[x][z] * matrix2[z][y];

In case your last operation result is 0 then this explains why you get a 0 matrix..

EDIT:

The sign is one of the problems .. There is also something wrong with the way you get input from the user.

    for(int x = 0; x < row_elems; x  ){
        for(int y = 0; y < col_elems; y  ){
            printf("Enter element at position %d,%d: \n", x 1, y 1);
            // Note the change of "&" to "%" and the extra sequence
            // "\r\n" which expects the user to press ENTER (i.e.:
            // new line) between input cells
            rc = scanf("%d\r\n", &mtrx[x][y]);
            if (rc != 1) {
                printf("ERROR: scanf did not proceed as expected\r\n");
            }
        }
    }
  • Related