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
|
#include <sys/time.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
using namespace std;
// declare global variables
// define the size of the matrix
const int size1 = 5;
const int size2 = 5;
//function prototype for the withdrawal of the matrix on the screen
void print(int [][size2]);
int main()
{
//declare and define the initial matrix
int array[size1][size2] = {{1, 2},
{3, 4},
{5, 6}};
/* if (size1|size2 == 0)
{
printf("%s", "error\n ");
return 0;
}*/
/*{{ 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}};*/
int temp;
//display a source matrix
cout << "The original matrix (default 5x5): " << endl << endl;
print(array);
cout << endl;
//transpose the first method
for(int i = 0; i < size1; i++)
{
for(int j = i; j < size2; j++)
{
temp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = temp;
}
}
//display a transposed matrix
cout << "Transposed matrix: " << endl << endl;
print(array);
cout << endl;
// transpose the second method
// declare a new matrix
int transpArray[size1][size2];
for(int i = 0; i < size1; i++)
for(int j = 0; j < size2; j++)
transpArray[j][i] = array[i][j];
// display the transposed matrix
cout << "Transposed matrix: " << endl << endl;
print(transpArray);
cout << endl;
}
//function to output matrix to screen
void print (int array[][size2])
{
for(int i = 0; i < size1; i++)
{
for(int j = 0; j < size2; j++)
{
cout << setw(2) <<
array[i][j] << " ";
}
cout << endl << endl;
}
}
|