you need to create a C/C++ struct to contain the 6 values for each city (name, h
ID: 666125 • Letter: Y
Question
you need to create a C/C++ struct to contain the 6 values for each city (name, high, low, forecast, forecased-high and forecasted-low). So the weather information will be stored in an array of structs rather than 6 different arrays.
You will also need to create the array as a dynamic array. Initially, the array may contain spaces for 10 cities. As you are reading in the data, the amount of data able to be stored should double everytime the array gets full.
Since we don't know the size of the file or which file will contain the information. Your program should first prompt the user for the name of the file to read the data. Below are three sample data files:
weatherdata.txt
weatherdata2.txt
weatherdata3.txt
This data file contains information in the following format
ALLENTOWN 90 72 PTCLDY 89 61
AMARILLO 88 65 TSTRMS 87 67
ANCHORAGE 74 58 PTCLDY 68 54
ASHEVILLE 89 68 MOCLDY 87 67
ATLANTA 93 77 PTCLDY 93 75
The file contains the following information:
The first column of data contains the name of a city. The city name is no more than 15 characters long.
The second column of data contains an integer showing the current days high temperature.
The third column contains an integer showing the current days low temperature.
The fourth column shows the weather forecast for the next day.
The fifth column contains an integer showing the next day's forecasted high temperature.
The last column contains an integer showing the next day's forecasted low temperature.
You are to first write a function that will open up the data file, read in the information until the end of the file is reached and store the data read on each line into the array.
You are to compute and print out the following information from the array. Each of these operations MUST be done in its own function. Note some of the below operations are the same as Lab Assignment 6, but some are not the same.
1. Print out the average high and low temperatures for all of the cities listed 2. Print out the name of the city (or cities) with the highest high temperature. 3. Print out the names of the cities with a low temperature that is less than the average low temperature. 4. Print out all cities that have a 10 degree or more difference between the current days low temperature and the forecast days low temperature. Also print out the degree difference between the low temperatures for each city. 5. For all cities that have a "TSTRMS" or a "SHWRS" forecast, print out the city names who have a forecast high temperature that is higher than the current days average high temperature for all cities. 6. For all cities that have a "SUNNY" forecast, print out the average forecast high temperature and the average forecast low temperature.Explanation / Answer
#include <cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<cstring>
using namespace std;
struct weather
{
char city[21],fc[20];
int high,low,fchigh,fclow;
};
void printAvgHighLow(vector <weather> v);
void printHigh(vector <weather> v);
void printLessThanAvg(vector <weather> v);
void printDiffmorethan10(vector<weather> v);
void printavgofSUNNY(vector <weather> v);
int main()
{
string name;
struct weather temp;
cout<<"enter the file name ";
cin>>name;
ifstream in(name, ios::in);
vector <weather> report;
while(in>>temp.city)
{
in>>temp.high;
in>>temp.low;
in>>temp.fc;
in>>temp.fchigh;
in>>temp.fclow;
report.push_back(temp);
}
printAvgHighLow(report);
printHigh(report);
printLessThanAvg(report);
printDiffmorethan10(report);
printavgofSUNNY(report);
return 0;
}
void printAvgHighLow(vector <weather> v)
{
int i;
for(i=0;i<v.size();i++)
{
cout <<"Name = "<<v[i].city<<" High ="<<v[i].high<<" Low="<<v[i].low<<" average="<<(v[i].high+v[i].low)/2.0<<endl;
}
}
void printHigh(vector <weather> v)
{
int i,max=0,maxi=0;
for(i=0;i<v.size();i++)
{
if(v[i].high>max)
{
max=v[i].high;
maxi=i;
}
}
cout<<v[maxi].city<<endl;
}
void printLessThanAvg(vector <weather> v)
{
float avg,sum=0;
for(int i=0;i<v.size();i++)
{
sum+=v[i].low;
}
avg = sum/v.size();
for(int i=0;i<v.size();i++)
{
if(v[i].low<avg)
cout<<v[i].city<<endl;
}
}
void printDiffmorethan10(vector<weather> v)
{
int temp;
for(int i;i<v.size();i++)
{
temp = abs(v[i].low-v[i].fclow);
if(temp > 10)
cout<<v[i].city<<" difference= "<<temp<<endl;
}
}
void printavgofSUNNY(vector <weather> v)
{
float suml=0,sumh=0,avgl,avgh;
int i;
for(i=0;i<v.size();i++)
{
if(!strcmp(v[i].fc,"SUNNY"))
{
suml+=v[i].fclow;
sumh+=v[i].fchigh;
}
}
avgl=suml/v.size();
avgh=sumh/v.size();
cout<<"average foract low = "<<avgl<<endl;
cout<<"average forecast high = "<<avgh<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.