Write program using C++ code. Requirements e Obtain a file name from the user, w
ID: 3724752 • Letter: W
Question
Write program using C++ code. Requirements e 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 cnun rows> cnum colssExplanation / Answer
#include <iostream>
using namespace std;
int main() {
int x=10;
int y=25;
int z=x+y;
cout<<"Sum of x+y = " << z;
string fileName;
getline(cin, fileName);
file.open(fileName.c_str());
ofstream average;
outputFile.open("averages.txt");
ifstream fp("abc.txt");
if (! fp) {
cout << "Error, file couldn't be opened" << endl;
return 1;
}
int m,n;
cin>>m>>n;
int arr[m][n];
for(int row = 0; row < m; row++) { // stop loops if nothing to read
for(int column = 0; column < n; column++){
fp >> arr[row][column];
if ( ! fp ) {
cout << "Error reading file for element " << row << "," << col << endl;
return 1;
}
}
}
for(int row = 0; row < m; row++) { // stop loops if nothing to read
for(int column = 0; column < n; column++){
sum+=arr[row][column];
sumrow+=arr[row][column];
}
avgrow=sumrow/n;
sumrow=0;
}
avg=sum/(m*n);
outputFile << avg;
outputFile << avgrow;
void flip_columns(T (&arr)[N][M]) {
ofstream reverse;
outputFile.open("reverse.txt");
std::for_each(std::begin(arr), std::end(arr),
[](auto &i) {std::reverse(std::begin(i), std::end(i));});
outputFile<<arr;
}
void flip_rows(T (&arr)[N][M]) {
outputFile.open("flip.txt");
std::for_each(std::begin(arr), std::end(arr),
std::reverse(std::begin(arr), std::end(arr));
outputFile<<arr;
}
int row = 0;
for (int j = 0; j < n; j++) {
stack<int> s;
int i = row, k = j;
while (i < n && k >= 0)
s.push(arr[i++][k--]);
i = row, k = j;
while (i < n && k >= 0) {
arr[i++][k--] = s.top();
s.pop();
}
}
int column = n - 1;
for (int j = 1; j < n; j++) {
stack<int> s;
int i = j, k = column;
while (i < n && k >= 0)
s.push(arr[i++][k--]);
i = j;
k = column;
while (i < n && k >= 0) {
arr[i++][k--] = s.top();
s.pop();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.