Average of numbers input by user

Hi,

I am new here. I am pretty new to C++ and I want to solve a problem with the following statement: "read numbers inserted by keyboard until user press 0. Show the average of all the numbers inserted". I should use while loop to solve it. Well, I tried to solve it and 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
    cout << "Basics using While and Do While 5 While" << endl;

    int num;
    int sum=0;
    int n=0;

    while (num!=0) {
        printf("Insert a number: ");
        scanf("%d", &num);
        sum=sum+num;
        n++;
        }
    float avg=(sum/n);
    printf("The average of the numbers inserted is %f \n",avg);
    printf("End of the program");
    return 0;
}


I tried to print the sum, everything is ok but for any reason, I can't get the average, instead, the program gives me 1 or 2. Too much frustration at this moment. Could you please tell me what I am doing wrong?

Just to clarify, I don't use cout because I have been taught to use libraries instead using printf. I am using an open source software: CodeBlocks.

Thank you very much in advance.
Last edited on
can you further explain this?
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
int num,sum=0,n=0;
double avg;
for(;;)
{
cout<<"type number: ";
cin>>num;
if(num==0)
{
goto next;
}else{
sum=sum+num;
}
++n;
}
next:
avg=sum/n;
cout<<"\nthe avg is: "<<avg;
getchar();
}
The exercise consists in: The user insert numbers through keyboard until insert "0" to end the program. Then, you must calculate the average of those numbers before 0 was inserted. You must solve it using a while loop and again using do while loop.

The code I am trying without any success is the mentioned above. Further explanations:

1) libraries used:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
The last two ones is for printf

2) cout << "Basics using While and Do While 5 While" << endl;
is the name of the exercise, just to display on screen.

3) Variables are: num, sum and n. Sum and n starts with 0.
num: the number the user inserts.
sum: The sum of the numbers.
n: The number of numbers inserted (for example, the user insert 1,3,4,5,6 -> n is 5)

4) while (num!=0) is the condition while number inserted is not 0.

5) printf("Insert a number: ");
scanf("%d", &num);
This is used to ask the user the numbers through keyboard.

6) sum=sum+num;
the sum operation sums the current number with the last inserted by user.

7) n++;
acumulator for n to divide the sum of the numbers by the number of numbers inserted (n)

8) float avg=(sum/n)
operation to calculate average of numbers inserted in float format to display decimals

9) printf("The average of the numbers inserted is %f \n",avg);
printf("End of the program");
Just to display what is the average of the numbers with decimals and to advice that the program is finished.
I tried to print the sum, everything is ok but for any reason, I can't get the average, instead, the program gives me 1 or 2. Too much frustration at this moment. Could you please tell me what I am doing wrong?


float avg=(sum/n);

You've defined avg as a float, but sum and n are both integers, so you'll end up with integer division and an integer result. One way to get around that could be to declare sum as a float (or double) and initialize it to 0.0.

Right now, if they do enter 0, that value is being added in to sum and n is being incremented at line 19 - this will throw off your average calculation.
Exactly, is almost done! I changed int sum=0 and int n=0 for float sum=0 and float n=0. The program calculates the average.

However... the program also includes 0 in the calculation and 0 is only to exit the program. What can I do to exclude the 0 in the average calculation?
Have you covered break; statements for loops? If the user enters 0, you could use a break statement to exit the loop before the sum and counter update.
Hi wildblue. Sorry for this question, where should I put the break? The code is still the same as you can see above except I placed float in sum and n instead of int.
After you read in the number in the loop - check if it's equal to 0 - if it is, then break.
Yes! Works! You saved me a lot of time. The code at this moment:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
    cout << "Bloque I - Conceptos Basicos EI While Do While 5 While" << endl;

    int num;
    float suma=0;
    float n=0;

    while (num!=0) {
        printf("Introduce un numero: ");
        scanf("%d", &num);
        if (num==0) {
            break;
        }
        else {
        suma=suma+num;
        n++;
        }
    }
    float media=(suma/n);
    printf("La media de los numeros introducidos es %.2f \n",media);
    printf("Fin del programa");
    return 0;
}
Well, were are my manners?

Let me to introduce myself. I am not programmer, I am a chemical engineer. I am unemployed so I am taking a freelance course in C/C++, I am still starting. The problem is that the teacher is not good at least explaining and unresponsive to questions. It's sad I've paid 450 Eur for that course without support. Lots of exercises but I don't get any feedback from the teacher.

I noticed some employers demand programming skills for chemical engineering jobs so I thought it's time to earn an additional skill. In the degree I only had one subject related.

Thanks for your help, it's a great pleasure to meet you.
And welcome to the forum, JorgeChemE - glad you were able to get your code working as you wanted it to.
Topic archived. No new replies allowed.