Dymanic Matrix Transpose
the program is working well with square matrices but when I input for example 1*3
the program stop working
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <iostream>
using namespace std;
int** addMatrices(int** matrix1, int rows1, int columns1)
{
int ** Result ;
Result = new int*[rows1];
for(int i=0;i<rows1;i++)
{
Result[i]= new int[columns1];
}
for(int i=0;i<rows1;i++)
{
for(int j=0;j<columns1;j++)
{
Result[j][i] =matrix1[i][j];
}
}
return Result;
}
int main()
{
int **Matrix1 ,**transposeMatrix;
int Rows1,Cols1;
cout<<"Number of Rows of Matrix#1 : ";
cin>>Rows1;
cout<<"Number of Columns of Matrix#1 : ";
cin>>Cols1;
//Allocation of 2D Matrices Arrays
Matrix1= new int*[Rows1];
transposeMatrix = new int*[Rows1];
for(int i=0;i<Rows1;i++){
Matrix1[i]= new int[Cols1];
}
for(int i=0;i<Rows1;i++){
transposeMatrix[i]= new int[Cols1];
}
//Filling 2D Matrix#1 Array
cout<<"Matrix #1 : "<<endl;
for(int i=0;i<Rows1;i++)
{
for(int j=0;j<Cols1;j++)
{
cin>>Matrix1[i][j];
}
}
transposeMatrix = addMatrices(Matrix1,Rows1,Cols1);
for(int i=0;i<Rows1;i++)
{
for(int j=0;j<Cols1;j++)
{
cout<<transposeMatrix[i][j]<<" ";
}
cout<<endl;
}
//Deallocation of 2D Array
for(int i=0; i<Rows1;i++)
{
delete []Matrix1[i];
}
delete []Matrix1;
return 0;
}
|
Topic archived. No new replies allowed.