Why this code dosen't work? How can I improve it? I think function should be void type. The function transposeMatrix() that takes as an argument a 4 × 5 matrix and a 5 × 4 matrix. The function transpose the 4 × 5 matrix and store the results in the 5 × 4 matrix.
void transposeMatrix(int a, int b, int N[a][b], int M[b][a]) // get 2 arrays
{
for (int i = 0; i < a; i++) { // go through arrays
for (int j = 0; j < b; j++)
M[i][c] = N[i][c];
}
}
int main(void)
{
int a = 4, b = 5;
int M[b][a] = // fist array
{
{ 0, 10, 22, 30 },
{ 4, 50, 6, 77 },
{ 80, 9, 1000, 111 },
{ 12, 130, 14, 15 },
{ 16, 17, 102, 103 }
};
int N[a][b] = // second array
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
};
printf("array M\n");
for (int i = 0; i < a; i++) {
for (int c = 0; c < b; i++)
printf("%4i", M[i][c]);
printf("\n");
}
transposeMatrix(a, b, N, M); // call function
printf("array M after transposeMatrix function/n ");
for (int i = 0; i < a; i++) {
for (int c = 0; c < b; i++)
printf("%4i", M[i][c]);
printf("\n");
}
return 0;
}
Everytime we take transpose we use atranspose[i][j]=a[j][i].
Whereas in ur code u are using M[i][c] = N[i][c] which is wrong.
#include<iostream>
using namespace std;
int main()
{
int matrix[5][5],transpose_matrix[5][5];
int i,j,rows,cols;
// Taking Input In Array
cout<<"Enter Number of ROWS :";
cin>>rows;
cout<<"Enter Number Of COLS :";
cin>>cols;
for( i=0;i<rows;i++){
for( j=0;j<cols;j++)
{
cin>>matrix[i][j];
}
}
cout<<"\n Matrix You Entered\n";
for( i=0;i<rows;i++){
for( j=0;j<cols;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
// Calculating Transpose of Matrix
cout<<"\n\n\nTranspose of Entered Matrix\n";
for( i=0;i<rows;i++) {
for( j=0;j<cols;j++)
{
transpose_matrix[j][i]=matrix[i][j];
}
cout<<endl;
}
//Displaying Final Resultant Array
for( i=0;i<cols;i++) {
for( j=0;j<rows;j++)
{
cout<<transpose_matrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.