Ask 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 : 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

+2
Answers (1)
  1. 4 November, 11:18
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers