Write a C++ program: - The text file (city-temps) is a list of weather statistic
ID: 3784323 • Letter: W
Question
Write a C++ program: - The text file (city-temps) is a list of weather statistics for various US cities. Each line has 9 tab-separated fields that can be read using the stream-in operator.
City name
January average temperature in degrees Fahrenheit
April average temperature
July average temperature
October average temperature
Average annual precipitation in inches
Average annual days of rain
Average annual snowfall in inches
--Years of observations used to compile the statistics: rain[/snow]
--The first line in the file is the field names, which can be ignored. Cities with multiple words in their names, such as New York, have the words connected with underscores, New_York, so the city name can be read with a single stream-in request. The only whitespaces in the file are the tab field separators.
Write a C++ program to read the file and calculate the average annual temperature for the city by averaging the four seasonal temperatures. Display the names and temperatures of the cities with the highest and lowest average annual temperatures. The output should look like:
number of cities examined:____
________ has the minumum average annual temperature of _____
________ has the maximum average annual temperature of _____
I am not sure how to upload a text file, so I copied and pasted it below. The text file named as city-temps
City Jan(avg_F) Apr(avg_F) Jul(avg_F) Oct(avg_F) Rain(avg_in) Rain(avg_days) Snow(avg_in) Obs(yrs)
Albany,_N.Y. 22.2 46.6 71.1 49.3 38.60 136 64.4 57
Albuquerque,_N.M. 35.7 55.6 78.5 57.3 9.47 60 11.0 64
Anchorage,_Alaska 15.8 36.3 58.4 34.1 16.08 115 70.8 39/60
Austin,_Texas 50.2 68.3 84.2 70.6 33.65 85 0.9 62/58
Baltimore,_Md. 32.3 53.2 76.5 55.4 41.94 115 21.5 53
Baton_Rouge,_La. 50.1 66.6 81.7 68.1 63.08 110 0.2 52/46
Billings,_Mont. 24.0 46.1 72.0 48.1 14.77 96 56.9 69
Birmingham,_Ala. 42.6 61.3 80.2 62.9 53.99 117 1.5 60
Burlington,_Vt. 18.0 43.5 70.6 47.7 36.05 154 79.3 60
Caribou,_Maine 9.5 38.1 65.6 42.8 37.44 161 112.1 64/63
Charlotte,_N.C. 41.7 60.9 80.3 61.7 43.51 112 5.6 64
Cheyenne,_Wyo. 25.9 41.6 67.7 45.4 15.45 100 55.8 68
Chicago,_Ill. 22.0 47.8 73.3 52.1 36.27 125 38.0 45/44
Cleveland,_Ohio 25.7 47.6 71.9 52.2 38.71 155 57.6 62
Columbia,_S.C. 44.6 63.2 82.0 63.7 48.27 109 1.9 56/55
Columbus,_Ohio 28.3 52.0 75.1 54.7 38.52 137 28.2 64/56
Concord,_N.H. 20.1 44.6 70.0 47.8 37.60 127 64.5 62
Dallas-Ft._Worth,_Texas 44.1 65.0 85.0 67.2 34.73 79 2.6 50/45
Denver,_Colo. 29.2 47.6 73.4 51.0 15.81 89 60.3 61
Dodge_City,_Kan. 30.1 53.9 79.8 57.1 22.35 78 20.3 61
Duluth,_Minn. 8.4 39.0 65.5 43.5 31.00 134 80.6 62/60
El_Paso,_Texas 45.1 64.6 83.3 64.9 9.43 49 5.3 64/57
Fairbanks,_Alaska -9.7 31.7 62.4 23.5 10.34 106 67.7 52
Fargo,_N.D. 6.8 43.5 70.6 45.3 21.19 101 40.8 61
Grand_Junction,_Colo. 26.1 50.9 76.8 52.7 8.99 72 23.6 57
Grand_Rapids,_Mich. 22.4 46.3 71.4 49.9 37.13 144 73.3 40
Hartford,_Conn. 25.7 48.9 73.7 51.9 46.16 128 49.6 49/46
Helena,_Mont. 20.2 44.1 67.8 44.8 11.32 95 46.9 63/58
Honolulu,_Hawaii 73.0 75.6 80.8 80.2 18.29 96 0.0 54/52
Houston,_Texas 51.8 68.5 83.6 70.4 47.84 105 0.4 34/69
Indianapolis,_Ind. 26.5 52.0 75.4 54.6 40.95 126 23.9 64/72
Jackson,_Miss. 45.0 63.4 81.4 64.4 55.95 110 1.0 40/38
Jacksonville,_Fla. 53.1 66.6 81.6 69.4 52.34 116 0.01 62/60
Juneau,_Alaska 25.7 40.8 56.8 42.3 58.33 223 97.0 59
Kansas_City,_Mo. 26.9 54.4 78.5 56.8 37.98 104 19.9 31/69
Knoxville,_Tenn. 37.6 57.8 77.7 58.8 48.22 127 11.5 61/58
Las_Vegas,_Nev. 47.0 66.0 91.2 68.7 4.49 26 1.2 55/48
Lexington,_Ky. 32.0 54.6 76.1 56.6 45.91 130 16.1 59/53
Little_Rock,_Ark. 40.1 61.4 82.4 63.3 50.93 104 5.2 61/56
Long_Beach,_Calif. 57.0 63.0 73.8 68.6 12.94 31 0.01 59/52
Los_Angeles,_Calif. 57.1 60.8 69.3 66.9 13.15 35 0.01 68/62
Louisville,_Ky. 33.0 56.4 78.4 58.5 44.54 124 16.4 56
Madison,_Wisc. 17.3 45.9 71.6 49.3 32.95 120 43.8 55
Memphis,_Tenn. 39.9 62.1 82.5 63.8 54.65 107 5.1 53/49
Miami,_Fla. 68.1 75.7 83.7 78.8 58.53 131 0.01 61/59
Milwaukee,_Wisc. 20.7 45.2 72.0 51.4 34.81 125 47.0 63
Minneapolis-St._Paul,_Minn. 13.1 46.6 73.2 48.7 29.41 115 49.9 65/62
Mobile,_Ala. 50.1 66.1 81.5 67.7 66.29 121 0.4 62/61
Newark,_N.J. 31.3 52.3 77.2 56.4 46.25 122 28.3 62
New_Orleans,_La. 52.6 68.2 82.7 70.0 64.16 114 0.2 55/51
New_York,_N.Y. 32.1 52.5 76.5 56.6 49.69 121 28.6 134/135
Thank you so much for helping.
Explanation / Answer
//Following is C++ program for given problem statement :
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;
int main()
{
ifstream infile;
infile.open("city-temps.txt"); // opening city-temps file in read mode
string name,line;
string max_name;
string min_name;
double a, b,c,d,avg,min_ans=numeric_limits<double>::max(); //initializing min_ans to maximum value
double max_ans=numeric_limits<double>::min(); //initializing max_ans to minimum value
int count=0;
while(getline(infile,line))
{
count++;
istringstream iss(line);
iss >> name >> a >> b >> c >> d;
avg=a+b+c+d; //addition of all temperature
avg=avg/4; //average
if(min_ans>avg){min_ans=avg; min_name=name;}
if(max_ans<avg){max_ans=avg; max_name=name;}
}
cout<<"number of cities examined :"<<count<<" ";
cout<<min_name<<" has the minimum average annual temperature of "<<min_ans<<" ";
cout<<max_name<<" has the minimum average annual temperature of "<<max_ans<<" ";
}
Output :
G580:~$ g++ temread.cpp
G580:~$ ./a.out
number of cities examined :51
Fairbanks,_Alaska has the minimum average annual temperature of 26.975
Honolulu,_Hawaii has the minimum average annual temperature of 77.4
G580:~$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.