You must Sign In to post a response.
  • Write a C program to add two 3 x 3 matrices


    Are you preparing for Diploma MSBTE exam? Looking out for a program solution for addition of two matrices? Our ISC experts shall provide you a program solution on this page to solve the problem.

    Following is a question that was asked in the question paper of Diploma in Electronics and Telecommunication Engineering (Summer 2019)conducted by MSBTE Mumbai, for subject C Programming (22218).

    This question is asked for 6 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a program to add two 3 × 3 matrices. [6 Marks]
  • Answers

    2 Answers found.
  • Two dimensional array represents matrix structure. In two dimensional array first dimension represents row and second dimension represents column.
    e.g. int mat[2][3];
    In above example mat is name of array which holds all integer values. This array holds total 6 integers. In above declaration first dimension which is 2 which represents row and second dimension which is 3 that represents columns and hence this array has two rows and each row has three columns.
    In case of two or more dimensional array user have to use loop for accepting the array elements, processing array elements or outputting array elements. In general for every purpose user have to use loop.
    Below is the program to add two 3 x 3 matrices.
    Please include stdio.h and conio.h before main function.
    //ADDITION OF TWO MATRICES.
    #include
    #include
    void main(void)
    {
    int m1[3][3],m2[3][3],add[3][3],i,j;
    clrscr();
    printf("Enter first matrix elements(any 9 numbers)\n");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&m1[i][j]);
    }
    }
    printf("Enter second matrix elements(any 9 numbers)\n");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&m2[i][j]);
    }
    }
    printf("\n************************************************************\n");
    printf("Resultant matrix is as follows\n");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    add[i][j]=m1[i][j]+m2[i][j];
    printf("%d ",add[i][j]);
    }
    printf("\n");
    }
    getch();
    }

  • Below is my code.

    #include

    int main()
    {
    int matrix1[10][10], matrix2[10][10], sumOfMatrix[10][10];

    printf("Enter first 3*3 matrix : ");
    for(int i = 0; i < 3; i++)
    {
    for(int j = 0; j < 3; j++)
    {
    scanf("%d ", &matrix1[i][j]);
    }
    }

    printf("\nEnter second 3*3 matrix: ");
    for(int i = 0; i < 3; i++)
    {
    for(int j = 0; j < 3; j++)
    {
    scanf("%d ", &matrix2[i][j]);
    }
    }

    printf("\n Sum of both matrix is :");
    for(int i = 0; i < 3; i++)
    {
    for(int j = 0; j < 3; j++)
    {
    sumOfMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
    printf("%d ", sumOfMatrix[i][j]);
    }
    printf("\n");
    }
    return 0;
    }


  • Sign In to post your comments