Cs201p Assignment no 2 Solution Fall 22
Code:
#include<iostream>
using namespace std;
int arr[3] [3] ={{56, 27, 9},{25, 32, 18},{88, 11, 23}};
void display(int arr[][3])
{
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
cout<<arr[i][j]<<" ";
cout<<endl;
}
}
void sortRow(int arr[][3])
{
int temp;
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
for (int k=j+1;k<3;k++)
{
if (arr[i][j]>arr[i][k])
{
temp=arr[i][j];
arr[i][j]=arr[i][k];
arr[i][k]=temp;
}
}
}
}
}
void diagonalSum1(int arr[3][3])
{
int sum1=0;
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
if (i==j)
{
sum1+=arr[i][j];
}
}
}
cout<<"Sum of diagonal Elements for RowWise Sorted Array is: "<<sum1<<endl<<endl;
}
void sortCol(int arr[][3])
{
int temp;
for (int j=0;j<3;j++)
{
for (int i=0;i<3;i++)
{
for (int k=i+1;k<3;k++)
{
if (arr[i][j]>arr[k][j])
{
temp=arr[i][j];
arr[i][j]=arr[k][j];
arr[k][j]=temp;
}
}
}
}
}
void diagonalSum2(int arr[3][3])
{
int sum2=0;
for(int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
if(j==i)
{
sum2+=arr[i][j];
}
}
}
cout<<"Sum of diagonal Elements for ColWise Sorted Array is: "<<sum2<<endl;
}
int main()
{
cout<<" Solution By GetReady "<<endl;
cout << " To get solution visit getready "<<endl;
cout<<"-----------------------------------------------------------------------------------------------------------------------
-"; cout<<"RowWise Sorted Arrays: "<<endl;
sortRow(arr);
display(arr);
cout<<endl;
diagonalSum1(arr);
cout<<"ColWise Sorted Array: "<<endl;
sortCol(arr);
display(arr);
cout<<endl;
diagonalSum2(arr);
return 0;
}