Write a class named MatrixSum with the following two methods and functionality:
ID: 3602150 • Letter: W
Question
Write a class named MatrixSum with the following two methods and functionality:
1. Write a main method to prompt the user to enter a 3 by 4 matrix row by row.
Have this method to reads and builds a multidimensional array which houses the 3 by 4 matrix
Have this method to call the sumColumn method to produce the column sum.
Have this method to display the sum of each column
2. Write a sumColumn method that returns the sum of all the elements in a specified column in a matrix using the following header:
public static double sumColumn( double[][] m, int columnIndex )
Here’s a sample run:
Enter a 3 by 4 matrix row by row:
1.4 2 3 4 [ENTER]
5.5 6 7 8 [ENTER]
9.5 1 3 1 [ENTER]
Sum of the elements at column 0 is 16.4
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 13.0
Sum of the elements at column 3 is 13.0
Explanation / Answer
#include<iostream>
using namespace std;
class MatrixSum
{
public:/* Declaration of two dimensional array */
int a[3][4];
void insertMatrix()
{
cout << "Enter data for first array:" << endl;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
cin >> a[i][j];
}
}
}
double sumColumn( double[][] a, int columnIndex )
{
int sum=0;
for (int j = 0; j < columnIndex; ++j)
{
for (int i = 0; i < 3; ++i)
{
sum = sum + a[i][j];
}
cout<<"Sum of the"+ j+" column is =" +sum;
sum = 0;
}
}
};
int main()
{
int a[3][4];
MatrixSum msum=MatrixSum();
msum.insertMatrix();
msum.sumColumn(a,4);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.