Answer to Question #264801 in C++ for nbnnn

Question #264801

Write a program that generate 20 random integers between 10 and 90 and then stores

it in an array. Your goal is to find:

• Mean

• Median

• Mode

For this question, provide switch menu to user in order to select one of the option from

above.



1
Expert's answer
2021-11-12T05:06:28-0500
#include <iostream>
#include <stdlib.h>
#include <time.h>


using namespace std;
void sort(int *arr)
{
 int i,j,k;
 for(i=0; i<19; i++)
 {
 for(j=i+1; j<20; j++)
 {
 if(arr[i]>arr[j])
 {
 k=arr[i];
 arr[i]=arr[j] ;
 arr[j]=k;
 }
 }
 }
};


void Arr(int *arr)
{
 cout << "Array: ";
 for(int i=0;i<20; i++)
 {
 cout << arr[i];
 cout << ' ';
 }
 cout << endl;
};


void Mean(int *arr)
{
 int summ = 0;
 for(int i=0; i<19; i++) summ+=arr[i];
 cout << "Mean = " << summ/20 << endl;
};


void Median(int *arr)
{
 cout << "Median = " << (arr[9]+arr[10])/2 << endl;
};




void Mode(int *arr)
{
 int num=-1; 
 int maxNumCount=0;
 int numCount=0;
 int i,j;
 for(i=0; i<19; i++)
 {
 numCount=0;
 for(j=i+1; j<20; j++)
 {
 if(arr[i]==arr[j])
 {
 numCount++;
 }
 }
 if(numCount>maxNumCount)
 {
 maxNumCount=numCount;
 num=arr[i];
 }
 }


 if(num > -1)
 {
 cout << "Mode = ";
 for(i=0; i<19; i++)
 {
 numCount=0;
 for(j=i+1; j<20; j++)
 {
 if(arr[i]==arr[j])
 {
 numCount++;
 }
 }




 if(numCount==maxNumCount)
 {
 cout << arr[i] << ' ';
 }
 }
 cout << endl;
 }
 else cout << "No modes" << endl;
};


int main()
{
 int arr[20];
 int c;
 srand(time(0)); 
 cout << "Original array: ";
 for(int i=0;i<20; i++)
 {
     arr[i]=(int)rand()%81+10;
     cout << arr[i]<< ' ';
 }
 cout << endl;




 sort(arr);
 cout << endl;
 cout << "1. Mean" <<endl;
 cout << "2. Median" <<endl;
 cout << "3. Mode" <<endl;
 cout<<"\nEnter your choice: ";
 cin>>c;


 switch (c)
 {
 case 1: Mean(arr); break;
 case 2: Median(arr); break;
 case 3: Mode(arr); break;
 }


 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