write a c++ program. 1. When your program starts you will read in the dimensions
ID: 3808477 • Letter: W
Question
write a c++ program.
1. When your program starts you will read in the dimensions of the matrix followed by N*M values. For example, after compiling and starting your program you can enter the values as follows: g++ lab9assignment.cpp ./a.out 34 1 2 3 4 5 6789 1011 12 The above code represents the matrix 3 2. Print out a menu that allows the user to select the following Print the matrix Sum all of the elements Show the upper half Show middle row Show middle column 3. For each of the menu options above implement a corresponding function: Print the matrix: print the matrix as shown in the example above Sum all of the elements: print the resultof the sum of all elements Show the upper half print out an error message if the matrix is not square. If it is a square matrix, print the upper half, e.g. for the following matrix. 8 9 0 7 3 1 0 1 4 the result would be Page 1 of 2Explanation / Answer
#include <iostream>
using namespace std;
int m,n;
/*
void show_middlecolumn(int *a[])
{
//printing array..
cout<<"Printing middlecolumn of matrix :"<<endl;
if(m==n)
for(int i = 0; i < m; i++)
{
for(int j=(int)n/2;j<=n/2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
else
{
cout<<"Given is not square matrix ";
}
}
void show_middlerow(int a[][100])
{
//printing array..
cout<<"Printing middlerow of matrix :"<<endl;
if(m==n)
for(int i = m/2; i <= m/2; i++)
{
for(int j=i;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
else
{
cout<<"Given is not square matrix ";
}
}
void show_upper(int a[][100])
{
//printing array..
cout<<"Printing upper half of matrix :"<<endl;
if(m==n)
for(int i = 0; i < m; i++)
{
for(int j=0;j<i;j++)cout<<" ";
for(int j=i;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
else
{
cout<<"Given is not square matrix ";
}
}
void show(int a[][100])
{
//printing array..
cout<<"Sum of all elements in matrix :"<<endl;
int sum =0;
for(int i = 0; i < m; i++)
{
for(int j=0;j<n;j++)
{
sum=sum+a[i][j];
}
}
cout<<sum<<" ";
}
///////
/*
void print_matrix(int **a)
{
//printing array..
cout<<"Printing matrix :"<<endl;
for(int i = 0; i < m; i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
}
*/
int convert(char* c)
{
int i;
int s=0;
for(i=0;c[i]!='';i++)
s=s*10+((int)c[i]-48);
return s;
}
int main(int argc, char* argv[]) {
//cout << "argc = " << argc << endl;
///1)
int mm=convert(argv[1]),nn=convert(argv[2]);
m=mm;n=mm;
int a[m][n];//declaring matrix with size given in command line arguments
int k=3;
cout<<"Printing matrix :"<<endl;
for(int i = 0; i < m; i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=convert(argv[k]);//assigning values..
k++;
cout<<a[i][j]<<" ";
}
cout<<" ";
}
//2)
int cc=0;
cout<<" 1:Print Matrix 2:Sum of all the elements 3:Show the upper half 4:Show middle row 5:Show middle column Enter ur choice :";
cin>>cc;
if(cc==1)
{
//printing array..
cout<<"Printing matrix :"<<endl;
for(int i = 0; i < m; i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
}
else if(cc==2)
{
cout<<"Sum of all elements in matrix :"<<endl;
int sum =0;
for(int i = 0; i < m; i++)
{
for(int j=0;j<n;j++)
{
sum=sum+a[i][j];
}
}
cout<<sum<<" ";
}
else if(cc==3)
{
cout<<"Printing upper half of matrix :"<<endl;
if(m==n)
for(int i = 0; i < m; i++)
{
for(int j=0;j<i;j++)cout<<" ";
for(int j=i;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
else
{
cout<<"Given is not square matrix ";
}
}
else if(cc==4)
{
cout<<"Printing middlerow of matrix :"<<endl;
if(m==n)
for(int i = m/2; i <= m/2; i++)
{
for(int j=i;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
else
{
cout<<"Given is not square matrix ";
}
}
else if (cc==5)
{
cout<<"Printing middlecolumn of matrix :"<<endl;
if(m==n)
for(int i = 0; i < m; i++)
{
for(int j=(int)n/2;j<=n/2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
else
{
cout<<"Given is not square matrix ";
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.