Question

In: Computer Science

Write a C++ program to multiply two matrices a and b and print the result. Use...

Write a C++ program to multiply two matrices a and b and print the result. Use two-dimensional arrays to represent the matrices.

Solutions

Expert Solution

C++ Code:

#include <iostream>
using namespace std;

#define N 4

void multiply(int m1[][N],int m2[][N],int m3[][N])
{
   int i, j, k;
   for (i=0;i<N;i++) {
       for (j=0;j<N;j++) {
           m3[i][j]=0;
           for(k=0;k<N;k++)
               m3[i][j]+=m2[i][k]*m2[k][j];
       }
   }
}
int main()
{
   int i, j;
   int m3[N][N]; // To store result
   int m1[N][N]={{ 1,2,3,4 },{ 4,3,2,1 },{1,2,3,4},{4,3,2,1 } };

   int m2[N][N]={ { 1,2,3,4 },{1,2,3,4},{1,2,3,4},{1,2,3,4} };
multiply(m1, m2, m3);

   cout << "Result after multiplication of matrix is \n";
   for (i=0;i<N;i++) {
       for (j=0;j<N;j++)
           cout<<m3[i][j]<< " ";
       cout << "\n";
   }
return 0;
}

if you like the answer please provide a thumbs up


Related Solutions

Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
Write a program that reads two square Matrices of size 2 *2 and calculate and print...
Write a program that reads two square Matrices of size 2 *2 and calculate and print the sum of them. Also, the signed value of the matrices are input by the user Note: 1) Use CamelCase for variables names 2) Write Comments such as your name, the class, description of the program, and description of your logic. 4) Use sematic names for your variables 5) Your program should be C program not C++.
Write a Fortran program that reads in two NxN matrices A & B, and prints their...
Write a Fortran program that reads in two NxN matrices A & B, and prints their element-wise sum (A+B), element-wise difference (A-B), element-wise division (A/B), element-wise product (A*B), and their matrix product (matmul(A,B)), on the standard output.
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Complete the following program so that C = A*B where A and B are the matrices...
Complete the following program so that C = A*B where A and B are the matrices defined below. N=4; M=3; A=rand(M,N); B=rand(N,M); C = ? for m = 1: M    for n = 1 : ? for p = 1 : ? C (m,n) = ?       end    end end You may upload a script
*****In C++***** Exercise #3: Develop a program (name it AddMatrices) that adds two matrices. The matrices...
*****In C++***** Exercise #3: Develop a program (name it AddMatrices) that adds two matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...
Write a program to multiply two polynomials. Code needed in Java.
Write a program to multiply two polynomials. Code needed in Java.
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT