[C++] Average number of numbers

All tutorials created in C++ to be posted in here.
2 posts Page 1 of 1
Contributors
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

[C++] Average number of numbers
Filip
Hello,

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;
}
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
benji_19994
VIP - Donator
VIP - Donator
Posts: 156
Joined: Mon Apr 16, 2012 3:13 pm

Thanks #Filip, =D Nice tutorial
2 posts Page 1 of 1
Return to “C++ Tutorials”