Help with calculating avrg from input file.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help with calculating avrg from input file.

 
0
  #11
Mar 27th, 2007
How about:
  1. int total=0;
  2.  
  3. while (inFile) {
  4. total += ctemp;
  5. // ....
  6. }
Last edited by John A; Mar 27th, 2007 at 7:12 pm. Reason: whoops, forgot to initalize total!
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: swordy06 is an unknown quantity at this point 
Solved Threads: 0
swordy06 swordy06 is offline Offline
Newbie Poster

Re: Help with calculating avrg from input file.

 
0
  #12
Mar 27th, 2007
ok. almost there, what about the line counter waltp suggested so that i could calculate the average?? how do i incorporate that
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help with calculating avrg from input file.

 
0
  #13
Mar 27th, 2007
Originally Posted by swordy06 View Post
ok. almost there, what about the line counter waltp suggested so that i could calculate the average?? how do i incorporate that
Here's a start:
  1. int counter=0; // increment this inside the loop, add +1 after the loop
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: swordy06 is an unknown quantity at this point 
Solved Threads: 0
swordy06 swordy06 is offline Offline
Newbie Poster

Re: Help with calculating avrg from input file.

 
0
  #14
Mar 27th, 2007
  1. int total=0;
  2. int count=0;
  3. while (inFile) //End-ofFile Controlled Loop
  4. {
  5. total += ctemp;
  6. (total=1; total<25; total++)
  7. outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
  8. << "°F" << endl << endl; // Write Record
  9. inFile >> ref >> mydate >> ctemp; // Read File
  10. (count=1; count<25; )
  11. }
  12. count++;
  13. average= total/count;

it should be something like that, i imagine, i must be bugging the hell outta u joeprogrammer, for that im sorry:rolleyes:. Its simply because up till now i havent done any programs that require counters of any sort, just very basic stuff, and the logic escapes me but thanks for your patience.
Last edited by swordy06; Mar 27th, 2007 at 9:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help with calculating avrg from input file.

 
0
  #15
Mar 27th, 2007
Put the incrementation inside the loop. And what are these lines supposed to do?
  1. (total=1; total<25; total++)
  2. (count=1; count<25; )

Here's how it should look:
  1. int total=0;
  2. int count=0;
  3. while (inFile) //End-ofFile Controlled Loop
  4. {
  5. total += ctemp;
  6. ++count;
  7. outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
  8. << "°F" << endl << endl; // Write Record
  9. inFile >> ref >> mydate >> ctemp; // Read File
  10. }
  11. ++count;
  12. average= total/count;

And you don't need to worry about bugging me, we were all newbies once.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: swordy06 is an unknown quantity at this point 
Solved Threads: 0
swordy06 swordy06 is offline Offline
Newbie Poster

Re: Help with calculating avrg from input file.

 
0
  #16
Mar 27th, 2007
  1. (total=1; total<25; total++)
  2. (count=1; count<25; )
I thought i had to define the number of lines within the input file, and i used that from another example code that i saw,
thats probably not the right way to do it. In other words i thought that was the incrementation or that thats how you do it
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: swordy06 is an unknown quantity at this point 
Solved Threads: 0
swordy06 swordy06 is offline Offline
Newbie Poster

Re: Help with calculating avrg from input file.

 
0
  #17
Mar 27th, 2007
nevermind those 2 lines, it didnt make sense to include them, ok, i have a problem, the counter runs fine, but it is taking the temperatures from the input file, i need it to count the temperatures from the output file since those are the temperatures already converted how can i make the counter so that it counts the already converted temperatures??

I imagine it might be done with another decision?

while (inFile)

{ total += ctemp // .....
Last edited by swordy06; Mar 27th, 2007 at 11:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help with calculating avrg from input file.

 
0
  #18
Mar 27th, 2007
Originally Posted by swordy06 View Post
  1. (total=1; total<25; total++)
  2. (count=1; count<25; count++)
I thought i had to define the number of lines within the input file, and i used that from another example code that i saw,
thats probably not the right way to do it.
You were taking a section from a for() loop. A typical for() loop looks like this:
  1. for (int i=0; i < x; ++i) {
  2.  
  3. // blah blah
  4. }

The part that actually does the incrementation is the last statement, ++i. All the rest is just loop-related stuff.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: swordy06 is an unknown quantity at this point 
Solved Threads: 0
swordy06 swordy06 is offline Offline
Newbie Poster

Re: Help with calculating avrg from input file.

 
0
  #19
Mar 27th, 2007
i have a problem, the counter runs fine, but it is taking the temperatures from the input file, i need it to count the temperatures from the output file since those are the temperatures already converted how can i make the counter so that it counts the already converted temperatures??

I imagine it might be done with another decision?

while (inFile)

{ total += ctemp // .....
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Help with calculating avrg from input file.

 
0
  #20
Mar 28th, 2007
Convert the amount to celsius before you add it onto the total. (not tested)
  1. int total=0;
  2. int count=0;
  3. int celsiusTemp;
  4. while (inFile) //End-ofFile Controlled Loop
  5. {
  6. celsiusTemp = Celsius(ctemp); // convert it to celsius beforehand
  7. total += celsiusTemp;
  8. ++count;
  9.  
  10. // don't have to call 'Celsius' here, use the variable 'celsiusTemp'
  11. outFile << "\t" << ref << "\t" << mydate << "\t" << celsiusTemp
  12. << "°F" << endl << endl; // Write Record
  13. inFile >> ref >> mydate >> ctemp; // Read File
  14. }
  15. ++count;
  16. average= total/count;
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 3135 | Replies: 21
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC