Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Requirements: Obtain a file name from the user, which will contain data pertaini

ID: 3728501 • Letter: R

Question

Requirements:

Obtain a file name from the user, which will contain data pertaining to a 2D array

Create a file for each of the following:

averages.txt : contains the overall average of the entire array, then the average of each row

reverse.txt : contains the original values but each row is reversed

flipped.txt : contains the original values but is flipped top to bottom (first row is now the last row etc.)

If the dimensions of array are symmetric (NxN), create a diagonal.txt: contains the array mirrored on the diagonal

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Example files

The input file will be formatted in the following way:

Sample file called "input.txt"

Output to averages.txt

Output to reverse.txt

Output to flipped.txt

Since it's symmetric we also get diagonal.txt

----------------------------------------------

I need it in C++

Explanation / Answer

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filename, file1, file2, file3, file4;
cout << "Enter the filename : ";
getline(cin, filename);
int row, col;
ifstream ifile;
ifile.open(filename.c_str());
if(!ifile)
{
cout << "File does not exist." << endl;
}
else
{
ifile >> row >> col;
float mat[row][col], diag[row][col];
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
ifile >> mat[i][j];
}
cout << "Enter the filename to save averages : ";
getline(cin, file1);
ofstream avgFile(file1.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
float t_avg = 0, avg = 0;
for(int i=0; i<row; i++)
{
avg = 0;
for(int j=0; j<col; j++)
{
avg += mat[i][j];
}
t_avg += avg;
avg = avg/col;
avgFile << std::fixed << std::setprecision(1) << "Row " << i+1 << " average: " << avg << endl;
}
t_avg = t_avg / (row*col);
avgFile << std::fixed << std::setprecision(1) << "Total average: " << t_avg << endl;
cout << "Enter the filename to store reverse matrix : ";
getline(cin, file2);
ofstream revFile(file2.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
for(int i=0; i<row; i++)
{
for(int j=col-1; j>=0; j--)
{
revFile << std::fixed << std::setprecision(1) << mat[i][j] << " ";
}
revFile << endl;
}
cout << "Enter the filename to store flipped matrix : ";
getline(cin, file3);
ofstream flipFile(file3.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
for(int i=row-1; i>=0; i--)
{
for(int j=0; j<col; j++)
{
flipFile << std::fixed << std::setprecision(1) << mat[i][j] << " ";
}
flipFile << endl;
}
cout << "Enter the filename to store diagonal matrix : ";
getline(cin, file4);
ofstream diagFile(file4.c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
diag[j][i] = mat[i][j];
}
}
for(int i=0; i<col; i++)
{
for(int j=0; j<row; j++)
{
diagFile << std::fixed << std::setprecision(1) << diag[i][j] << " ";
}
diagFile << endl;
}
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote