C++ please. Assignment 1: An Introduction to C++ Using Statistics Data Structure
ID: 3742790 • Letter: C
Question
C++ please.
Assignment 1: An Introduction to C++ Using Statistics Data Structures and Analysis of Algorithms, ccm290 Objectives To strengthen student's knowledge of C++ programming .To give student experience in writing Data Structures for data types Background Statistic formulas are in important mathematical tool to measure occurrences of events, population growth, rates of change, among several other applications. In modern times, computers have made it possible to automate and script the calculation of stats, making result finding faster and more accurate. 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: 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, Cl, C2, C3, Ca 1. cn , the mean is (Co + C1 + C2 + C3 + The median is the middle value in a set of data. and items in data "C" being labeled Co, C, C2, Ca, C..Cn, and the items in Collection "C" are in order, the is Cn/2 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. 2. For a collection of data called "C", 3. 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 useExplanation / Answer
#include<iostream>
#include<algorithm> //for sort function
using namespace std;
float 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
return avg;
}
int median(int a[],int n) //function for calculating median
{
return a[n/2]; //As the array is sorted, so median is the n/2th element
}
int mode(int a[],int n) //function for calculating mode
{
int number = a[0]; //For comparing with consecutive numbers
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]
}
}
return m;
}
int main()
{
int a[100],n; //n is the no of elements and a is used to store those
cout<<"Enter the no of elements: ";
cin>>n;
cout<<"Enter the elements one by one: ";
for(int i=0;i<n;i++) //reading all the elements
cin>>a[i];
sort(a,a+n); //sorting the array
cout<<"Mean is: "<<mean(a,n)<<endl; //Displaying mean
cout<<"Median is: "<<median(a,n)<<endl; //Displaying median
cout<<"Mode is: "<<mode(a,n)<<endl; //Displaying mode
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.