C Homework Help Create a C program as follows 1 Ask the user
C++ Homework Help
Create a C++ program as follows:
1. Ask the user for the name of a file, read the name of the file from standard input. The first row of the file contains the number of rows and columns in the file. The remainder of the file is expected to contain a matrix of numbers in the form of multiple lines of space or tab delimited numbers. Examples are provided below. For this project, no valid input file shall have more then 20 rows or 20 columns.
2. Read the contents of the file and determine if the matrix of numbers exhibits “vertical additive symmetry”. The definition of this term is provided below.
3. If the matrix of numbers in the file exhibits “vertical additive symmetry”, output “vertical additive symmetry” to standard output. Otherwise, output “no vertical additive symmetry” to standard output.
4. Print each row of the matrix in descending order, one row per line of output, with each number separated by a space.
Definition of vertical additive symmetry: A matrix of numbers is defined to exhibit vertical additive symmetry if the sum of the numbers in the columns of the matrix exhibits vertical symmetry.
Solution
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
string str;
int i,j,n,m;
cout<<\"Enter input file : \";
getline(cin,str);
ifstream file;
file.open(str.c_str()); //opening file
if(file.fail())
{
cerr<<\"error in opening file\";
exit(1);
}
file>>n; //read number of rows
file>>m; //read number of columns
int array[n][m]; //declare array size of nXm
for(i=0;i<n;i++)
for(j=0;j<m;j++)
file>>array[i][j]; //reading from file
cout<<\"Contents of file are\"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
cout<<array[i][j]<<\"\\t\"; //printing file data
cout<<endl;
}
int vsum[m]; //array for storing vertical columns sum
for(i=0;i<m;i++)
{
vsum[i]=0;
for(j=0;j<n;j++)
vsum[i]=vsum[i]+array[j][i]; //calculating vertical columns sum
}
cout<<\"Columns sum is\"<<endl;
for(i=0;i<m;i++)
cout<<vsum[i]<<\"\\t\"; //printing vertical columns sum
cout<<endl<<\"Checking symmetricity. . . .\"<<endl;
int symmetry=1; //ariable of symmetricity
for(i=0;i<m/2;i++)
if(vsum[i]!=vsum[m-1-i]) //checking of vertical additive symmetry
symmetry=0;
if(symmetry==1)
cout<<\"Matrix is vertical additive symmetry\"<<endl;
else
cout<<\"Matrix is not vertical additive symmetry\"<<endl;
int temp;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
for(int k=0;k<m;k++)
{
if(array[i][j]>array[i][k]) //sorting
{
temp=array[i][j];
array[i][j]=array[i][k];
array[i][k]=temp;
}
}
}
}
cout<<\"After Sorting of Rows Decending\"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
cout<<array[i][j]<<\"\\t\"; //printing sorted data
cout<<endl;
}
file.close();
return 0;
}
output:--
1)
inputfile.txt is:--
5 5
2 5 6 7 1
8 4 4 6 7
9 4 7 5 7
8 8 5 8 2
5 9 1 4 1
corsponding output is:--
Enter input file : inputfile.txt
Contents of file are
2 5 6 7 1
8 4 4 6 7
9 4 7 5 7
8 8 5 8 2
5 9 1 4 1
Columns sum is
32 30 23 30 18
Checking symmetricity. . . .
Matrix is not vertical additive symmetry
After Sorting of Rows Decending
7 6 5 2 1
8 7 6 4 4
9 7 7 5 4
8 8 8 5 2
9 5 4 1 1
Process exited normally.
Press any key to continue . . .
--------------------------------
2)
inputfile.txt is:--
5 5
2 5 6 7 9
8 4 4 6 7
9 4 7 5 9
4 8 5 8 2
5 9 1 4 1
corsponding output is:--
Enter inpur file : inputfile.txt
Contents of file are
2 5 6 7 9
8 4 4 6 7
9 4 7 5 9
4 8 5 8 2
5 9 1 4 1
Columns sum is
28 30 23 30 28
Checking symmetricity. . . .
Matrix is vertical additive symmetry
After Sorting of Rows Decending
9 7 6 5 2
8 7 6 4 4
9 9 7 5 4
8 8 5 4 2
9 5 4 1 1
Process exited normally.
Press any key to continue . . .


