Array numbers difference from the average math proble

Supposed to accept 12 numbers, display the numbers and the difference from the average. I've searched the forum and similar threads have helped some but I'm still stuck on the math not working. It compiles but the resulting math is wrong.

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
68
69
70
71
72
  // Start
//     Declarations
//         num SIZE = 12
//         num numbers[SIZE]
//         num value = 0
//         num counter = 0
//         num total = 0
//         num average = 0
//         num diffFromAvg = 0
//         num  SENTINEL = -1
//
//     output "Please enter a positive number: "
//     input value
//     while ((counter < SIZE) AND (value <> SENTINEL) )
//       total = total + value
//       numbers[counter] = value
//       counter = counter + 1
//       if (counter <> SIZE)
//         output "Please enter a positive number: "
//         input value
//       endif
//     endwhile
//
//     if (counter > 0) then
//       average = total/counter
//       for i = 0 to counter - 1
//         diffFromAvg = numbers[i] - average
//         output "Number[",i,"]: ",numbers[i]," Difference from Average is ",diffFromAvg
//       endfor
//     else
//       output "Processing incomplete. No values in the array."
//     endif
// Stop

#include <iostream>
using namespace std;
int main()
{
	const int SIZE = 12;
	int numbers[SIZE];
	int value = 0;	
	int counter = 0;
	int total = 0;
	int average = 0;
	int diffFromAvg = 0;
	const int SENTINEL = -1;
	
	cout << "Please enter a positive number: " ;
	cin >> value;
	
while((counter < SIZE) && (value != SENTINEL))
{
	counter++;
		total = total + value;
	cout << "Please enter a positive number: ";
		cin >> value;
}	
	if ((counter != SIZE) && (counter > 0))
	{
	average = total/counter;
	numbers[counter] = value;
	}
	counter = counter + 1;
	for (int counter=0; counter<SIZE; counter++)
	{
	diffFromAvg = numbers[counter] - average;
	cout << "Number[ " << counter << " ]: " << numbers[counter] << " Difference from Average is " << diffFromAvg << endl;
	}
{
			cout << "Processing incomplete. No values in the array.";	
	}
}


Results:
Please enter a positive number: 1
Please enter a positive number: 2
Please enter a positive number: 3
Please enter a positive number: 4
Please enter a positive number: 5
Please enter a positive number: 6
Please enter a positive number: 7
Please enter a positive number: 8
Please enter a positive number: 9
Please enter a positive number: 10
Please enter a positive number: 11
Please enter a positive number: 12
Please enter a positive number: 13
Number[ 0 ]: 4253680 Difference from Average is 4253680
Number[ 1 ]: 0 Difference from Average is 0
Number[ 2 ]: 85 Difference from Average is 85
Number[ 3 ]: 0 Difference from Average is 0
Number[ 4 ]: 8393520 Difference from Average is 8393520
Number[ 5 ]: 0 Difference from Average is 0
Number[ 6 ]: 1 Difference from Average is 1
Number[ 7 ]: 0 Difference from Average is 0
Number[ 8 ]: -1 Difference from Average is -1
Number[ 9 ]: -1 Difference from Average is -1
Number[ 10 ]: 4253781 Difference from Average is 4253781
Number[ 11 ]: 0 Difference from Average is 0
Processing incomplete. No values in the array.
--------------------------------
Process exited after 9.533 seconds with return value 0
Press any key to continue . . .
Last edited on
It's not the "maths" that's wrong, it's the fact that you enter values in the loop from lines 51 to 57 but don't put them in the array there.

There is nothing meaningful in the array numbers[] ... because you didn't put anything meaningful in the array numbers[].
Topic archived. No new replies allowed.