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

For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
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
*****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...
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
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).
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT