Create a C++ program as follows: 1. Ask the user for the name of a file, reading
ID: 3882258 • Letter: C
Question
Create a C++ program as follows:
1. Ask the user for the name of a file, reading the name of the file from standard input. The first two values in the file contains the number of rows and columns in a matrix. The remainder of the file is expected to contain the numbers in a matrix. Simple examples are provided below. For this project, no valid input file shall have more than 20 rows or 20 columns.
2. Read the contents of the file and determine if the matrix of numbers exhibits “horizontal additive symmetry”. The definition of this term is provided below.
3. If the matrix of numbers in the file exhibits “horizontal additive symmetry”, output “horizontal additive symmetry” to standard output. Otherwise, output “no horizontal additive symmetry” to standard output.
4. Print each row of the matrix, with the values of that row in descending order, one row per line of output. Format the output in an organized table using right-justified columns, as seen below.
5. Ask the user if they wish to process another file. If so, go back to step #1, above. Otherwise, terminate the program. Your program should use “defensive programming” techniques. This means your program should check input provided by the user for errors and deal with unexpected values without crashing. Ideally, the program should recover from any errors and continue processing.
Sample Run #1
3 2
56 12 98
25 34.5
45
Definition of horizontal additive symmetry:
A matrix of numbers is defined to exhibit horizontal additive symmetry if the sum of the numbers in the rows of the matrix exhibits horizontal symmetry. For this input file, the sums of the rows are:
68
123
79.5
Because this list is not symmetrical, this input file does not exhibit horizontal additive symmetry. (Remember, in a list with an odd number of values, the middle value is considered symmetric with itself.)
Here is the sample output of the program run with this input file:
Sample Run #2
4 3
1.4 4.6 1
1.6 6.65 1
7.8 1.45 0
7 -2 2
The rows of this matrix yield the following sums:
7 9.25 9.25 7
This list of sums exhibits symmetry thus the matrix exhibits horizontal additive symmetry.
Here is the sample output of the program run with this input file:
Explanation / Answer
Hello There,
PFB code for requested functionality with comments specifying the functionality. Output of the program for test run is also provided.
Code:
-------------------------
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main(int argc, char const *argv[])
{
char choice = 'y';
while(choice == 'y' || choice == 'Y'){
string filename;
cout << "Enter the filename :";
cin >> filename;
ifstream inFile;
inFile.open(filename.c_str());
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
// reading the matrix from the file
int rows, columns;
inFile >> rows;
inFile >> columns;
float matrix[rows][columns];
float sumOfRows[rows];
for(int i=0; i<rows; i++){
sumOfRows[i] = 0;
for(int j=0; j<columns; j++){
inFile >> matrix[i][j];
sumOfRows[i] += matrix[i][j];
}
}
inFile.close();
//checking horizontal additive symmetry
int start = 0;
int end = rows-1;
bool symmetry = true;
while(start < end){
if(sumOfRows[start] == sumOfRows[end]){
start++;
end--;
}else{
symmetry = false;
break;
}
}
if(symmetry){
cout << "horizontal additive symmetry" << endl;
}else{
cout << "no horizontal additive symmetry" << endl;
}
// sorting the values in each row and printing the matrix
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
for(int k=j+1; k<columns; k++){
if(matrix[i][j] < matrix[i][k]){
float temp = matrix[i][j];
matrix[i][j] = matrix[i][k];
matrix[i][k] = temp;
}
}
}
for(int j=0; j<columns; j++){
cout << right << setw(5) << matrix[i][j] << " ";
}
cout << endl;
}
// check for another execution of the program
cout << "Would you like to process another file? Enter Y or y to process another file:";
cin >> choice;
}
return 0;
}
-----------------------------
Text Files
----------------------
abc.txt
-----------------
3 2
56 12 98
25 34.5
45
-----------------------
def.txt
----------------------
4 3
1.4 4.6 1
1.6 6.65 1
7.8 1.45 0
7 -2 2
----------------
Test run:
-------------------
$ ./a.out
Enter the filename :abc.txt
no horizontal additive symmetry
56 12
98 25
45 34.5
Would you like to process another file? Enter Y or y to process another file:y
Enter the filename :def.txt
horizontal additive symmetry
4.6 1.4 1
6.65 1.6 1
7.8 1.45 0
7 2 -2
Would you like to process another file? Enter Y or y to process another file:n
$