Search This Blog

Wednesday, 30 May 2018

Introduction to Programming (CS201)-Lab Exercise No.4 - WEEK 6 (Transpose of the Matrix

#include <iostream>

using namespace std; //Allows computer to output data

const int arraySize=3;


void readMatrix(int arr[][arraySize]);
void DisplayMatrix(int a[][arraySize]);
void TransposeMatrix(int a[][arraySize]);

main() //main function begins
{
int a[arraySize][arraySize];

readMatrix(a);


cout << "The Matrix is: \n\n";
DisplayMatrix(a);

TransposeMatrix(a);

//Display the transposed matrix
cout << "\n\n" << "The transposed matrix is: " << '\n';
DisplayMatrix(a); 


}//end of main function

void readMatrix(int arr[][arraySize])
{
int row, col;
for (row = 0; row < arraySize; row ++)
{
for(col=0; col < arraySize; col ++)
{
cout << "\n" << "Enter " << row << ", " << col << " element: ";
cin >> arr[row][col]; 
}
cout << '\n';
}
}

void DisplayMatrix(int a[][arraySize])
{
int row, col;
for (row = 0; row < arraySize; row ++)
{
for(col = 0; col < arraySize; col ++)
{
cout << a[row][col] << '\t';
}
cout << '\n';



void TransposeMatrix(int a[][arraySize])
{
int row, col;
int temp;

for (row=0;row<arraySize;row++)
{
for (col=row;col<arraySize;col++)
{
temp=a[row][col];
a[row][col]=a[col][row];
a[col][row]=temp;
}
}

}

No comments:

Post a Comment