216k views
3 votes
Write a program (using functions) starting from directives to compute and display the transpose of a matrix of dimension m x n. [Note: Here, transpose of a matrix means the element at row r and column c in the original is placed at row c and column r of the transpose]. (Programming in C)

User Greg Low
by
5.7k points

1 Answer

0 votes

Answer:

#include <iostream>

#include <cstdlib>

using namespace std;

int m, n;

void transpose(int matrix[]){

int transp[m][n];

for (int i = 0; i < n; i++){

for (int j = 0; j < m; j++){

transp[j][i] = matrix[i][j];

cout<< transp[j][i]<< " ";

}

cout<< "\\";

}

}

int main(){

cout<< "Enter the value for n: ";

cin>> n;

cout>> "Enter the value for m: ";

cin>> m;

int mymatrix[n][m];

for (int i = 0; i < n; i++){

for (int j = 0; j < m; j++){

mymatrix[i][j] = (rand() % 50);

}

}

transpose(mymatrix);

}

Step-by-step explanation:

The C source code defined a void transpose function that accepts a matrix or a two-dimensional array and prints the transpose on the screen. The program gets user input for the row (n) and column (m) length of the arrays. The C standard library function rand() is used to assign random numbers to the array items.

User Tony Ju
by
5.3k points