Program name: Matrix Transpose (Please use on C++ programming methods)thanks Wri
ID: 3764315 • Letter: P
Question
Program name: Matrix Transpose (Please use on C++ programming methods)thanks
Write a program to read a matrix from a data file into a program array, divide it by a constant k, and transpose the resultant matrix. There must be two different functions used in this program. The division process will be accomplished in the first, and transposition in the second. A test constant k=10 will be used for verification, but k can be any value, including fractional, and will be read in from the keyboard.
Main must read in a data file and place the contents in a matrix defined in the program having size 10x10. The data file will then be closed. Contents of the matrix will then be written to the screen.
The program must be capable of transposing a matrix with variable row and column counts up to 10x10.
Necessary parameters will be passed to the functions from main.
An output data file must be written for the resultant transposed matrix. The transposed matrix must be written to the screen.
A verification input data file is given below. This is only a test matrix, and other smaller matrices are not to be extracted from it. Each matrix to be transposed will be in a separate data file.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
The program should handle each file as a separate item. For example, if the input were to be a 3x4 matrix, that would be a different input file from this one and would therefore need a different name.
All input and output data files must accompany the .cpp file for program assignment submission.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int m[10][10], trans[10][10], r, c, k, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
/* used storing the elements in the array */
cout << endl << "Enter elements of matrix: " << endl;
for(k=0; k<r; ++k)
for(j=0; j<c; ++j)
{
cout << "Enter elements m" << k+1 << j+1 << ": ";
cin >> m[k][j];
}
/* used to diaplya the array */
cout << endl << "Entered Matrix: " << endl;
for(k=0; k<r; ++k)
for(j=0; j<c; ++j)
{
cout << " " << m[k][j];
if(j==c-1)
cout << endl << endl;
}
/* transpose of matrix a[][] and storing it in array trans[][]. */
for(k=0; k<r; ++k)
for(j=0; j<c; ++j)
{
trans[j][k]=m[k][j];
}
/* Displaying the transpose */
cout << endl << "Transpose of Matrix: " << endl;
for(k=0; k<c; ++k)
for(j=0; j<r; ++j)
{
cout << " " << trans[k][j];
if(j==r-1)
cout << endl << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.