Ask Question
29 July, 16:35

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

+1
Answers (1)
  1. 29 July, 20:28
    0
    see explaination

    Explanation:

    #include

    #include

    #include

    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
    {

    for (int j=0; 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
    {

    avg = 0;

    for (int j=0; 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
    {

    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
    {

    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
    {

    for (int j=0; j
    {

    diag[j][i] = mat[i][j];

    }

    }

    for (int i=0; i
    {

    for (int j=0; j
    {

    diagFile << std::fixed << std::setprecision (1) << diag[i][j] << " ";

    }

    diagFile << endl;

    }

    }

    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 : ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers