Finds average of numbers typed by user - C++ Data Type

C++ examples for Data Type:float

Description

Finds average of numbers typed by user

Demo Code

#include <iostream>
using namespace std;
int main()//w w w  .  j a  v  a 2 s  .  c  o m
{
   float flarr[100];                   //array for numbers
   char ch;                            //user decision
   int num = 0;                        //counts numbers input
   do
   {
      cout << "Enter number: ";        //get numbers from user
      cin >> *(flarr+num++);           //until user answers 'n'
      cout << "   Enter another (y/n)? ";
      cin >> ch;
   }
   while(ch != 'n');
   float total = 0.0;                  //total starts at 0
   for(int k=0; k<num; k++)            //add numbers to total
      total += *(flarr+k);
   float average = total / num;        //find and display average
   cout << "Average is " << average << endl;
   return 0;
}

Result


Related Tutorials