[C++] Average number of numbers
Posted: Wed Mar 06, 2013 11:19 pm
Hello,
I will explain this tutorial with comments next to code:
I will explain this tutorial with comments next to code:
Code: Select all
#include <iostream> //Input/output library
#include <cmath> //mathematical operations library
int main()
{
int sum, num, no;
float avg;
sum = 0; //Declarations
printf("How much numbers will there be?");
scanf("%d", &num); //Input of number of numbers
for(int i = 0; i < num; i++) {
scanf("%d", &no); //Load number
sum += no;
avg = (float)sum / i; //Calculate temp average and print it out
printf("Average so far: %.2lf \n", avg);
}
//Another calcultion this time final one
avg = (float)sum / count;
printf("Overall average %.2lf \n", avg);
system("pause");
//Pause console, end function
return 0;
}