Skip to content

Instantly share code, notes, and snippets.

@kdmkdmkdm
Created April 22, 2012 08:29
Show Gist options
  • Select an option

  • Save kdmkdmkdm/2462727 to your computer and use it in GitHub Desktop.

Select an option

Save kdmkdmkdm/2462727 to your computer and use it in GitHub Desktop.
averageWithTheHelpOfReddit3CPlusPlus
// averageWithTheHelpOfReddit
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
// Declare.
int numberOfInputs = 0;
float valueOfCurrentInput = 0.0f;
float sum = 0.0f;
float avg = 0.0f;
cout << "Enter the number of values to average: ";
cin >> numberOfInputs;
// Find sum.
for(int i = 0; i <= (numberOfInputs - 1); ++i)
{
cout << "[" << i << "] = ";
cin >> valueOfCurrentInput;
sum += valueOfCurrentInput;
// Calculate average.
avg = sum / numberOfInputs;
}
// Show average.
cout << "Average = " << avg << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment