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
*****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...
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...
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 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT