Help with calculating avrg from input file.

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

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

Help with calculating avrg from input file.

 
0
  #1
Mar 25th, 2007
Hi fellow c++ programmers,

I need some help with an assignment from my c++ class. The assignment is to use a input file with the following

1 4/12/2007 34 -- 1 is the reference number, date , and temperature to be converted to farenheit.

Now i have to output it to a .txt file that looks like this:

CELCIUS TO FAHRENHEIT CONVERSION
----------------------------------
REF. DATE CONVERSION
1 4/12/2007 1°F
2 4/12/2007 1°F
3 4/12/2007 2°F
4 4/12/2007 3°F
6 4/12/2007 4°F
7 4/12/2007 5°F
8 4/12/2007 6°F
9 4/12/2007 6°F
10 4/12/2007 7°F
11 4/12/2007 7°F
12 4/12/2007 8°F
13 4/12/2007 9°F
14 4/12/2007 10°F
15 4/12/2007 10°F
16 4/12/2007 11°F
17 4/12/2007 11°F
18 4/12/2007 12°F
19 4/12/2007 13°F
20 4/12/2007 13°F
21 4/12/2007 14°F
22 4/12/2007 15°F
23 4/12/2007 15°F
24 4/12/2007 16°F
----------------------------------
Total temperature average:
my problem is, that i have been trying to use a counter to display the average and im stumped... simply put i have no idea how to do it and i cant seem to find information on how to do it anywhere. Heres the code i have done so far, any help or explanation is appreciated.


  1. #include <iostream.h>
  2. #include <fstream.h>
  3. int Celsius(int); //Celsius conversion formula function
  4. int main()
  5. {
  6. // Variables Setup
  7. int ref, ctemp, myloop;
  8. char mydate[9];
  9. //File Management
  10. ifstream inFile; //Input File Logical Name
  11. ofstream outFile; //Output File Logical Name
  12. inFile.open("H:\\1.dat"); //Input File Merge Setup
  13. outFile.open("H:\\2.txt"); //Output File Merge Setup
  14. inFile >> ref >> mydate >> ctemp; // Read File
  15.  
  16. //Headings
  17. outFile << "\tCELCIUS TO FAHRENHEIT CONVERSION" << endl;
  18. outFile << "\t";
  19. for (myloop=1; myloop<35; myloop++)
  20. {outFile << "-";} // Dash Line Print Cycle
  21. outFile << endl;
  22. outFile << "\tREF." << "\tDATE" << "\tCONVERSION" << endl << endl;
  23. // End of Headings
  24. while (inFile) //End-ofFile Controlled Loop
  25. {
  26. outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
  27. << "°F" << endl << endl; // Write Record
  28. inFile >> ref >> mydate >> ctemp; // Read File
  29. }
  30. outFile << "\t";
  31. for (myloop=1; myloop<35; myloop++)
  32. {outFile << "-";} // Dash Line Print Cycle
  33. outFile << endl;
  34. outFile << "\tTotal temperature average:" << endl;
  35. int number;
  36. int average;
  37. int count = 0;
  38. {
  39. ctemp += number;
  40. count++;
  41. }
  42. average = ctemp / count;
  43.  
  44. return 0;
  45. }
  46.  
  47. int Celsius(int ctemp) // Celsius to Fahrenheit Conversion Function
  48. {
  49. int ctof = 0;
  50. ctof = (ctemp - 32) * 5/9;
  51. ctemp = ctof;
  52. return ctemp;
  53. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 287
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 29
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz in Training

Re: Help with calculating avrg from input file.

 
0
  #2
Mar 25th, 2007
Look at your output table.... do you see anything in your code you could use as a counter? (hint: your days are numbered)
Last edited by Clinton Portis; Mar 25th, 2007 at 7:40 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
  #3
Mar 25th, 2007
>my problem is, that i have been trying to use a counter to display the average and im stumped
The problem is that you need to store the data in some sort of array -- preferably a vector, and then you can calculate the average.

Vectors are nice, because they can dynamically resize. I suggest you read up on them here:
http://www.cprogramming.com/tutorial/stl/vector.html
"Technological progress is like an axe in the hands of a pathological criminal."
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
  #4
Mar 26th, 2007
how would i apply the vector to my code?? by using vector <int>; and including all the reference numbers or is there a more simple way because i really dont understand the explanation that is in that link. Thanks for the replies!
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
  #5
Mar 26th, 2007
>how would i apply the vector to my code??
Basically keep pushing values into the vector as you read in new temperatures. Then outside of the loop, you would read them out and add up the total, yada yada.
"Technological progress is like an axe in the hands of a pathological criminal."
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
  #6
Mar 26th, 2007
Ok, so it would go something like this :

  1. {
  2. vector <int> num; //Vector to store integers
  3. num.push_back(1);
  4. num.push_back(2);
  5. num.push_back(3);
  6. //**etc. till 24
  7. for(int x=0; x<num.size(); x++)
  8. {
  9. cout << num[x]/24 << " ";
  10. }
another question which values should i be pushing into the
vector from my input file, the temperatures in themselves
or just the reference numbers as above? or...am i going
completely wrong about this which i feel i am... argh! some
things are so easy and yet some things are so hard.
Last edited by swordy06; Mar 26th, 2007 at 1:42 am.
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
  #7
Mar 26th, 2007
No... insert it here:
  1. while (inFile) //End-ofFile Controlled Loop
  2. {
  3. // push_back 'ctemp' here
  4.  
  5. outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
  6. << "°F" << endl << endl; // Write Record
  7. inFile >> ref >> mydate >> ctemp; // Read File
  8. }

>which values should i be pushing into the vector from my input file, the temperatures in themselves
Yes, the temperatures. You don't need to worry about the reference numbers; vectors have their own way of referencing values. Remember, you want to calculate the average of the temperatures, so that's what you're going to need to store, not anything else.

Once you're out of the loop, create another one to calculate the sum of all the temperatures, and then divide this total by the number of temperatures, which ends up being your average.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help with calculating avrg from input file.

 
0
  #8
Mar 26th, 2007
Originally Posted by joeprogrammer View Post
>my problem is, that i have been trying to use a counter to display the average and im stumped
The problem is that you need to store the data in some sort of array -- preferably a vector, and then you can calculate the average.
Why? Go simple.
In this loop:
  1. while (inFile) //End-ofFile Controlled Loop
  2. {
  3. outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
  4. << "°F" << endl << endl; // Write Record
  5. inFile >> ref >> mydate >> ctemp; // Read File
  6. }
Simply add ctemp to a total counter and add 1 to a 'lines read' counter before your output. Then when you exit the loop, calculate total/lines. No need to store all the data unless you need it later in the program.
Last edited by WaltP; Mar 26th, 2007 at 3:55 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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
  #9
Mar 26th, 2007
>No need to store all the data unless you need it later in the program.
It's good to plan ahead. I prefer doing things harder the first time if it makes future options easier. Now say you want to add some extra features to the program using the data you collected, wouldn't it be way easier if you already had done the groundwork?

But I see your point, and perhaps it's better in this case to calculate the average on-the-spot, since this is just a homework assignment...
"Technological progress is like an axe in the hands of a pathological criminal."
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
  #10
Mar 27th, 2007
Originally Posted by WaltP View Post
Simply add ctemp to a total counter and add 1 to a 'lines read' counter before your output. Then when you exit the loop, calculate total/lines. No need to store all the data unless you need it later in the program.
sorry for replying so late, ive been kinda busy with other schoolwork,
so it would be something like this
  1. while (inFile)
  2. {
  3.  
  4. int total = ctemp;
  5. (total=1; total<25; total++)
  6. outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
  7. << "°F" << endl << endl; // Write Record
  8. inFile >> ref >> mydate >> ctemp; // Read File
  9. }
where would i put the lines counter or how would i apply it to here??
Last edited by swordy06; Mar 27th, 2007 at 5:00 pm.
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
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC