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:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
usingnamespace 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.
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.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
usingnamespace 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;
}
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.