And please explain it clearly! write a program that calculates the sample mean o
ID: 3782087 • Letter: A
Question
And please explain it clearly! write a program that calculates the sample mean of a collection of numbers that the user enters. Your program should prompt the user for the amount of numbers, and then all of the numbers. The calculation should then be done according to the formula: sample mean, x L where N is the size of the sample. Test case: enter: 1, 2, 3, 4, 5 F 3.0 Test case: enter: 4, 6, 8, 10, 12. 8.0 Next, your program should calculate the sample variance of the collection of numbers sample variance, o where N is the size of the sample. N-1 Test case: enter: 3.0: 1, 2, 3, 4, 5 o 2.5 Test case: enter T 8.0; 4, 6, 8, 10, 12. o 10.0Explanation / Answer
/* Program is given below variable amt is for amount of numbers, array num[] for storing numbers, sum and temp are temporary variable */
#include <iostream>
using namespace std;
int main()
{ int amt,i,num[50];
int sum=0;
float mean,temp=0,variance;
//prompt user to enter total amount of numbers
cout << "Enter total numbers:";
cin>>amt;
//accept numbers from user and store it in num[]
cout<<"Enter Numbers:";
for(i=0;i<amt;i++)
cin>>num[i];
//calculate Mean
for(i=0;i<amt;i++)
sum+=num[i]; //adding total numbers in array
mean = (sum/amt); //calculate mean by dividing sum by amt
cout<<"Mean is :"<<mean<<endl;
// calculate Variance
for(i=0;i<amt;i++)
{ //calulate numerator part of variance formula and store in temp
temp += (num[i] - mean) * (num[i] - mean);
}
variance = temp/(amt-1); //Variance
cout<<"Variance is :"<<variance<<endl;
return 0;
}
Output:
Test case 1:
sh-4.2$ main
Enter total numbers:5
Enter Numbers:1 2 3 4 5
Mean is :3
Variance is :2.5
Test case 2:
Enter total numbers:5
Enter Numbers:4 6 8 10 12
Mean is :8
Variance is :10