Help With Averaging Code

Write your question here.
Could someone help me figure out why the program has an initialized local variable?
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

 #include <iostream>

int main()
{

    int n;
    int i;
    int a;
    int answer;
    float num[100];

    std::cout << "Enter the numbers of data: ";
    std::cin >> n;

    while (n > 100 || n <= 0)
    {
        std::cout << "Error! number should in range of (1 to 100).\n";
        std::cout << "Enter the number again:\n";
        std::cin >> n;
    }
    for (i = 0; i < n; i++)
    {
        std::cout << i + 1 << ". Enter number: ";
        std::cin >> i;
        
        a += i;
    }

    answer = a / n;
    std::cout << "Average\n" << answer;

    return 0;

}


Also, A secondary link where I thought posting the same post again was a good idea.
http://www.cplusplus.com/forum/beginner/271026/
Last edited on
Hello LegalWeasel,

Lines 7 - 11 define variables, but they all have a garbage value. "a" is defined as an "int" and on my computer the garbage value is usually "-858993460", but could be different. So "-858993460" + 1 = "-858993459" and in the end "answer" will never have a correct value.

Try:
1
2
3
4
5
int n{};
int i{};  // <--- Should be defined in the for loop.
int a{};
int answer{};
double num[100]{};

The empty {}s or "uniform initializer" will set the "int"s to zero and all the elements of the array to "0.0". It is always best to initialize your variables when defines. At the very least you know they will not contain a garbage value.

"double" is the preferred floating point type, but sometimes a "float" will work if the numbers are small.

Beyond that you will have to be more specific as to which line is causing the problem.

In this code:
1
2
3
4
5
6
7
    for (i = 0; i < n; i++)
    {
        std::cout << i + 1 << ". Enter number: ";
        std::cin >> i;
        
        a += i;
    }

"i" is the loop iterator, so on line line 4 when you enter a new value for "i" it throws the whole for loop off. You need a variable other than "i" unless you want to use "i" to access the array. Then it would be: std::cin >>num[i]; and the next line would be: a += num[i];. You may want to consider:
std::cout << ". Enter number " << i +1 <<": ";

And the output would look like:

Enter the numbers of data: 3
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30

The average is: 20


In line 31 I removed the "\n" from the end of "average and put it at the beginning of the string.

Andy.
Hello LegalWeasel,

Sorry I got side tracked and di not mention.

Try not to use single letter variables. They may be easy to type, but hard to follow and keep track of.

Learn to give variables a proper name the describes what it is or what it does. The first benefit is to you and then to others that have to read your code.

Save the single letters for for loops.

As an example "a" would work better as "sum" because that is what it is doing. "n" could be "numOfData" or "maxData" or something else that is similar in its description.

In for loops it is best to define the variable inside the for loop as: for (int i = 0; i < n; i++). This makes "i" local to the for loop and the variable is destroyed when the for loop ends. The only reason to define "i" outside the for loop is if you need its value outside the for loop.

Andy
Hello Andy,

Thanks for the advice on variables, garbage values, and initializing "i" within the for loop.

The initial problem has gone away but now I've encountered another problem and I was wondering if you could help.

When I run the program it asks for the initial numbers of data but not the numbers themselves.
Hello LegalWeasel,


When I run the program it asks for the initial numbers of data but not the numbers themselves.



I am not sure I follow what you mean. With the changes I have talked about the program will work.

This is what I have come up with along with some late changes:
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

#include <iostream>
#include <iomanip>

int main()
{

    int maxNumbers{};
    //int i{};  // <--- Should be defined in the for loop.
    int sum{}, input{};
    double answer{};
    //double num[100]{};  // <--- Could be useful, but not really needed.

    std::cout << std::fixed << std::setprecision(6);

    std::cout << "\n How many numbers would you like? max 100: ";
    std::cin >> maxNumbers;

    while (maxNumbers > 100 || maxNumbers <= 0)
    {
        std::cout << "\n     Error! number should in range of (1 to 100).\n";

        std::cout << "\n Enter the number again: ";
        std::cin >> maxNumbers;
    }

    for (int i = 0; i < maxNumbers; i++)
    {
        std::cout << " Enter number: " << i + 1 << ": ";
        std::cin >> input;

        sum += input;
    }

    answer = static_cast<double>(sum) / static_cast<double>(maxNumbers);

    std::cout << "\n The average is: " << answer;

    return 0;

}

In case you want a different output for the average I added lines 3 and 14. Along with changing line 11.

I changed the prompt on line 16. I am guessing that English may not be your primary language.

In line 21 I like to indent an error message. It makes it different and tends to catch your attention.

In line 23 I removed the "\n" from the end of the string to put the "cin" on the same line. May be a personal choice, but I like it better.

Line 35 integer division is fine if that is what you want, but floating point division gives a more accurate answer. Since the 2 variables on the rhs are "int"s I had to type cast them to doubles, but only 1 is actually needed. The other will be promoted to a double before the division is done.

Andy
Hey there, Thanks for all the advice given, sorry if it was a bit of a hassle. Turns ou the problem was with the for loop missing the 3rd statement, Also prompt on line 16 was simply for testing, Thanks for all the help, have a nice day.
Topic archived. No new replies allowed.