In: Computer Science
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks]
Explain the time complexity of this function inside of the function code as a comment. [3 marks]
in C++
#include <iostream>
using namespace std;
int n=0;
int m=0;
void transpose(int *A, int *B) 
{ 
    int i, j; 
    for (i = 0; i < n; i++) //It will run n times
        for (j = 0; j < n; j++) // it will also run n times
            *((B+i*n)+j)= *((A+j*n)+i); 
    //time complexity Explanation
    /*Since transpose() function has 2 for loops and each loop executes n time, thus it will n*n time in total
    Thus Time complexity of transpose is T(n) = O(n*n) */
} 
int main() {
  cout << "Enter size for Matrix: ";
  cin >> n;
  cout<<"Enter matrix in n×n form:"<<endl;
  int A[n][n];
  int B[n][n];
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>A[i][j];
    }
  }
  transpose((int*)A, (int *)B);
  cout<<"Matrix after transpose is:"<<endl;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout<<B[i][j]<<" ";
    }
    cout<<endl;
  }
}
Sample output is as following:
