| | |
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:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> int Celsius(int); //Celsius conversion formula function int main() { // Variables Setup int ref, ctemp, myloop; char mydate[9]; //File Management ifstream inFile; //Input File Logical Name ofstream outFile; //Output File Logical Name inFile.open("H:\\1.dat"); //Input File Merge Setup outFile.open("H:\\2.txt"); //Output File Merge Setup inFile >> ref >> mydate >> ctemp; // Read File //Headings outFile << "\tCELCIUS TO FAHRENHEIT CONVERSION" << endl; outFile << "\t"; for (myloop=1; myloop<35; myloop++) {outFile << "-";} // Dash Line Print Cycle outFile << endl; outFile << "\tREF." << "\tDATE" << "\tCONVERSION" << endl << endl; // End of Headings while (inFile) //End-ofFile Controlled Loop { outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp) << "°F" << endl << endl; // Write Record inFile >> ref >> mydate >> ctemp; // Read File } outFile << "\t"; for (myloop=1; myloop<35; myloop++) {outFile << "-";} // Dash Line Print Cycle outFile << endl; outFile << "\tTotal temperature average:" << endl; int number; int average; int count = 0; { ctemp += number; count++; } average = ctemp / count; return 0; } int Celsius(int ctemp) // Celsius to Fahrenheit Conversion Function { int ctof = 0; ctof = (ctemp - 32) * 5/9; ctemp = ctof; return ctemp; }
>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
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."
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
Ok, so it would go something like this :
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.
C++ Syntax (Toggle Plain Text)
{ vector <int> num; //Vector to store integers num.push_back(1); num.push_back(2); num.push_back(3); //**etc. till 24 for(int x=0; x<num.size(); x++) { cout << num[x]/24 << " "; }
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.
No... insert it here:
>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.
C++ Syntax (Toggle Plain Text)
while (inFile) //End-ofFile Controlled Loop { // push_back 'ctemp' here outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp) << "°F" << endl << endl; // Write Record inFile >> ref >> mydate >> ctemp; // Read File }
>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."
•
•
•
•
>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.
In this loop:
c Syntax (Toggle Plain Text)
while (inFile) //End-ofFile Controlled Loop { outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp) << "°F" << endl << endl; // Write Record inFile >> ref >> mydate >> ctemp; // Read File }
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
>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...
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."
•
•
Join Date: Mar 2007
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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.
so it would be something like this
C++ Syntax (Toggle Plain Text)
while (inFile) { int total = ctemp; (total=1; total<25; total++) outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp) << "°F" << endl << endl; // Write Record inFile >> ref >> mydate >> ctemp; // Read File }
Last edited by swordy06; Mar 27th, 2007 at 5:00 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Binary Recursion Function Errors - Help!!
- Next Thread: Structure in Maps
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






