Error when testing user input

I am creating a program for my class that gets ten integers from the user, stores them in an array. Then passes that information to a function to compute the average for those ten numbers. I have the program finished (although any tips are welcome) and am now testing. When I fill the array with all zeros the sum of everything comes to 3.76505e-039. Is this common? And is there a way to change it so the sum equals 0?

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cstdlib>
#include <iostream>

/****************************************************/
/* File: name of your file with the source code     */
/*                                                  */
/* Created by: Aaron Criswell                       */
/* Date: 15 Aug 2011                                */
/*                                                  */
/* Program to compute average from array            */
/*                                                  */
/* Inputs: (keyboard)                               */
/*   Ten integers numbers[10]                       */
/*                                                  */
/* Output:                                          */
/*   Average of ten user supplied integers          */
/*                                                  */
/*                                                  */
/****************************************************/

using namespace std;

//prototype function
void average(float avg[], int sizeOfArray);

int main()
{
    char cont = 'y';
    
    do
    {
    //declare and initialize variables
    int i = 0;
    float numbers[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    
    //print instructions and request for user input
    cout <<"This program will calculate the average for ten positive integers." << endl; 
    cout <<"Please enter your numbers now." << endl;
    cout << endl;
    
    //cycles through array for user input
    for (i=0; i<10; i++)
    {
        cout <<"Enter integer #" << i+1 << ": ";
        cin >> numbers[i];
        cin.ignore();
        cout << endl;
    }
    
    //test to see array data
    //    for (i=0; i<10; i++)
    //{
    //    cout << numbers[i] << endl;
    //}
    
    //call average function
    average(numbers, 10);
    cout << endl;
    
    cout <<"Would you like to try again? (Y/N):  ";
    cin >> cont;
    cin.ignore();  
    }
    //gives user option to continue
    while (cont == 'Y' || cont == 'y');
    
    return EXIT_SUCCESS;
}


void average(float avg[], int sizeOfArray)
{ 
      //declare and initialize variables
      int i = 0;
      float x = 0.0;
      float sum = 0.0;
      float n = 0.0;
      
      //loop to add array input, then divide to get average
      for (i=0; i<=sizeOfArray; i++)
          {
                x = avg[i];
                sum = sum + x;
                n = sum / sizeOfArray;
          }
      cout << endl;    
            
      cout << "The average for your number set is:  " << n << endl;    
}
In your loop, i<=sizeOfArray should be i<sizeOfArray because sizeOfArray-1 is the last index.
Thank you. It works now and makes since to me. The function was grabbing garbage because I had the for loop set to cycle 1 to many times. I will have to work on paying attention to detail more.
Also, you are calculating the average over and over in the loop. That isn't necessary. You should wait until you have summed everything and then calculate the average.

Since you are a student, I can understand why you would write your own loops, but for future reference, it is better to use std algorithms to perform common tasks. Take a look at these links.
http://cplusplus.com/reference/std/numeric/accumulate/
http://cplusplus.com/articles/zvbCpfjN/
Topic archived. No new replies allowed.