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

this should be done in C++ Overview Practice array and functions Objective Given

ID: 3728187 • Letter: T

Question

this should be done in C++

Overview Practice array and functions Objective Given a list of N integers, find its basic statistics (as in previous exercise), root-mean-square and standard deviation. Your program will first ask for N, the number of integers in the list, which the user will input. Then the user will input N more numbers. You need to use an integer array to store all the numbers. You also need to use separate functions for each calculation. Note: Please consult wikipedia for root-mean-square and standard deviation. nput Format: Your program will loop on user inputs and print out the statistics until user stops by entering O integers. Output Format: Try your imagination More: Have you tried different way of getting the input numbers? What difference does it make if the numbers are floating point? tDo you use one function for each calculation, min), max), avg), rms), sd)? Can you do one function which does more than one calculations, such as statistics(avg, mean, sd)? >For those who are bored of array, what if all the numbers are stored in a file? Do you still need "count"? How about replace cin with file input?

Explanation / Answer


#include <cstdlib>
#include <iostream>
#include<math.h>
using namespace std;

/*
* for calculating root mean squared and standard deviation
*/

//prototypes for calculating root mean squared and standard deviation
double rms(int[],int);
double stddev(int[],int);

int main(int argc, char** argv) {
int n;//number of numbers
//prompt the user for entering n
cout<<" Enter the value for n :";
cin>>n;
  
int i; //counter variable
int total = 0; //stores the square root total
int mean,sum =0;//stores the mean of the numbers
int sd=0;
int array[n];//stores the array of numbers
  
while(n!=0){//performs the loop until the value is 0
//prompt the user to enter the values of n numbers
cout<<" Enter the values :";
for(i=0; i<n; i++){
cin>>array[i];
}
//display RMS by calling the function
cout<<" The Square Root of Mean :"<<rms(array,n)<<endl;

//display sd;
cout<<" The Standard Deviation of the given numbers is: "<<stddev(array,n);
//prompt the user for next entry
cout<<" Enter the value for n (0 to exit ) :";
cin>>n;
}
return 0;
}

//function for calculating rms and returns the values
double rms(int arr[],int n){
int total;
for(int i=0; i<n; i++){
total = total +arr[i]*arr[i]; //calculates the squared mean
}
return(sqrt(total/n));
}
//function calculates the standard deviation and returns the sd
double stddev(int arr[], int n) {
int total=0;
int sd =0;
for(int i=0; i<n; i++){
total = total +arr[i]; //calculates the total
}
//calculate the mean
int mean = total/n;
for(int i=0; i<n; i++){
sd +=pow(arr[i]-mean,2);
}
return(sqrt(sd/n));
}

Sample output:


Enter the value for n :10

Enter the values :1
2
3
4
5
7
8
9
6
10

The Square Root of Mean :6.16441

The Standard Deviation of the given numbers is: 2.82843
Enter the value for n (0 to exit ) :4

Enter the values :2
3
4
5

The Square Root of Mean :3.60555

The Standard Deviation of the given numbers is: 6
Enter the value for n (0 to exit ) :0

RUN SUCCESSFUL (total time: 25s)

code for file :

//prototypes for calculating root mean squared and standard deviation
double rms(int[],int);
double stddev(int[],int);

int main(int argc, char** argv) {
int n;//number of numbers
  
ifstream in("in.txt");
if(!in){
cout<<"error";
return(0);
}
else{
in>>n;
int array[n];
int i=0;
while(!in.eof()||(i==n)){
in>>array[i];
i++;
}
//display RMS by calling the function
cout<<" The Square Root of Mean :"<<rms(array,n)<<endl;

//display sd;
cout<<" The Standard Deviation of the given numbers is: "<<stddev(array,n);
}
return 0;
}

//functions are given above.

sample ouput:

The Square Root of Mean :6.16441

The Standard Deviation of the given numbers is: 2.82843
RUN SUCCESSFUL (total time: 156ms)

in.txt

10
1
2
3
4
5
6
7
8
9
10