Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m writing a program in c++ which reads an outside weather file with 365 lines

ID: 672354 • Letter: I

Question

I'm writing a program in c++ which reads an outside weather file with 365 lines (one for each day) in 3 columns. the data is sorted into a string, an int and an int for each row and sorts it's contents into 3 separate arrays. I couldn't use structs, but that part is working. Now I need to find the date of the highest temperature, and I can't seem to get my for loop working correctly. I keep getting a segmentation fault. I also need to compute significant cold fronts, (days 20 degrees (high temp) colder than the previous, the 5 hottest and coldest days, and the 15 median days. I haven't touched those yet, but any advice on how to tackle those would be awesome too. Here's what I got so far:

#include <iostream>

#include <fstream>

using namespace std;

int find_hottest_day(int high_temps[], int);

int main(){

// set size for arrays

const int SIZE = 365;

// initialized arrays

string dates[SIZE];

int high_temps[SIZE];

int low_temps[SIZE];

int hottest_index;

//open in/out files

ifstream in_file;

ofstream out_file;

in_file.open("weather_data.txt");

out_file.open("weather_results.txt");

int i = 0;

//read file line by line, break if the file doesn't open

while(in_file){

in_file >> dates[i];

in_file >> high_temps[i];

in_file >> low_temps[i];

i++;

if(in_file.fail()){

break;

}

}

//print each line

for(int j = 0; j<SIZE; j++){

cout << dates[j] << " " << high_temps[j] << " " << low_temps[j] << endl;

// out_file << dates[j] << " " << high_temps[j] << " " << low_temps[j] << endl;

}

cout << "the hottest day of the year was" << dates[hottest_index];

}

//This function takes the array values from high temps and the Size as parameters.

//The function will find the index of the hottest day of the year.

//The return value is an integer value which is to be plugged in as an index in the dates[] array.

int find_hottest_day(int high_temps[], const int SIZE){

int hottest_index=0;

for(int k = 0; k < SIZE; k++){

if(high_temps[k] > high_temps[hottest_index]){

hottest_index = k;

}

}

return hottest_index;

}

Explanation / Answer

Modified Program:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int find_hottest_day(int high_temps[], int);

int main()
{
const int SIZE = 365;

string dates[SIZE];
int high_temps[SIZE];
int low_temps[SIZE];

int hottest_index;

ifstream in_file;
ofstream out_file;

in_file.open("weather_data.txt");
out_file.open("weather_results.txt");

int i = 0;
string str;

in_file >> str;
while(in_file && i < SIZE)
{
  dates[i] = str;
  in_file >> high_temps[i];
  in_file >> low_temps[i];

  i++;

  in_file >> str;
}

for(int j = 0; j < i; j++)
{
  cout << dates[j] << " " << high_temps[j] << " " << low_temps[j] << endl;
  // out_file << dates[j] << " " << high_temps[j] << " " << low_temps[j] << endl;
}

hottest_index = find_hottest_day(high_temps, i);

cout << "the hottest day of the year was " << dates[hottest_index] << endl;

system("pause");
return 0;
}


int find_hottest_day(int high_temps[], const int SIZE)
{
int hottest_index = 0;

for(int k = 0; k < SIZE; k++)
{
  if(high_temps[k] > high_temps[hottest_index])
  {   
   hottest_index = k;
  }
}

return hottest_index;
}