arrayscmatrix

Logical error in matrix calculation code in C. error suspected in scope of Arrays


I was expected to write a C code where we initialize Two matrices, both of size 100 by 100. the user is to input the sizes of the matrices that he wants to multiply and input the values too.

The Martices werent multiplying properly but there wasnt any issue in the multiplymatrices function. I believe that the error is being caused is that the matrix A and B arent populating properly. this is the entire code:

ps, we arent allowed to use pointers or dynamically create arrays.

#include <stdio.h>

//prototypes
void getMatrixElements(int row, int col, int array[row][col]);
void multiplyMatrices(int size_1[], int size_2[],int rows,int cols, int result[rows][cols]);
void display(int row,int col,int matrix[row][col]);

//declare variables
int matrix_A[100][100] = {0};
int matrix_B[100][100] = {0};



int size_A[2] = {-1,-1}; //holds size given by user for matrix a
int size_B[2] = {-1,-1}; //holds size given by user for matrix b


int main(void)
{

    //get inputs from user

    do
    {

    printf("Remember to enter valid sizes!\n\n");

    printf("Please enter row size of Matrix A: ");
    scanf(" %d", &size_A[0]);

    printf("Please enter column size of Matrix A: ");
    scanf(" %d", &size_A[1]);

    printf("Please enter row size of Matrix B: ");
    scanf(" %d", &size_B[0]);

    printf("Please enter column size of Matrix B: "); // Fixed typo here
    scanf(" %d", &size_B[1]);
}
while (size_A[1] != size_B[0]);


int matrix_C[100][100] = {0};

    //populate the array now
    printf("\nNow enter the values of the matrix A\n\n");
    getMatrixElements(size_A[0], size_A[1], matrix_A);

    //display(size_A[0],size_A[1],matrix_A);

    for (int i = 0; i < 2; i++)
    {
        for(int j = 0; j < 2; j++)
        {
            printf("\n\n %d  ", matrix_A[i][j]);
        }
    }
    printf("\nNow enter the values of the matrix B\n\n");
    getMatrixElements(size_B[0], size_B[1], matrix_B);

    display(size_B[0],size_B[1],matrix_B);


    printf("abc:  %d\n", matrix_B[1][0]);


    //now multiply the matrices
    multiplyMatrices(size_A, size_B, size_A[0],size_B[1], matrix_C);

    printf("\n\n");
    //display resultant
    display(size_A[0],size_B[1],matrix_C);

    return 0;
}

//TODO

void getMatrixElements(int row, int col, int array[row][col]) {
    for (int i = 0; i < row; i++) {

        printf("Enter row %d values\n", i + 1);

        for (int j = 0; j < col; j++) {

            printf("Enter value: ");
            scanf(" %d", &array[i][j]);
        }
    }
    return;
}


//TODO

void multiplyMatrices(int size_1[], int size_2[],int rows,int cols, int result[rows][cols])
{
    int row1= size_1[0];
    int col1 = size_1[1];

    int row2= size_2[0];
    int col2 = size_2[1];

    printf("%d\n", matrix_A[1][0]);
    printf("%d", matrix_B[1][0]);
    for (int i = 0; i < row1; i++) //loop over the rows of the first matrix
    {
        for (int j = 0; j < col2; j++)//cols of two
        {
            result[i][j] = 0;

            for (int a = 0; a < col1; a++) //cols of 1
            {
                result[i][j] += matrix_A[i][a] * matrix_B[a][j];
            }

        }
    }

    return;
}

void display(int row, int col,int matrix[row][col])
{
    for (int i = 0; i < row; i++)
    {
        printf("[  ");
        for (int j = 0; j < col; j++)
        {
            printf("%d  ", matrix[i][j]);
        }
        printf("  ]\n");
    }
    return;
}

When i Hardcoded the array sizes and inputted the values according to them then the multiplication worked properly. i tried to change the scope of the arrays but that also didn't work. I thought there was an error in the multipier function but that also wasn't the case.

when i print the first row after populating the array they print just fine but the second and rows after that are zero for somereason. but when i try to print the array by passing it through a function such as the display() function it works. any help would be greatly appreciated!


Solution

  • The functions that access the 2-D array need to use the actual column dimension from the declaration of the array object, not the number of columns supplied by the user. The user's matrix will use the top-left corner of the 2-D array (assuming row 0, column 0 is the top-left corner). An R×C user matrix would be stored in the 100×100 array like this:

             <-------------- used ------------------> <--------- unused --------->
           ^ [ 0 ][ 0 ], [ 0 ][ 1 ], ..., [ 0 ][C-1], [ 0 ][ C ], ..., [ 0 ][ 99];
      used | [ 1 ][ 0 ], [ 1 ][ 1 ], ..., [ 1 ][C-1], [ 1 ][ C ], ..., [ 1 ][ 99];
           |   .                     .                            .           .
           |   .                       .                            .         .
           v [R-1][ 0 ], [R-1][ 1 ], ..., [R-1][C-1], [R-1][ C ], ..., [R-1][ 99];
           ^ [ R ][ 0 ], [ R ][ 1 ], ..., [ R ][C-1], [ R ][ C ], ..., [ R ][ 99];
    unused |   .                     .                            .           .
           |   .                       .                            .         .
           v [ 99][ 0 ], [ 99][ 1 ], ..., [ 99][C-1], [ 99][ C ], ..., [ 99][ 99].
    

    The functions can just assume the 2-D arrays have 100 columns, like this:

    void getMatrixElements(int row, int col, int array[][100]);
    void multiplyMatrices(int size_1[], int size_2[],int rows,int cols, int result[][100]);
    void display(int row,int col,int matrix[][100]);
    

    Alternatively, you can add an extra parameter for the array column dimension like this:

    void getMatrixElements(int row, int col, int col_dim, int array[][col_dim]);
    void multiplyMatrices(int size_1[], int size_2[],int rows,int cols, int col_dim, int result[][col_dim]);
    void display(int row,int col,int col_dim,int matrix[][col_dim]);
    

    Callers of the functions would set the col_dim argument to 100 to match the declared number of columns.

    For multiplyMatrices it is assumed that all three arrays have the same number of columns, but you could use separate col_dim parameters for each array if necessary. You should also be passing matrix_A and matrix_B as parameters of the function, not hard-coding them.


    It is also possible to store the matrices in a flattened, 1-D form and calculate the index of each element as r * cols + c. That may be more flexible because you can store any size matrix whose number of elements (rows * cols) is less than or equal to the 1-D array dimension. Only the first rows * cols elements of the 1-D array will be used to store the user's matrix. All the unused elements will be at the end of the 1-D array. This differs from the 2-D array storage where there may be unused columns at the end of each row, and unused rows at the end of the 2-D array.