This program must be written in C, not C++. Create a C program as follows: 1. As
ID: 3797883 • Letter: T
Question
This program must be written in C, not C++.
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.
Explanation / Answer
/*C++ program that reads a input.txt file that contains
a matrix of rows and columns and checks if matrix
is vertical symmetry or not.
*/
//symmetry.cpp
#include <iostream>
#include<iomanip>
#include <fstream>
using namespace std;
int main()
{
int N;
int M;
int i,j;
bool verticalSymmetric=true;
ifstream fin("input.txt");
//checking if file exists
if(!fin)
{
cout<<"Input file doesnot exist.."<<endl;
system("pause");
return -1;
}
fin>>N>>M;
//creating dynamic matrix of size N rows and M cols
int **m=new int*[N];
for(int i = 0; i < N; ++i)
m[i] = new int[M];
for(i=0;i<N;i++)
for(j=0;j<M;j++)
fin>>m[i][j];
// Initializing as both horizontal and vertical
// symmetric.
bool vertical = true;
//check first colum with last colum and second and last second and so .
for (int i=0, k=M-1; i<M/2; i++, k--)
{
// Checking each cell of a row.
for (int j = 0; j < N; j++)
{
// check if correponding values of column values are equal or not
if (m[j][i] != m[j][k])
{
verticalSymmetric = false;
break;
}
}
}
//print matrix to console
cout<<"Matrix "<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
cout<<setw(3)<<m[i][j];
cout<<endl;
}
cout<<endl;
if (verticalSymmetric )
cout << "Matrix,m is vetical symmetric matrix ";
else
cout << "Matrix,m is not vertical symmetric ";
system("pause");
return 0;
}
-------------------------------------------------------------
input.txt
3 3
1 4 1
2 5 2
3 6 3
-------------------------------------
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.