Write a C++ program that sums the content of two 3 times 3 matrices array A and
ID: 3698074 • Letter: W
Question
Write a C++ program that sums the content of two 3 times 3 matrices array A and arrayB and store the output in arrayC. I or example, cell 0.0 in arrayC should hold the sum of cell 0,0 from array A and cell 0,0 from arrayB, etc. Your program should use three functions: readData//fills an array (make it general to till only one array and any array) sum//sums two arrays and place output in a third printArray//print the content of any array (make it general to print only one array and any array) Your program will perform the following: Fill arrays: array A and arrayB by calling readData 2 times. Print the content of both arrays. sum the arrays. Print the content arrayC by calling printArray 3 times.Explanation / Answer
Test.cpp
#include <iostream>
using namespace std;
void readData(int** a,int numRow,int numCol);
void sum(int** a,int** b,int** c,int numRow,int numCol);
void print(int** a,int numRow,int numCol);
int main()
{
int** a= new int*[3];
int** b=new int*[3];
int** c=new int* [3];
for(int i = 0; i <3; i++)
{
a[i] = new int[3];
b[i] = new int[3];
c[i] = new int[3];
}
cout<<"Enter the data for first array"<<endl;
readData(a,3,3);
cout<<"Enter the data for second array"<<endl;
readData(b,3,3);
cout<<"First array is"<<endl;
print(a,3,3);
cout<<endl;
cout<<"Second array is"<<endl;
print(b,3,3);
cout<<endl;
cout<<"Sum of first and second array is"<<endl;
sum(a,b,c,3,3);
print(c,3,3);
delete []a;
delete []b;
delete []c;
return 0;
}
void readData(int** a,int numRow,int numCol)
{
for(int i=0;i<numRow;++i)
{
for(int j=0;j<numCol;++j)
{
cout<<"Enter a value"<<endl;
cin>>a[i][j];
}
}
}
void sum(int** a,int** b,int** c,int numRow,int numCol)
{
for(int i=0;i<numRow;++i)
{
for(int j=0;j<numCol;++j)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void print(int** a,int numRow,int numCol)
{
for(int i=0;i<numRow;++i)
{
for(int j=0;j<numCol;++j)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
Sample output:
nter the data for first array
Enter a value
1
Enter a value
2
Enter a value
3
Enter a value
4
Enter a value
5
Enter a value
6
Enter a value
7
Enter a value
8
Enter a value
9
Enter the data for second array
Enter a value
1
Enter a value
1
Enter a value
1
Enter a value
1
Enter a value
1
Enter a value
1
Enter a value
1
Enter a value
1
Enter a value
1
First array is
1 2 3
4 5 6
7 8 9
Second array is
1 1 1
1 1 1
1 1 1
Sum of first and second array is
2 3 4
5 6 7
8 9 10
logout
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.