Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Numbers in line (http://www.go4expert.com/forums/numbers-in-line-t1759/)

varundesi 31Oct2006 00:16

Numbers in line
 
Hi there, I need to write a program that reads up to 10 numbers per line to be stored in an array.The program calculates and displays the sum of the numbers, the mean (arithmetic average) of the numbers, and the largest and smallest values entered for each line.

the input file is

45 98 35 23 67 84
65 91 20 54 62 37 65 84 32
21 54 95 87 35 62 54 78
56
95 62 35 54 78


i have this much so far
Code:

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
        string sBuf;
        int nSum=0;
        double dMean=0;
        int nMin=999999;
        int nMax=0;
        int arnNums[10], nSize, i;
        ifstream fin("71dIN.txt");
        istringstream istr(sBuf); // open input array
        while(1)
        {
                getline(fin, sBuf);
                istr.clear();
                istr.str(sBuf);
                if(fin.eof()) break;
                i=0;
                while(1)
                {
                        istr>>arnNums[i];
                        i++;
                        if(istr.eof()) break;
                }       
                nSize = i;
                for(i = 0; i<nSize; i++)
                {
                if (arnNums[i]<nMin)
                        nMin=arnNums[i];
                else if (arnNums[i]>nMax)
                        nMax=arnNums[i];
                nSum+=arnNums[i];
                dMean=nSum/nSize;
                }
                cout<<nSum<<" ";
                cout<<dMean<<" ";
                cout<<nMin<<" ";
                cout<<nMax<<" ";
                cout<<endl;
                nSum=0;
        }
        return 0;
}


// The output i m getting is

352 58 23 98
510 56 20 98
486 60 20 98
56 56 20 98
324 64 20 98

shabbir 31Oct2006 10:17

Re: Numbers in line
 
Initialize nMin and nMax to the first member of the line when you first read it.

varundesi 1Nov2006 08:02

Re: Numbers in line
 
Quote:

Originally Posted by shabbir
Initialize nMin and nMax to the first member of the line when you first read it.


Thanks Shabbir for the reply, got working by reseting the value of min and max.
Thanks again.


All times are GMT +5.5. The time now is 23:14.