Vali RO
Vali RO

Reputation: 61

Sum and product of two matrices in C (with functions)

I have to write a program in C which finds the sum and the product of two matrices.

I wrote the functions but I get stuck at calling them in main. I don't know which variable is for rows and columns of result matrix.

#include <stdio.h>
void enterMatrix(int a[][10], int rows, int columns)
{
    int i,j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("a(%d,%d)=",i,j);
            scanf("%d",&a[i][j]);
        }
    }
}
void displayMatrix(int a[][10], int rows, int columns)
{
    int i,j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            printf("%d", a[i][j]);
            printf(" ");
        }
        printf("\n");
    }
}
void matrixSum(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
    int i,j;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }
}
void matrixProduct(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
    int i,j,k;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            c[i][j]=0;
            for(k=0;k<columns;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }

        }
    }
}
int main(void)
{
    int a[10][10], b[10][10], sum[10][10], product[10][10];
    int rowsA,columnsA,rowsB,columnsB;
    printf("Number of rows for matrix A: \n");
    scanf("%d",&rowsA);
    printf("Number of columns for matrix A: \n");
    scanf("%d",&columnsA);
    printf("Number of rows for matrix B: \n");
    scanf("%d",&rowsB);
    printf("Number of columns for matrix B: \n");
    scanf("%d",&columnsB);
    printf("Enter first matrix: \n");
    enterMatrix(a,rowsA,columnsA);
    printf("Show first matrix: \n");
    displayMatrix(a,rowsA,columnsA);
    printf("Enter second matrix: \n");
    enterMatrix(b,rowsB,columnsB);
    printf("Show second matrix: \n");
    displayMatrix(b,rowsB,columnsB);

    if((rowsA==rowsB) && (columnsA==columnsB))
    {
        matrixSum(a,b,sum, ???, ???);
        printf("The sum matrix is: \n");
        displayMatrix(sum, ???, ???);
    }
    else
    {
        printf("Wrong information.");
    }
    if((rowsA==columnsB) && (rowsB==columnsA))
    {
        matrixProduct(a,b,product,???,???);
        printf("The product matrix is \n");
        displayMatrix(product,???,???);
    }
    return 0;
}

Upvotes: 0

Views: 3358

Answers (1)

JohanC
JohanC

Reputation: 80409

For matrixSum you just give rowsA and columnsA, as they are equal to rowsB and columnsB.

For matrixProduct you need three numbers: rowsA, columnsA and columnsB. rowsB is not needed, as it is equal to columnsA.

You need to change your matrixProduct function to use these three numbers at the correct places. Your current code doesn't work except when all numbers are equal.

Also your test if((rowsA==columnsB) && (rowsB==columnsA)) before calling matrixProduct only needs if(rowsB==columnsA).

if((rowsA==rowsB) && (columnsA==columnsB))
{
    matrixSum(a,b,sum, rowsA, columnsA);
    printf("The sum matrix is: \n");
    displayMatrix(sum, rowsA, columnsA);  // the sum has the same dimensions as A and B
}
else
{
    printf("Both matrices don't have equal dimension.\n");
}
if(rowsB==columnsA)
{
    matrixProduct(a,b,product,rowsA,columnsA,columnsB);
    printf("The product matrix is \n");
    displayMatrix(product,rowsA,columnsB);  // the product matrix has the
       // number of rows of A and the number of columns of B
}
else
{
    printf("The number of columns of A needs to be equal to the number or rows of B.\n");
}

Your matrixProduct function could be adapted as follows:

void matrixProduct(int a[][10], int b[][10], int c[][10], int rowsA, int columnsA, int columnsB)
{
    int i,j,k;
    for(i=0;i<rowsA;i++)
    {
        for(j=0;j<columnsB;j++)
        {
            c[i][j]=0;
            for(k=0;k<columnsA;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }
        }
    }
}

Upvotes: 1

Related Questions