CS201P Assignment 2 Solution Fall 22 by getready

 CS201P Assignment 2 Solution Fall 22 by getready




#include<iostream>

using namespace std;

int array [3][3]={{56,27,9},{25,32,18},{88,11,23}};

void display(int array[][3])

{

for (int i=0;i<3;i++)

    {

      for  (int j=0;j<3;j++)

     {    

  cout<<array[i][j]<<" ";

  }

    cout<<endl;

   

}

}

void sortRow(int array[][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(array[i][j]>array[i][k])

{

temp=array[i][j];

array[i][j]=array[i][k];

array[i][k]=temp;

    }

    }

    }

    }

}




void diagonalSum1(int array[3][3])

{

int sum1=0;

for(int i=0;i<3;i++)

{

for (int j=0;j<3;j++)

{

if(i==j)

{

sum1+=array[i][j];

}

}

}

cout<<"Sum of Diagonal Elements For Rowise Sorted Array is:"<<sum1<<endl;

}

void sortCol(int array[][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 (array[i][j]>array[k][j])

{

temp=array[i][j];

array[i][j]=array[k][j];

array[k][j]=temp;

}

}

}

}

}

void diagonalSum2(int array[3][3])

{

int sum2=0;

    for (int i=0;i<3;i++)

{

for (int j=0;j<3;j++)

{

if (j==i)

{

sum2+=array[i][j];

}

    }

}

cout<<"Sum of diagonal Element for Colwise Sorted Array is:"<<sum2<<endl;

}

int main()

{

cout<<"source arrays:"<<endl;

display(array);

cout<<endl;

cout<<"RowWise Sorted Arrays:"<<endl;

sortRow(array);

display(array);

cout<<endl;

diagonalSum1(array);

cout<<"Colwise sorted Arrays:"<<endl;

sortCol(array);

display(array);

cout<<endl;

diagonalSum2(array);

return 0;

}