Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

WRITE IN C++ Instructions In this assignment, you will be designing a program th

ID: 3742874 • Letter: W

Question

WRITE IN C++

Instructions In this assignment, you will be designing a program that computes the mean, median, and mode for a set of data. The definitions for each are as follows: 1. The mean is the average of a set of data. For a collection of data called "C", and items in data C being labeled Co, C1, C2, C3, 2. The median is the middle value in a set of data. For a collection of data called "C", and items in data "C" being labeled Co, C1, C2, C3 ...Cn , and the items in Collection "C" are in order, the is Cn/2 3. The mode of a set of data is the value that occurs most often in a set of data. It is computed by counting the number of occurrences of each unique element. For this assignment, use a hard-coded set of test values and print the statistics inside the function. The functions should accept the test data as well as the size of the test data. You may need to reference algorithms online to use to compute the three statistics. If you do, be sure to cite any resources you use

Explanation / Answer

#include<iostream>
#include<algorithm> //for sort function
using namespace std;
void mean(int a[],int n) //function to calculate mean
{
int sum=0;
float avg;
for(int i=0;i<n;i++)
{
sum=sum+a[i]; //adding the elements one by one to sum
}
avg=(float)sum/n; //mean=sum/n
cout<<"Mean is: "<< avg<<endl;
}
void median(int a[],int n) //function for calculating median
{
sort(a,a+n); //sorting the array
cout<<"Median is: "<<a[n/2]<<endl; //As the array is sorted, so median is the n/2th element
}
void mode(int a[],int n) //function for calculating mode
{
int number = a[0]; //For storing the current number in loop
int m = a[0], cnt=1; //m is for storing mode and cnt for the no of occurrence of a number
int count = 1; // count is for storing no of occurrences of mode

for (int i=1; i<n; i++)
{
if (a[i] == number) //The no is same as stored in number
{
cnt++; //When number is same we keep on incrementing cnt
}
else ////The no is not same as stored in number
{
if (cnt > count)
{
count= cnt;
m = number;
}
cnt = 1; //setting cnt to 1
number = a[i]; //setting number to a[i]
}
}
cout<<"Mode is: "<< m <<endl;
}
int main()
{
int a[10]={8,7,9,4,1,4,8,8,3,2} //a is used to store the numbers
int n=10; //n is the no of elements
mean(a,n); //Displaying mean
median(a,n); //Displaying median
mode(a,n); //Displaying mode
return 0;
}