Answer to Question #190589 in C++ for M Kamran

Question #190589

 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:




1
Expert's answer
2021-05-07T17:26:06-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS