Tuesday, April 24, 2012

C++ Compute Average of two Numbers (Average Program)

The Program Compute Average for Two Numbers Entered by User

#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include "vector"
#include <sstream>
using namespace std;

int _tmain(int argc)
{
    string str ;
    vector<int> vect;


     cout<<"enter the string of numbers"<<endl;
     cin >> str ;
    stringstream ss(str);
int i;
double sum = 0 ;
while (ss >> i)
{
        vect.push_back(i);

        if (ss.peek() == ',')
                ss.ignore();
}
 vector< int >::const_iterator constIterator;

   // display vector elements using const_iterator
   for ( constIterator = vect.begin();constIterator != vect.end(); ++constIterator )
   {
       sum=sum+*constIterator;
   }
   cout<<"Numbers  Sum  =  " << sum  <<endl;
   cout<<"Numbers Count   =  " << vect.size()  <<endl;
   cout<<"Numbers  Average  =  " << sum/vect.size()  <<endl;
   cout << endl;
getch();
    return 0;
}

No comments:

Post a Comment