Nightmare working with array

Hello everyone,
I am new to C++ and I am taking an online class in programming C++ and I am completely lost in this chapter. I have been working on a problem for the past week and cannot seem to make any progress. If someone could please tell what I am doing wrong I would greatly appreciate it. I have attached my pseudocode and the source file. I know there is more than one error. The first problem I have is that I should only be able to input 15 numbers, but I am able to input 16. I am not sure how to write the code to subtract one on the array. My second problem is how to convert the "for i = 1 to counter" line to C++. Thanks for your help in advance.

Design the logic for a program that allows a user to enter 15 numbers, then display each number and it's difference from the numeric average of the numbers entered.

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
 Start
     Declarations
         num SIZE = 15
         num numbers[SIZE]
         num counter = 0
         num total = 0
         num average = 0
         num diffFromAvg = 0
         num  SENTINEL = -1

     output "Please enter a positive number: "
     input numbers[counter]
     while ((counter < 15) AND (numbers[counter] <> SENTINEL))
       counter = counter + 1
       total = total + numbers[counter]
       output "Please enter a positive number: "
       input numbers[counter]
     endwhile
     if (counter > 0) then
       average = total/counter
       for i = 1 to counter
         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


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
#include <iostream>
#include <string>
using namespace std;
int main() 
{
	    const int SIZE = 15;
	    int numbers[SIZE];
	    int counter = 0;
	    int total = 0;
	    int average = 0;
	    int diffFromAvg = 0;
	    const int SENTINEL = -1;
	     
	    cout << "Please enter a positive number or " << SENTINEL << " to quit: ";
	    cin >> numbers[counter];
	 
	        while((counter < 15) && (numbers[counter] != SENTINEL))
	    {
              counter++;
	          total = total + numbers[counter];
              cout << "Please enter a postive number or " << SENTINEL << " to quit: ";
	          cin >> numbers[counter];
           }
	          if(counter > 0)
	          {
	             average = total/counter;
	              
	             for (int i = 1; i < SIZE; i++)
	             {
	                 diffFromAvg = numbers[i] - average;
	                 cout << "Number[ " << i << " ]: " << numbers[i] << " Difference from Average is " << diffFromAvg << endl;
	                 }
                    } 
                    else
	                 {
	                     cout << "Processing incomplete. No values in the array." << endl;
                     }

               
	    
	          return 0;
}
Here are 2 equivalent loops:

1
2
3
4
5
int count;
for (count = 0; count < 15; count++) {
//do some stuff

}


1
2
3
4
5
6
7
int count = 0;

while (count < 15) {
//do some stuff

count++
}


Are you sure you need the sentinel variable?

This:
total = total + numbers[counter];

can be written like this:

total += numbers[counter];

This is a book written by the guys who invented the C language, you might find it a good reference for basic stuff - maybe the first 4 chapters.



http://zanasi.chem.unisa.it/download/C.pdf


Also there is lots of referenc on this page (top left of page)

Finally Google


HTH
Thank you for your reply. I noticed the change with total. If you take more code and compile it, it does nothing that it is suppose to. I am so lost right now. I would pull my hair out but I have none left!!!!
Topic archived. No new replies allowed.