Question

Write a function Transpose that transposes a matrix T with M rows and N colums. The...

Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14)

Example:
1  2  3
0  -6  7

The transposed matrix TT is

1  0
2  -6
3  7

DEFAULT CODE:

Add to code as needed:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void Transpose(int T[100][100], int TT[100][100], int M, int N)
{
int i;
int j;

for(i = 0; i < TT; i++)

for(j = 0; i < TT; i++)
{
if(M == N-1)
}
return;
}

int main()
{
int T[100][100];
int TT[100][100];
int M, N;
int i = 0;

cin >> M;
cin >> N;

while (i < M*N)
{
cin >> T[i / N][i % N];
i = i + 1;
}

Transpose(T, TT, M, N);

i = 0;

while (i < M*N)
{
cout << TT[i / M][i % M] << " ";

i = i + 1;

if (i % M == 0)
cout << endl;
}


return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void Transpose(int T[100][100], int TT[100][100], int M, int N)
{
int i;
int j;

for(i = 0; i < M; i++)

for(j = 0; i < N; j++)
{
TT[j][i]=T[i][j];
}
return;
}
int main()
{
int T[100][100];
int TT[100][100];
int M, N;
int i = 0;
cin >> M;
cin >> N;
while (i < M*N)
{
cin >> T[i / N][i % N];

i = i + 1;
}

Transpose(T, TT, M, N);
i = 0;


while (i < M*N)
{
cout << TT[i / M][i % M] << " ";
i = i + 1;
if (i % M == 0)
cout << endl;
}
return 0;
}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
Write a function Transpose that transposes a matrix T with M rows and N colums. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning...

    I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning the values in the rows go from low to high). I tried to make the sort function but it just puts all the numbers from lowest to highest, up to down in column format. So please please help me fix it so it sorts correctly, I have bolded the code that needs to be fixed, everything else is fine. The code is below: #include...

  • Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I...

    Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I have also given the main_problem3.cpp and problem3.h to test. problem3.cpp::: #include "problem3.h" // A function to rotate a matrix mat[][MAX] void rotatematrix(int m, int n, int mat[][MAX]) { // your code here    } Makefile::: all : problem3.o    g++ -o mainp3 # your code here problem3.o : problem3.h problem3.cpp    # your code here clean:    rm -f *.o mainp3 main_problem3.cpp::: #include <iostream>...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace...

    Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace std; int main(){    int rows; int cols; int offset; cin >> rows; cin >> cols; cin >> offset;    int** arr = new int*[rows]; for(int i = 0; i < rows; ++i) arr[i] = new int[cols];    for(int i = 0; i < rows; i++) // initialization for(int j = 0; j < cols; j++) arr[i][j] = offset + i;    // printing, your code goes here   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT