We have a 4 x 4 two dimensional array. Fill it with random values from 1 to 100. Write a code to take transpose of the matrix. The following array is shown as an example:
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;
void show(int matrix[4][4]){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++)
cout<<left<<setw(4)<<matrix[i][j];
cout<<endl;
}
}
void swap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;
}
int main(){
int matrix[4][4];
srand(time(NULL));
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
matrix[i][j] = rand() % 101;
cout<<"Filled with random numbers:\n";
show(matrix);
for(int i = 0; i < 4; i++)
for(int j = i + 1; j < 4; j++)
swap(&matrix[i][j], &matrix[j][i]);
cout<<"\nTransposed:\n";
show(matrix);
return 0;
}
Comments
Leave a comment