Subject:
Computer ScienceAuthor:
clayfarleyCreated:
1 year agoAnswer:
srand(static_cast<unsigned int>(time(0)));
const int m = 4, n = 4;
int arr[4][4];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
arr[i][j] = rand() % 100;
cout << "Random matrix: \n";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
cout << arr[i][j] << " ";
cout << endl;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < i; j++)
{
int tmp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = tmp;
}
cout << endl;
cout << "Transponse of the matrix: \n";
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
cout << arr[i][j] << " ";
cout << endl;
Explanation:
Author:
bud96kz
Rate an answer:
5Answer:
#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;
}
Explanation:
#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;
}
Author:
ann51
Rate an answer:
6