file operation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 27
Reputation: esesili is an unknown quantity at this point 
Solved Threads: 0
esesili esesili is offline Offline
Light Poster

file operation

 
0
  #1
Aug 24th, 2009
Hi all,
I am having problem about file operations. There are a few numbers in my txt file. I want the program to calculate average of these numbers in the txt file. The code I have written is as below. Number of numbers in the file is 5. When I return count it returns 5 .The sum of numbers should be 92 but I get 94, and I get average 18.8 but the correct average should be 18,4 (txt file is attached). So can anyone help me about it? Do you think is it a good idea to calculate it in this way?
Thanks

the code:
  1. int main()
  2. {
  3. ifstream fin;
  4. fin.open("numbers.txt");
  5. if (fin.fail())
  6. {
  7. cout << "erorr on opening the file \n";
  8. exit (1);
  9. }
  10. cout << "the caculated average is " << avg_file(fin) << endl;
  11. double calculated_avg = avg_file(fin);
  12. }
  13. double avg_file(ifstream& source_file)
  14. {
  15. double number_in_file;
  16. double total = 0;
  17. int count = 0;
  18. source_file >> number_in_file;
  19. while (! source_file.eof())
  20. {
  21. source_file >> number_in_file;
  22. total = number_in_file + total;
  23. count ++;
  24. }
  25. //average of numbers in file
  26. return (count);
  27. }
Attached Files
File Type: txt numbers.txt (47 Bytes, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: file operation

 
0
  #2
Aug 24th, 2009
double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	while (source_file >> number_in_file)
	{		
		total = number_in_file + total;
		count ++;
	}	
	//average of numbers in file
	return (count);	
}
http://www.daniweb.com/forums/post15...tml#post155265
Last edited by Dave Sinkula; Aug 24th, 2009 at 5:57 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: file operation

 
0
  #3
Aug 24th, 2009
Two things:
First, you forgot to add the first number to the total.
Second, with while(!source_file.eof()) you run through the last line twice.

Therefore, instead of
10+20+40+10+12 = 92
you get
20+40+10+12+12 = 94
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 27
Reputation: esesili is an unknown quantity at this point 
Solved Threads: 0
esesili esesili is offline Offline
Light Poster

Re: file operation

 
0
  #4
Aug 24th, 2009
Thanks for helps. Can you tell me how can I fix it?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,453
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: file operation

 
0
  #5
Aug 24th, 2009
Originally Posted by esesili View Post
Thanks for helps. Can you tell me how can I fix it?
Originally Posted by Dave Sinkula View Post
double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	while (source_file >> number_in_file)
	{		
		total = number_in_file + total;
		count ++;
	}	
	//average of numbers in file
	return (count);	
}
http://www.daniweb.com/forums/post15...tml#post155265
Pay attention.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 244 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC