Thread: sum and product of two matrix in C (with functions)

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    sum and product of two matrix in C (with functions)

    I have to write a program in C which finds the sum and the product of two matrix.
    I wrote the functions but I get stucked at calling them in main .I don't know which variable is for rows and columns of result matrix.

    Code:
    
    
    Code:
    #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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,615
    > .I don't know which variable is for rows and columns of result matrix.
    ...
    > if((rowsA==rowsB) && (columnsA==columnsB))
    But if they're the same, does it matter?

    matrixSum(a,b,sum, rowsA, columnsA);
    is exactly the same as
    matrixSum(a,b,sum, rowsB, columnsB);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-06-2016, 01:15 AM
  2. [C++] Matrix and vector product
    By Cazan Adelin in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2015, 02:50 PM
  3. Matrix Product
    By Cazan Adelin in forum C Programming
    Replies: 6
    Last Post: 04-08-2015, 08:31 AM
  4. Matrix product using macro definition
    By ElNoob in forum C Programming
    Replies: 5
    Last Post: 12-08-2012, 09:44 PM
  5. Matrix product
    By Cotizo in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2008, 10:10 AM

Tags for this Thread