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

Instructions Write a C++ program to determine the average temperature for a four

ID: 3702759 • Letter: I

Question

Instructions

Write a C++ program to determine the average temperature for a four-day holiday weekend in various cities in Arizona, and display the highest and lowest temperatures for the state.

Requirements:

In main have the user input a text file name, then have the user repeatedly:

-input the city name (using getline) followed by 4 temperatures. You do not know how many cities the user will be entering.

         ->input validation: the temperature entered must be 175 or below and -50 or above.

-use function Calc_Avg_Temp to calculate the average temperature for the city, return it to main and display it there (to 1 decimal).

-use a second function to find the highest temperature for the city, return it to main via a reference variable and display it in main.

-use a third function to find the lowest temperature for the city, also return it to main via a reference variable and display it in main.

    -in main write the city name, high temperature and low temperature to the text file.

    -process another city (your program should use a loop that terminates only when the user enters the word “done”).

In a fourth function read the file to determine the city with the highest temperature and the city with the lowest temperature, return the two city names and temperatures to main using reference variables, and display, with appropriate descriptions, these two cities and their temperatures in main.

Data to use for screen prints:

Friday

Saturday

Sunday

Monday

Tucson

80

81

83

87

Phoenix

81

80

84

89

Flagstaff

56

63

65

67

Yuma

78

80

85

90

Submit:

Your .cpp file (not zipped) from within the project folder

Screen shot (s) of your output

Friday

Saturday

Sunday

Monday

Tucson

80

81

83

87

Phoenix

81

80

84

89

Flagstaff

56

63

65

67

Yuma

78

80

85

90

Explanation / Answer

#include <iostream>

#include <fstream>

#include <sstream>

using namespace std;

void high_And_low(string f_name,string *h_city,string *l_city,float *h_temp,float *l_temp)

{

ifstream in(f_name.c_str());

string s;

string city;

float temp1;

float temp2;

string inp[3];

int next_in_pos ;

int i = 0;

while(getline(in,s))

{

next_in_pos = 0;

int pre = 0;

int pos = 0;

while(s[pos])

{

if(s[pos]==' ')

{

inp[next_in_pos] = s.substr(pre,pos-pre);

pre = pos+1;

next_in_pos++;

}

pos++;

if(s[pos]=='')

{

inp[next_in_pos] = s.substr(pre,pos-pre);

next_in_pos++;

}

}

if(i==0)

{

city = inp[0];

stringstream value0(inp[1]);

value0 >> temp1;

stringstream value1(inp[2]);

value1 >> temp2;

*h_temp = temp1;

*l_temp = temp2;

*h_city = city;

*l_city = city;

}

else

{

city = inp[0];

stringstream value2(inp[1]);

value2 >> temp1;

stringstream value3(inp[2]);

value3 >> temp2;

if(temp1 > *h_temp)

{

*h_temp = temp1;

*h_city = city;

}

if(temp2 < *l_temp)

{

*l_temp = temp2;

*l_city = city;

}

}

i++;

}

}

float Cal_Avg_Temp(float *temp,int n)

{

int i;

float sum = 0.0;

float average;

for(i=0;i<n;i++)

{

sum+=temp[i];

}

average = sum/n;

return average;

}

void find_Highest_Temp(float *temp,int n,float *high_temp)

{

int i;

float max = temp[0];

for(i=1;i<n;i++)

{

if(temp[i]>max)

{

max = temp[i];

}

}

*high_temp = max;

}

void find_Lowest_Temp(float *temp,int n,float *low_temp)

{

int i;

float min = temp[0];

for(i=1;i<n;i++)

{

if(temp[i]<min)

{

min = temp[i];

}

}

*low_temp = min;

}

int main()

{

cout<<"Enter File To write : ";

string f_name ;

getline(cin,f_name);

ofstream out_file;

out_file.open(f_name.c_str());

string s ;

cout<<"Enter City and 4 Tempratures "<<endl;

getline(cin,s);

while(s.compare("done")!=0)

{

int pos = 0;

int pre = 0;

string inp[6];

int next_in_pos = 0;//position where to insert in inp array

while(s[pos])

{

if(s[pos]==' ')

{

inp[next_in_pos] = s.substr(pre,pos-pre);

pre = pos+1;

next_in_pos++;

}

pos++;

if(s[pos]=='')

{

inp[next_in_pos] = s.substr(pre,pos-pre);

next_in_pos++;

}

}

int i;

if(next_in_pos==5)

{

float temp[4];

int next_temp_pos = 0;

bool allow = true;

string city;

for(i = 0;i<next_in_pos && allow;i++)

{

if(i>0)

{

stringstream value(inp[i]);

value >> temp[next_temp_pos];

if(temp[next_temp_pos]<(float)-50 || temp[next_temp_pos]>(float)175)

{

allow = false;

}

next_temp_pos++;

}

else

{

city = inp[i];

}

}

if(allow)

{

float high_temp;

float low_temp;

string h_temp,l_temp;//to store respective float to string because only strings of chars are allowed to write to a file

ostringstream f_to_str,f_to_str1;

printf("Average = %.1f ",Cal_Avg_Temp(temp,4));

find_Highest_Temp(temp,4,&high_temp);

find_Lowest_Temp(temp,4,&low_temp);

f_to_str<<high_temp;

h_temp = f_to_str.str();

f_to_str1<<low_temp;

l_temp = f_to_str1.str();

cout<<"High Temp = "<<high_temp<<endl;

cout<<"Low Temp = "<<low_temp<<endl;

out_file<<city<<" "<<h_temp<<" "<<l_temp<<endl;

}

else

{

cout<<"Temprature should be between -50 to 175 (inclusive)"<<endl;

}

}

else

{

cout<<"Please Enter city name and 4 Tempratures"<<endl;

}

cout<<"Enter City and 4 Tempratures "<<endl;

getline(cin,s);

}

string h_city,l_city;

float hi_temp;

float lo_temp;

high_And_low(f_name,&h_city,&l_city,&hi_temp,&lo_temp);

cout<<"=========================== High Temprature city = "<<h_city<<" with temp = "<<hi_temp<<endl;

cout<<"Low Temprature city = "<<l_city<<" with temp = "<<lo_temp<<endl;

out_file.close();

return 0;

}