Question

In: Computer Science

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

Solutions

Expert Solution

Program :

#include <iostream>
using namespace std;
void Read_a_Matrix();
void Add_two_matrices();
void Subtract_two_matrices();
void multiplication_two_matrices();
void display_a_matrice();
int main()
{
  
Read_a_Matrix();
cout<<"\n====== Add two matrices=======\n";
Add_two_matrices();
cout<<"\n====== Subtraction of two matrices=======\n";
Subtract_two_matrices();
cout<<"\n====== Multiplication of two matrices=======\n";
multiplication_two_matrices();
cout<<"\n=============== Display_a_matrice======\n";
display_a_matrice();

return 0;
}
void Read_a_Matrix()
{
int i,j, r,c;
cout<<"Enter row size:";
cin>>r;
cout<<"Enter column size:";
cin>>c;
int m1[r][c];
cout<<"Enter "<<r<<"*"<< c <<" values :\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>m1[i][j];
}
  
}
void Add_two_matrices()
{
int r1,c1,i,j,r2,c2;
cout<<"Enter 1st matrix row size:";
cin>>r1;
cout<<"Enter 1st matrix column size:";
cin>>c1;
int add[r1][c1];
int m1[r1][c1];
cout<<"Enter "<<r1<<"*"<< c1 <<" values of matrix one:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cin>>m1[i][j];
}
cout<<"Enter 2nd matrix row size:";
cin>>r2;
cout<<"Enter 2nd matrix column size:";
cin>>c2;
int m2[r2][c2];
cout<<"Enter "<<r2<<"*"<< c2 <<" values of matrix2 :\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cin>>m2[i][j];
}
  
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]+m2[i][j];
}
}
cout<<"\n Addtion of two matrixces is:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cout<<add[i][j]<<" ";
cout<<"\n";
}

}
void Subtract_two_matrices()
{
int r1,c1,i,j,r2,c2;
cout<<"Enter 1st matrix row size:";
cin>>r1;
cout<<"Enter 1st matrix column size:";
cin>>c1;
int m1[r1][c1];
int sub[r1][c1];
cout<<"Enter "<<r1<<"*"<< c1 <<" values of matrix one:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cin>>m1[i][j];
}

cout<<"Enter 2nd matrix row size:";
cin>>r2;
cout<<"Enter 2nd matrix column size:";
cin>>c2;
int m2[r2][c2];
cout<<"Enter "<<r2<<"*"<< c2 <<" values of matrix two:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cin>>m2[i][j];
}
  
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
sub[i][j]=m1[i][j]-m2[i][j];
}
}
cout<<"\n Subtraction of two matrixces is:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cout<<sub[i][j]<<" ";
cout<<"\n";
}

}
void multiplication_two_matrices()
{
int r1,c1,i,j,r2,c2,sum=0,k;
cout<<"Enter 1st matrix row size:";
cin>>r1;
cout<<"Enter 1st matrix column size:";
cin>>c1;
int m1[r1][c1];
cout<<"\nEnter "<<r1<<"*"<< c1 <<" values of matrix one :\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cin>>m1[i][j];
}
cout<<"Enter 2nd matrix row size:";
cin>>r2;
cout<<"Enter 2nd matrix column size:";
cin>>c2;
int m2[r2][c2];
int mul[r1][c2];
cout<<"\nEnter "<<r2<<"*"<< c2 <<" values of matrix two:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cin>>m2[i][j];
}
  
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
sum = sum + m1[i][k]*m2[k][j];
}
mul[i][j] = sum;
sum = 0;
}
}
cout<<"\n multiplication two matrices of two matrixces is:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cout<<mul[i][j]<<" ";
cout<<"\n";
}

}

void display_a_matrice()
{
int i,j, r,c;
cout<<"Enter row size:";
cin>>r;
cout<<"Enter column size:";
cin>>c;
int m[r][c];
cout<<"Enter "<<r<<"*"<< c <<" values :\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>m[i][j];
}
cout<<"\ndisplay a matrice:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<m[i][j]<<" ";
cout<<"\n";
}
}


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 c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Write a program using c, c++, or java that have four dynamic memory partitions of size...
Write a program using c, c++, or java that have four dynamic memory partitions of size 100 KB, 500 KB, 200 KB, and 450 KB. The program should accept from user the number of processes and their sizes. Then output the assignment of processes using the next fit algorithm (specifying which process, if any, is block).
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT