cs201 assignment 2 solution fall 2019

cs201 assignment 2 solution fall 2019

Soloution file

// write your student id here

#include<iostream>
#include<time.h>
#include<stdlib.h>
void poulateArray(int [][10]);
void showElements(int [][10]);
void showLargestElement(int [][10]);
void transposeArray(int [][10]);
using namespace std;



int main()
{
int option,arr[10][10]= {0};
bool arrEmpty=true;
while(1)
{
cout<<“1.Press 1 to populate a two-dimensional array with integers from 1 to 100.\n”
<<“2.Press 2 to display the array elements.\n”
<<“3.Press 3 to display the largest element present in the array along with its row and column index.\n”
<<“4.Press 4 to find and show the transpose of the array.\n\n”
<<“Please select a option, use numbers from 1 to 5: “;
cin>>option;
cout<<“\n——————————————————–\n\n”;
switch(option)
{
case 1:
poulateArray(arr);
arrEmpty=false;
break;
case 2:
if(arrEmpty==true)
{
cout<<“Sorry the array is empty, first populate it by pressing 1 to perform this task\n\n\n\n”;
}
else
{
showElements(arr);
}
break;
case 3:
if(arrEmpty==true)
{
cout<<“Sorry the array is empty, first populate it by pressing 1 to perform this task\n\n\n\n”;
}
else

 



{
showLargestElement(arr);
}
break;
case 4:
if(arrEmpty==true)
{
cout<<“Sorry the array is empty, first populate it by pressing 1 to perform this task\n\n\n\n”;
}
else
{
transposeArray(arr);
}
break;
case 5:
exit(0);
break;
default:
cout<<“You entered wrong option.\n\n”;
}
}



return 0;
}
void poulateArray(int ar[][10])
{
int num;
srand(time(0));
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
num=1+rand()%100;
ar[i][j]=num;
}
}
cout<<“Array has been populated successfully!\n\n”;
}
void showElements(int ar[][10])
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<ar[i][j]<<“\t”;
}
cout<<endl;
}
}
void showLargestElement(int ar[][10])
{
int largest=0,i,j,row,colum;
for( i=0; i<10; i++)
{
for( j=0; j<10; j++)
{
if(ar[i][j]>largest)
{
largest=ar[i][j];
row=i+1;
colum=j+1;
}
}
}
cout<<“Largest element is “<<largest<<” which is at row “<<row<<” and colum “<<colum<<“\n\n”;
}
void transposeArray(int ar[][10])
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
cout<<ar[j][i]<<“\t”;
}
cout<<“\n”;
}
}

 

Note: This is idea solution. Before submitting assignment please make some necessary changes by your self.

 

 

 

 

 

 

 

 

 

 

 

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: