Answer:
see explaination
Explanation:
#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;
}