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

2D Arrays: Transpose of Matrix Write a C++ program that does the following Read

ID: 3688880 • Letter: 2

Question

2D Arrays: Transpose of Matrix

Write a C++ program that does the following

Read 16 integer numbers from an input file “Numbers.txt” (You can manually create this file first and fill it with any sixteen integer values written one below the other; so that reading them through code becomes easy).

Store them in 4x4 two dimensional array (also known as matrix) named Values. Set of 4 numbers should belong to each row as you read the input file.

Display the contents of the two dimensional (2D) array using NESTED loops in a form of square matrix.

Create a function columnStats() that takes the two dimensional array as parameter from main() and computes the Sum and Average of every column in this matrix. The function should also display the above computed values.

Output Format:

Column 1: Sum = Average=

Column 2: Sum = Average=

Column 3: Sum = Average=

Column 4: Sum = Average=

Create a function transpose() that takes Values ( defined above) and transposed_Values ( a parallel two dimensional array whose all elements are initialized to 0) as parameters from main() . The function computes the transpose of Values which is then stored in transposed_Values. It should also display the contents of transposed_Values 2D-array in a form of square matrix.

Do NOT use STL vector in your program.

Example:

Values

1 3 5 7

21 31 11 5

6 0 8 25

-4 -6 30 65

transposed_Values

1 21 6 -4

3 31 0 -6

5 11 8 30

7 5 25 65

Tip: Transpose of a matrix means “All Columns (or Rows) of original matrix become the corresponding Rows (or Columns) of the transposed matrix”.

Explanation / Answer

main.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int ROWS = 4;
const int COLS = 4;

//prototype
void columStats(int [][COLS], int);
void transpose(int [][COLS], int [][COLS]);

int main() {
  
    //create a two dimension array
    int Values[ROWS][COLS];
  
    //open the file
    ifstream inputfile;
  
    inputfile.open("Numbers.txt");
    if(!inputfile){
        cout << "Error opening file! ";
    }
    else{
        //store data into array
        for (int r = 0; r<ROWS; r++) {
            for (int c = 0; c< COLS; c++)
            {
                inputfile >> Values[r][c];
            }
        }
    }
  
    //close file
    inputfile.close();
  
    //print out the 2d array
    cout<<"Values:"<<endl;
    for (int r = 0; r<ROWS; r++) {
        for (int c = 0; c< COLS; c++) {
            cout<<setw(4)<<Values[r][c]<<" ";
        }
        cout<<endl;
    }
  
    //call columnstats function
    columStats(Values,COLS);
  
    //call transpose function
    int transpose_Value[ROWS][COLS];
    transpose(Values,transpose_Value);
  
    return 0;
}

void columStats(int Value[][COLS], int ROWS){
    int colsum = 0;
    int colavg;
    int r, c = 0;
  
    cout<<endl;
    cout<<"Sum and Average for each column:"<<endl;
    for (r = 0; r<COLS; r++) {
        for (c = 0; c<ROWS; c++)
            colsum+=Value[c][r]; //calcuate sum of each column
        colavg = colsum/COLS; //calcuate average of each column
      
        //display sum and average
        cout<<"Column"<<r+1<<": Sum = "<<colsum<<setw(11)<<"Average = "<<colavg;
        colsum = 0;
        cout<<endl;
    }
}

void transpose(int Value[][COLS] ,int transposed_Value[][COLS]){
    cout<<endl;
    cout<<"Transposed_Values:"<<endl;
    for (int r = 0; r<ROWS; r++) {
        for (int c = 0; c<COLS; c++) {
            //transpose Value and store new array into transposed_Value
            transposed_Value[r][c] = Value[c][r];
          
            //print out transposed_Value
            cout<<setw(4)<<transposed_Value[r][c]<<" ";
        }
        cout<<endl;
    }
  
  
}

Numbers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

sample output

Values:                                                                                                                                                     
   1     2     3     4                                                                                                                                      
   5     6     7     8                                                                                                                                      
   9    10    11    12                                                                                                                                      
13    14    15    16                                                                                                                                      
                                                                                                                                                            
Sum and Average for each column:                                                                                                                            
Column1: Sum = 28 Average = 7                                                                                                                               
Column2: Sum = 32 Average = 8                                                                                                                               
Column3: Sum = 36 Average = 9                                                                                                                               
Column4: Sum = 40 Average = 10                                                                                                                              
                                                                                                                                                            
Transposed_Values:                                                                                                                                          
   1     5     9    13                                                                                                                                      
   2     6    10    14                                                                                                                                      
   3     7    11    15                                                                                                                                      
   4     8    12    16                                                                                                                                      

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