How To Work Out This Part

I created the 3 functions already but I'm not sure how to do these two parts of the question.

PART 1: Read all the data in the temp.txt file and store the minimum temperature in one array and the maximum temperature in another array. The temperature should be stored as Fahrenheit values. While reading the file, your program should find and display the highest and lowest temperatures for the given year.

PART 2:
After reading the file, your program should find and display:
i. Two days in March when the temperature was the highest.
ii. The day in December which had the lowest temperature.
iii. The day in September when the difference between the maximum and minimum
temperature was the highest for that month.
iv. The average minimum temperature and the average maximum temperature for the month
of February; also, the days when the maximum temperature went above the average.


THIS IS WHAT I HAVE SO FAR:

float toFahrenheit(float temp){
float Fahr;
Fahr=((temp*1.0)*9/5)+32;
return Fahr;
}

bool isLeapYear(int year){
if((year%400==0)||(year%4==0)&&(year%100!=0))
return true;
return false;
}

int startIndex(int year, int mnth){
int index[12]= {0,31,59,90,120,151,181,212,243,273,304,334};
int leap_index[12] = {0,31,60,91,121,152,182,213,244,274,305,335};
if (isLeapYear (year)==true)
return leap_index[mnth-1];
else
return index[mnth -1];
}

#include <fstream>
#include<iostream>
using namespace std;
int main (){
int c,i,year;
int highest=0;
int lowest=9999;
int temp[366];
int min_temp[c];
int max_temp[c];


ifstream in;
ofstream out;

in.open("temp.txt");
out.open("results.txt");

if(!in.is_open()){
cout<<"Error Opening File..... ABORT";
return 2;
}
Last edited on
You created toFahrenheit but why?
> The temperature should be stored as Fahrenheit values
And what temperature scale is stored in the input file?

You created isLeapYear but why?

What you haven't done is make any attempt at reading from the file.
1
2
3
4
5
6
7
8
9
10
11
12
int main ( ) {
  ifstream fin("temp.txt");
  int min_temp, max_temp;  // or are they floats?
  int daynum = 0;
  while ( fin >> min_temp >> max_temp ) {
    daynum++;
    cout << "Day=" << daynum
         << ", low=" << min_temp
         << ", high=" << max_temp
         << endl;
  }
}

Make sure this works first.

Then examine your requirements and figure out an order to add them to your code.

For example, this seems like an easy thing to add next.
"While reading the file, your program should find and display the highest and lowest temperatures for the given year."


Remember, the aim of the game is to make small changes and keep testing as you go. Make sure each new added feature works before moving on.



DO NOT try to write all the requirements at the same time - you will fail.
I created toFahrenheit and isLeapYear because it was part of the question before.

a) Write a function toFahrenheit, which accepts a temperature in degrees Celsius (oC) and
returns the equivalent temperature in degrees Fahrenheit (oF) using the formula,
oF = (oC * 9/5) + 32.

b) Write a function isLeapYear which accepts a year as a parameter (an integer value) and
returns 1 (or true) if year is a leap year and 0 (or false) otherwise. A leap year is one that is
either:
Divisible by 400
Divisible by 4 but not by 100
For example, 2000, 2004, and 2016 were leap years but 2015, 1900, and 1800 were not.

c) Using the isLeapYear function, write a function startIndex which accepts a year and a month
as parameters (integer values) and returns the starting location for that month in the arrays
which store the temperature values. Note that the first day of the year is stored in location 0
and that February could have either 28 or 29 days.


The temperature stored in the file is in Celsius so we have to use the toFahreneit to convert it to fahreneit and store that value in the arrays of min and max
Last edited on
Right.
So how's the file reading coming along?
I'm getting through the file reading. I'm trying to figure out how I'm storing the maximum temps and minimum temps into the two arrays. I also have to use the toFahreneheit function to have the maximum and minimum temps stored in the arrays in Fahrenheit n not Celsius
So post what you have so far.
Topic archived. No new replies allowed.