Help for 1D Array

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.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cmath>
using namespace std;

// prototype here to print array
void PrintArray(double a1[], int);
double stdDev(double [], int);
double average(double [], int);

const int 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(const double 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 . . .
If your array contains five elements, they are indexed 0 - 4.

This loop...
1
2
3
4
for (int i=0; i <= size; i++)	
{
	cout << "The contents of the array are: " << s[i] << endl;  
}

...has six iterations: 0, 1, 2, 3, 4, 5.

The condition needs to be i < size not i <= size.
Last edited on
Well, there are several issues. But the one you are asking about it because of this line.

for (int i=0; i <= size; i++)

you need just just make it "i < size" because the arrays indexes start at s[0].
Thanks for the quick help guys.

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.

scapegote, what other issues do you see?
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.

1
2
3
4
5
6
7
8
9
10
11
12
double stdDev(double s[], int size)
{
	double indivDev, indivDev_sum(0), stand_dev;

	for (int i=0; i < size; i++)
	{
		indivDev = pow(s[i] - average(s, size),2);
		indivDev_sum = indivDev_sum + indivDev; 
	}
	stand_dev = sqrt((indivDev_sum / size-1));
	return (stand_dev);
}
What's the problem with it? You didn't say.
Dear Standard Deviation calculated as

squar root((x-(avg))*(x-(avg))/n)
Topic archived. No new replies allowed.