How do you find the max/min without arrays

May 31, 2010 at 1:14am
Hi, this forum was very helpful earlier. hopefully you guys can help again. I have to convert the temperatures from farenheight to celcius and then find the max/min temperatures from a .txt file without using arrays. And then output the new temperatures on a .txt file. This is what i have so far.

#include<iostream>
#include<fstream>
#include<cmath>
#include <iomanip>

using namespace std;

main()

{
double tempF=0, cnt=0, tempC=0, sum=0, average=0, min=0, max=0;

ifstream fin("dataF.txt");

while(fin>>tempF)
{

cnt++;
tempC=(5/9.0)*(tempF-32);
cout<<tempC<<endl;
sum+=tempC;
average= sum/cnt;

}

cout<< "The maximum temperature is "<<max<<endl;
cout<< "The minimum temperature is "<<min<<endl;
cout<< "The average temperature is " <<average<<endl;

ofstream fout("dataC.txt");
fout << tempC;
fout.close();

system("PAUSE");
return 0;
}


any tips on how to find the max/min. ive tried for hours. thanks!!!

May 31, 2010 at 1:17am
Just initialize min to a high value and max to a low value, then keep track of the smallest/highest value while you read the values.
May 31, 2010 at 1:27am
what do you mean by initialize min to a high value and max to a low value?
May 31, 2010 at 1:44am
Values that are guaranteed to be higher or lower than any value in your file. Just use DBL_MAX and -DBL_MAX for that.
That's not really a programming problem... if someone read 1000 numbers to you, how would you figure out the highest and the lowest? Yeah, it's not any different here.
May 31, 2010 at 1:57am
You could save the first value you read into two variables (max and min) and compare each value as you read. If the value < min, then min = value. Same principle for max.
Topic archived. No new replies allowed.