I have an assignment where I was provided some code and I have to write a couple functions. I'll write the assignment and then the code I have so far.
Define a function that takes a partially filled array of numbers as its arguments and returns the standard deviation of the numbers in the partially filled array.
1.The numbers in the array will be of type double. The do-while loop inside main() reads in numbers and put them into the array. This do-while loop should continue to read in numbers until either: the user types a negative number, or MAXSIZE has been read. Write the condition for the do-while.
2.Then write a function that will accept an array and print out the contents of the array. This function should accept an array and the number of elements that are to be printed.
3.Then write another function that will accept an array and calculate the standard deviation of the elements of the array. This function should accept an array and the number of elements that are to be printed. This function will also call the average function, which is provided for you.
Main Problems: The control variable in the for loop in the printarray function increments one too many times.
#include <iostream>
#include <cmath>
usingnamespace std;
// prototype here to print array
void PrintArray(double a1[], int);
double stdDev(double [], int);
double average(double [], int);
constint MAXSIZE = 100;
int main()
{
double array1[100];
int i = 0;
double somenumber;
do
{
cout << "Please enter a number to enter into the array or a negative number to stop."<<endl;
cin >> somenumber;
if (somenumber >= 0)
{
array1[i] = somenumber;
i++;
}
} while (somenumber >= 0 && somenumber <= MAXSIZE);
cout << "There were " << i << " numbers entered into the array." << endl;
//function call to function that will print the array
PrintArray (array1, i);
//function call to stdDev function
stdDev(array1, i);
return 0;
}
void PrintArray(constdouble s[], int size)
{
for (int i=0; i <= size; i++)
{
cout << "The contents of the array are: " << s[i] << endl;
}
}
double stdDev(double s[], int size)
{
double temp(0);
temp = ((pow(average(s, size),2))/ size - 1);
return (temp);
}
double average(double s[], int size)
{
double sum = 0;
for(int i = 0; i < size; i++) {
sum += s[i];
}
return sum / size;
}
here is some output:
Please enter a number to enter into the array or a negative number to stop.
1
Please enter a number to enter into the array or a negative number to stop.
2
Please enter a number to enter into the array or a negative number to stop.
3
Please enter a number to enter into the array or a negative number to stop.
4
Please enter a number to enter into the array or a negative number to stop.
5
Please enter a number to enter into the array or a negative number to stop.
-1
There were 5 numbers entered into the array.
The contents of the array are: 1
The contents of the array are: 2
The contents of the array are: 3
The contents of the array are: 4
The contents of the array are: 5
The contents of the array are: -9.25596e+061
The standard deviation of the numbers entered is: 0.8
Press any key to continue . . .
Is there a way that I can just display the numbers in the array? I would like to just say "The contents of the array are:" once. By the way, would it be better to say "the elements of the are"?.
I think my function to calculate the standard deviation is wrong.
It seems now that I have a big problem with the function that calculates the standard deviation. I'm not sure how to calculate the deviations from the average. I'm trying to use a for loop but I don't know how I would store each deviation. Should I create an array for each deviation? Is that really my only option here?
I think I'm getting fairly close. I decided I didn't need to create an array.
Here is the code for the function that calculates the standard deviation. I think the problem might be how I'm storing the individual deviations from the average for each number. Hopefully I can figure this out or someone can help. I had to skip organic chemistry and this program is due by 5:00 pm.