Hi--
I am trying to figure out how to get the coding for the transpose of a 2-D array to properly work. So far I can successfully generate the array and to print it out but whenever I try to add the part where it will transpose the array, the program simply crashes. I believe that I have something wrong with my indexing in the transpose function part (void trans) but am not sure. I'd appreciate any help. Thanks. Here is the code:
Code:
#include <iostream>
#include <ctime> // For time() //
#include <cstdlib> // For srand() and rand() //
#include <iomanip>
#define ROW 5
#define COL 10
using namespace std;
using std::setw;
void dis(int x[][COL],int,int);
void trans(int x[][COL],int,int);
int main()
{
int x[ROW][COL];
int rnum;
int t[ROW];
cout << "The values of the original array are:\n\n";
srand(time(0)); // Initialize random number generator.
rnum = (rand() % 100) + 1;
for(int r=0; r<ROW; r++) //row
{
for(int c=0; c<COL; c++)
x[r][c] = (rand()%100) + 1;
}
dis(x,ROW,COL);
trans(x,ROW,COL);
cout << "\nThe values of the modified array are:\n\n";
dis(x,ROW,COL);
system("pause");
return 0; // indicates successful termination
} // end main
void dis(int x[5][10],int r,int c)
{
for(int r=0; r<ROW; r++) //row
{
for(int c=0; c<COL; c++) //column
{
cout << setw(5) << x[r][c] << ' '; //display Array
}
cout << "\n" << endl;
}
}
void trans(int mat[][10],int r ,int c)
{
int temp;
int x[r][c];
for (int r = 0; r < ROW; r++)
for (int c = 0; c < COL; c++)
{ temp = x[r][c];
x[r][c] = x[c][r];
x[c][r] = temp;
}
}