Thread: Short and Sweet Part 2.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Short and Sweet Part 2.

    Why is it that when I use a calculator to get my answer, I come up differently than what the program gives.

    What is wrong here??

    Code:
    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    int main()
    
    {
    
    ifstream inFile;
    ofstream outFile;
    
    float average;
    float num,sum,count;
    bool lessThanThirteen;
    
    inFile.open("K:\\DATFILE1.TXT");
    outFile.open("K:\\ANSWERS.TXT");
    if ( !inFile )
    {
    	cout<<"**Can't open input file**"<<endl;
    	return 1;
    }
    
    sum = 0;
    count= 1;
    lessThanThirteen = true;
    
    	while (inFile)
    	
    	{
    		inFile >> num;
    		sum = sum + num;
    		count++;
    	
    	}	
    
    
    			while (lessThanThirteen)
    		{
    			lessThanThirteen = (count < 13);
    			average = sum / count;
    		}
    	
    	
    			outFile <<fixed<<setprecision(3)
    					 <<"The average of the first 12 integers is "<< average << " ."<<endl;
    inFile.close();
    inFile.clear();
    return 0;
    }

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Is it supposed to average the first 12 numbers? Because right now it it adds up the ALL the numbers in the file and then if the count is less than 13, it divides that sum by the count.

    You need to sum only the first 12 numbers which means you need to STOP adding numbers to sum when count gets bigger than 12.
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Brad0407 View Post
    Is it supposed to average the first 12 numbers? Because right now it it adds up the ALL the numbers in the file and then if the count is less than 13, it divides that sum by the count.

    You need to sum only the first 12 numbers which means you need to STOP adding numbers to sum when count gets bigger than 12.

    That is what I need to do. I think I can get this from here, but if not, I will be back.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    I don't know if I'm overtired or what here....This is what I came up with but still not the correct answer. Am I going about this all wrong?

    Code:
    inFile >> num;
    
    	while (inFile && count < 13)
    	
    	{
    		sum = sum + num;
    		count++;
    		inFile >> num;
    
    			
    	}	
    	
    			while (lessThanThirteen)
    		{
    			lessThanThirteen = (count < 13);
    			average = sum / count;
    		}

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would write the same as
    Code:
    	while (inFile >> num)
    	{
    		sum += num;
    		count++;
    		if(count >= 13)
    			break;
    	}
    	if(count > 0)
    		average = sum / count;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by vart View Post
    I would write the same as
    Code:
    	while (inFile >> num)
    	{
    		sum += num;
    		count++;
    		if(count >= 13)
    			break;
    	}
    	if(count > 0)
    		average = sum / count;
    Riddle me this... when I manually put my first 12 integers into a calculator I get an average of 340.083. Even when I use your code, I get an average of 313.923. Ughh.. this is going to be the death of me yet tonite.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Here is working code (without files for simplisity)
    Code:
    #include<iostream>
    #include<sstream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    int main()
    {
    
    	float average;
    	float num,sum = 0.f;
    	int count = 0;
    
    	stringstream in ("10.0 20.0 30.0");
    
    	while (in >> num)
    	{
    		sum += num;
    		count++;
    		if(count >= 13)
    			break;
    	}	
    
    	average = sum /count;
    
    	cout <<fixed<<setprecision(3)
    		<<"The average of the first " << count <<" integers is "<< average << " ."<<endl;
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    vart's code sums the first 13 integers. Perhaps that's why the average is higher? Try changing the 13 to 12.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by vart View Post
    Here is working code (without files for simplisity)
    Code:
    #include<iostream>
    #include<sstream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    int main()
    {
    
    	float average;
    	float num,sum = 0.f;
    	int count = 0;
    
    	stringstream in ("10.0 20.0 30.0");
    
    	while (in >> num)
    	{
    		sum += num;
    		count++;
    		if(count >= 13)
    			break;
    	}	
    
    	average = sum /count;
    
    	cout <<fixed<<setprecision(3)
    		<<"The average of the first " << count <<" integers is "<< average << " ."<<endl;
    	return 0;
    }
    Whoa... that is way beyond what I have been instructed so far. I'm just a beginner here. LOL. There is alot here I don't know the meaning to. Take it back to wayyyy basic. I would like to understand what I'm doing. I'm just wondering if I don't have a problem with my visual studio. It wouldn't be the first time and how the heck it happens is beyond me.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include<iostream>
    #include<sstream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    int main()
    {
    
    	float average;
    	float num,sum = 0.f;
    	int count = 0;
    
    	stringstream in ("55 67 458 23 81 33\n782 375 528\n405 324 950 46\n14 864 551 38 167 518 630");
    
    	while (in >> num)
    	{
    		sum += num;
    		count++;
    		if(count >= 12)
    			break;
    	}	
    
    	average = sum /count;
    
    	cout <<fixed<<setprecision(3)
    		<<"The average of the first " << count <<" integers is "<< average << " ."<<endl;
    	return 0;
    }
    Output
    Code:
    The average of the first 12 integers is 340.083 .
    You have to replace the in stream withyour inFile stream
    It should not be a problem
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Daved View Post
    vart's code sums the first 13 integers. Perhaps that's why the average is higher? Try changing the 13 to 12.
    I think his code is correct, it states greater than or equal to 13 to stop running.

    ok maybe I'm just too tired.. <sigh>
    Last edited by dispatch4599; 03-03-2008 at 12:03 AM.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dispatch4599 View Post
    I think his code is correct, it states greater than or equal to 13 to stop running.
    But I start counting from 0, so if you want only 12 - you have to stop when count riches 12 - as in my last sample
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I think his code is correct, it states greater than or equal to 13 to stop running.
    It stops running after including the 13th item. I thought you only wanted 12.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Daved View Post
    >> I think his code is correct, it states greater than or equal to 13 to stop running.
    It stops running after including the 13th item. I thought you only wanted 12.
    you are correct. I apologize. Now can someone explain to me what the float num, sum=0.f means.. mainly the 0.f? I have never seen this used before.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That just initializes the sum to zero. Since sum is a float, vart used a float value to initialize it. Numbers with decimals are doubles in C++, but since you are using floats, you have to add the f to the end to indicate that you want to use float. (doubles are actually recommended over float assuming you don't have a requirement to use float.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Short & Sweet for a beginner
    By dispatch4599 in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2008, 08:33 PM
  2. Function Overloading Short & Sweet Query
    By forumuser in forum C++ Programming
    Replies: 15
    Last Post: 10-17-2007, 08:15 AM
  3. Delete a binary record
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-01-2002, 02:07 AM