943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3726
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 25th, 2007
0

Help with calculating avrg from input file.

Expand Post »
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:

Quote ...
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.


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swordy06 is offline Offline
10 posts
since Mar 2007
Mar 25th, 2007
0

Re: Help with calculating avrg from input file.

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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Mar 25th, 2007
0

Re: Help with calculating avrg from input file.

>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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 26th, 2007
0

Re: Help with calculating avrg from input file.

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swordy06 is offline Offline
10 posts
since Mar 2007
Mar 26th, 2007
0

Re: Help with calculating avrg from input file.

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 26th, 2007
0

Re: Help with calculating avrg from input file.

Ok, so it would go something like this :

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swordy06 is offline Offline
10 posts
since Mar 2007
Mar 26th, 2007
0

Re: Help with calculating avrg from input file.

No... insert it here:
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 26th, 2007
0

Re: Help with calculating avrg from input file.

>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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Mar 26th, 2007
0

Re: Help with calculating avrg from input file.

>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...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 27th, 2007
0

Re: Help with calculating avrg from input file.

Click to Expand / Collapse  Quote originally posted by WaltP ...
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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
swordy06 is offline Offline
10 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Binary Recursion Function Errors - Help!!
Next Thread in C++ Forum Timeline: Structure in Maps





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC