#include<iostream.h>
#include<stdlib.h>
#include<time.h>
intmain() {
srand(time(0));
int m = 3;
int n = 3;
int** matrix = (int**) calloc(sizeof(int),m);
for ( int i = 0; i < m; i++ ) {
matrix[i] = (int*) calloc(sizeof(int),n);
}
// Fill random values in the matrix
for (i = 0; i < m; i++ ) {
for ( int j = 0; j < n; j++ ) {
matrix[i][j] = rand() % 100;
}
}
// Print the matrix
for (i = 0; i < m; i++ ) {
for ( int j = 0; j < n; j++ ) {
cout << matrix[i][j] <<" ";
}
cout << endl;
}
cout << endl;
// Transpose the matrix
for (i = 0; i < m; i++ ) {
for ( int j = i + 1; j < n; j++ ) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
// Swap the columns
for (i = 0; i < m; i++ ) {
for ( int j = 0; j < n/2; j++ ) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = tmp;
}
}
// Print the rotated matrix
for (i = 0; i < m; i++ ) {
for ( int j = 0; j < n; j++ ) {
cout << matrix[i][j] <<" ";
}
cout << endl;
}
}
Comments
Leave a comment