Question

Write a C program to implement matrix multiplicati

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<stdio.h>

int main()
{
   int matrix1[20][20], matrix2[20][20], resultantMatrix[20][20],result=0;
   int row1,column1,row2,column2;
   int i=0,j=0,k=0;
  
   int repeat=1;
  
   //prompt and read number of rows and columns for the matrices until correct order is entered
   do
   {
       //read the size of matrix 1
       printf("\nEnter size of matrix1 (rows columns) : ");
       scanf("%d%d",&row1,&column1);
      
       //read the size of matrix 2
       printf("\nEnter size of matrix2 (rows columns) : ");
       scanf("%d%d",&row2,&column2);
      
       //check if mauliplication is possible or not
       if(column1!=row2)
       {
           //if number of columns in matrix 1 is not equal to number of rows in matrix 2
           //multiplication is not possible prompt again
           printf("\nThe matrices of the given size cannot be multiplied. Try again...\n");
           repeat=1;
       }
       else
       {
           repeat=0;
       }
   }while(repeat);

   //read the matrix 1
   printf("\nEnter matrix-1: \n");
   for(i=0;i<row1;i++)
   {
       for(j=0;j<column1;j++)
       {
           printf("\nEnter element for row %d X column %d :",(i+1),(j+1));
           scanf("%d", &matrix1[i][j]);
       }
   }
  
   //read the matrix 2
   printf("\nEnter matrix-2: \n");
   for(i=0;i<row2;i++)
   {
       for(j=0;j<column2;j++)
       {
           printf("\nEnter element for row %d X column %d :",(i+1),(j+1));
           scanf("%d", &matrix2[i][j]);
       }
   }
  
   //multiply the two matrices and store the values in resultant matrix
   for(i=0;i<row1;i++)
   {
       for(j=0;j<column2;j++)
       {
           for(k=0;k<row2;k++)
           {
               //row value * column value
               result=result+matrix1[i][k]*matrix2[k][j];
           }
          
           //assign value in resultantMatrix
           resultantMatrix[i][j]=result;
           result=0;
       }
   }
  
   //clear the screen, can remove if not necessary
   system("cls");
   printf("\nMatrix-1 is:\n");
  
   //print matrix 1
   for (i=0;i<row1;i++)
   {
   for (j=0; j<column1;j++)
    {
        printf("%d\t", matrix1[i][j]);
       }
   printf("\n");
}
  
//print matrix 2
printf("\nMatrix-2 is:\n");
   for (i=0;i<row2;i++)
   {
   for (j=0;j<column2;j++)
    {
        printf("%d\t", matrix2[i][j]);
       }
   printf("\n");
}
  
//print the resultant matrix
printf("\nResultant Matrix is:\n");
   for (i=0;i<row1;i++)
   {
   for (j=0;j<column2;j++)
    {
        printf("%d\t", resultantMatrix[i][j]);
       }
   printf("\n");
}
   return 0;
}

Output:

C:Users\Vickyl\Desktop DevC++Programs MatrixMultiplication.exe Enter size of matrixi1 <rows columns : 2 2 Enter size of matriCAUsers Vicky Desktop\DevC++Programs MatrixMultiplication.exe Matrix-1 is: 3 2 4 Matrix-2 is: 5 7 6 8 Resultant Matrix is: 19CAUsers\VickyDesktopDevC++Programs\MatrixMultiplication.exe (rows colunns> : 2 3 Enter size of matrix! Enter size of matrix2.. CAUsers\VickyDesktop\DevC++Programs\MatrixMultiplication.exe Matrix-1 is: 4 2 5 3 6 Matrix-2 is: 2 9 8 10 12 Resultant Mat

Add a comment
Know the answer?
Add Answer to:
Write a C program to implement matrix multiplication operations. First, you need to prompt the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • It is required to write a Matrix Calculator Program using C Programming Language that could only...

    It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program...

  • Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming...

    Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • 06) Write a C program to perform matrix operations based of threads. The program accepts from...

    06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the...

  • The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be...

    The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be as simple as asking the user to enter two matrices and then presenting a simple menu that allows the user to select the operation they want to test. Have the menu in a loop so that the user can test any other operation unless they choose to exit the menu. Also, provide the user an option to select two new matrices. Make sure that...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

    Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT