This is basically my assignment, I have most of it finished but I am new to programming so i'm not really sure what some of the error messages that are coming out mean. any help would be greatly appreciated
The simple algorithm for the program is as follows:
1. Ask user to input numbers and the numbers must be saved in an array.
2. Display the numbers
3. Calculate the sum, mean, and standard deviation of the numbers.
4. Display them
5. Display the differences between the numbers and the mean.
- Finish the basic requirements in main.
- Define a function to display numbers in the array.
- Define a function to calculate the sum of the numbers.
- Define a function to calculate the sum of the squared numbers.
- Define a function to calculate the mean of the numbers.
- Define a function to calculate the standard deviation.
Define a function to display the differences between the numbers and the mean.
Code:
#include<math.h>#include<iostream>
#include<array>
#define MAX_ITEM 5
using namespace std;
//Display numbers in the array
void display_array(double ar[])
{
for(int i=0; i<MAX_ITEM; i++){
cout << ar[i] << " ";
}
}
//Calculate the sum of the numbers
double cal_sum (double ar[])
{
double sum = 0;
for(int i=0; i< MAX_ITEM; i++){
sum+=ar[i];
}
return sum;
}
//Calculate the sum of the squared numbers
double cal_sum_sqrt (double ar[])
{
double sum_sqrt = 0;
for ( int i = 0; i < MAX_ITEM; i++){
sum_sqrt+=ar[i]*ar[i];
}
return sum_sqrt;
}
//Calculate the mean of the numbers
double cal_mean (double sum)
{
double mean = 0;
mean = sum/MAX_ITEM;
return mean;
}
//Calculate the standard deviation of the numbers
double cal_st_dev (double mean, double sum_sqrt)
{
double st_dev = 0;
st_dev = sqrt(sum_sqrt/MAX_ITEM-mean*mean);
return st_dev;
}
main()
{
double x [MAX_ITEM], sum, sum_sqrt, mean, standard_deviation, square;
for (int i=0; i<MAX_ITEM; i++)
{
cout << "Enter a number " << endl;
cin >> x[i];
}
cout << "The numbers are ";
for (int i=0; i<MAX_ITEM; i++)
{
cout << x[i] << " ";
}
square = sum_sqrt - mean;
sum = cal_sum(i);
sum_sqrt = cal_sum_sqrt(i);
mean = cal_mean(sum);
standard_deviation = cal_st_dev(square, mean);
cout << endl << "Sum of numbers " << sum << endl;
cout << endl << "sum sqared " << sum_sqrt << endl;
cout << endl << "Mean of numbers " << mean << endl;
cout << endl << "Standard deviation of numbers " << standard_deviation << endl;
system("pause");
}