Numbers in line

Discussion in 'C' started by varundesi, Oct 30, 2006.

  1. varundesi

    varundesi New Member

    Joined:
    Oct 17, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Oct 31, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Initialize nMin and nMax to the first member of the line when you first read it.
     
  3. varundesi

    varundesi New Member

    Joined:
    Oct 17, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0

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

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice