avg of array

Why is my program not returning the correct average?

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
  
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double prices[10] = { 96.5, 100.5, 100.5, 100.5, 99, 99, 99, 100, 98.5, 98.9 };
	double total = 0.0;
	double average = 0.0;

	for (int x = 0; x < 10; x++)
	{
		total += prices[x];
		x++;

		average = static_cast<double> (total) / static_cast<double> (x);
	}

	cout << fixed << setprecision(2);
	cout << "Average stock price: $" << average << endl;

	system("pause");
	return 0;
}	//end of main function
Line 15.

PS. Why is the line 17 inside the loop?

PS2. You do have "magic value" 10 on lines 8 and 12. What if you later change one, but forget to fix the other(s)?
Furthermore, you both state the size of array and have some initializers for it. What if their count differs?
lines 12-18 is all i added to this pre-existing code. My task is to complete it using a for loop. I omitted line 15 and the {} in the for loop and
now I'm prompted that "x" is undefined line 17
Last edited on
Still off


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double prices[10] = { 96.5, 100.5, 100.5, 100.5, 99, 99, 99, 100, 98.5, 98.9 };
	double total = 0.0;
	double average = 0.0;

	for (int x = 0; x < 10; x++)
	{
		total += prices[x];
		average = static_cast<double> (total) / static_cast<double>(x);
	}

	cout << fixed << setprecision(2);
	cout << "Average stock price: $" << average << endl;

	system("pause");
	return 0;
}	//end of main function 
The big problem with the code in the OP is that x is being incremented inside the loop and also in the update section of the for construct for each iteration of the loop. That means that every other value is skipped.

The average can be calculated after the loop, although you won't get the wrong answer if you do it in the loop - you'll just be doing more work than is necessary.
cire

i fixed it above^^ but still the avg is off
Well, I was mistaken. To get the average, you must divide by the number of elements in the array, which would be 10. Within the loop, x can never be equal to 10.

You're dividing by 0 at one point. Bad stuff. ;)
Last edited on
alas! The subscripts and elements are throwing me off i got it to work i believe.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double prices[10] = { 96.5, 100.5, 100.5, 100.5, 99, 99, 99, 100, 98.5, 98.9 };
	double total = 0.0;
	double average = 0.0;

	for (int x = 0; x <= 10; x++)
	{
		total += prices[x];
		average = static_cast<double> (total) / static_cast<double>(x);
	}

	cout << fixed << setprecision(2);
	cout << "Average stock price: $" << average << endl;

	system("pause");
	return 0;
}	//end of main function 


Thanks keskiverto and cire!
Last edited on
No.

Now your loop adds 11 values together, but your array has only 10 values in it.
Furthermore, the first iteration of the loop still divides with 0.

Move the calculation of the average to the outside of the loop. Move line 14 into line 16.
Fix the range of the loop. It is undefined behaviour to dereference nonexisting prices[10].


Of course the x is undefined outside of the loop. It is declared in the initialization part of the loop and thus its lifetime ends when the loop ends.

You do assume that the array has 10 elements and your loop should iterate 10 times. Similarly, you can divide with 10, rather than with x.
Oh yea hmmm oops

Does this fix the issue keskiverto?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double prices[10] = { 96.5, 100.5, 100.5, 100.5, 99, 99, 99, 100, 98.5, 98.9 };
	double total = 0.0;
	double average = 0.0;

	for (int x = 0; x <=9; x++)
		total += prices[x];   
		
	average =  (total) / 10; 
	


	cout << fixed << setprecision(2);
	cout << "Average stock price: $" << average << endl;

	system("pause");
	return 0;
}	//end of main function 
Last edited on
Topic archived. No new replies allowed.