The data file that we are supposed to use is below: For these two assignments (P
ID: 3812606 • Letter: T
Question
The data file that we are supposed to use is below:
For these two assignments (Program 12 and Program 13), you will use the same data file but your programs will process the file in different ways. Data file weather data tot Notepad File Edit Format View Help 14 25 12 31 35 11 33 21 35 The data file linked for the assignment is 12 25 weatherdata.txt 13 34 32 This file contains wind speed and temperature 10 13 28 data for 30 days. 11 6 32 12 13 29 The first value on each line is the day day 1, day 13 6 28 2, day 3, up to day 14 35 15 25 The second value on the line is the wind speed in 16 6 28 knots 14 knots for day 1, 12 knots for day 2, etc., 17 34 up to 14 knots for day 30. 18 8 21 19 31 The third value on the line is the temperature in 20 15 22 Celsius 25 c for day 1, 31 c for day 2, etc., up to 21 5 35 22 c for day 30. 22 6 33 23 12 27 24 15 27 25 12 34 26 14 23 27 14 22 28 5 21 29 29 30 14 22 For Programs 12 and 13, you will read this data and process it to produce the average wind speed and the average temperature once without using arrays and once with using arrays. n either case your output can be very simple C:UsersVillDesktopMiami Programming Languages C++... he average of the wind speeds in the file is 9 he average of the temperatures in the file is 28 See the details for each below.Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
#include<stdlib.h>
using namespace std;
int main () {
char filename[20];
ifstream inFile;
float speed,temperature,avgspeed=0,avgtemp=0;
int count,getcount;
cout<<"Enter the filename ";
cin>>filename;
inFile.open(filename);//open the file
if (inFile.fail())//error handlig
{
cout<<"Input filename cannot be found ";
exit(1);
}
inFile>>count>>speed>>temperature;//get the details from file
while(inFile.good())
{
//calculate on the go
avgspeed=avgspeed+speed;
avgtemp=avgtemp+temperature;
getcount=count;
cout<<count<<" "<<speed<<" "<<temperature<<endl;
inFile>>count>>speed>>temperature;
}inFile.close();//close the file after reading
avgspeed=avgspeed/getcount;
avgtemp=avgtemp/getcount;
cout << fixed; //set the precision after decimal to 2 values
cout <<setprecision(2);
cout<<"The average of the wind speed in the file is "<<avgspeed<<endl;
cout<<"The average of the temperatures in the file is "<<avgtemp<<endl;
}
/***********soring into array**********/
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
#include<stdlib.h>
using namespace std;
const int SIZE=30;
int main () {
char filename[20];
ifstream inFile;
float speedarray[SIZE],temparray[SIZE],speed,temperature, avgspeed=0.0,avgtemp=0.0;
int count;
cout<<"Enter the filename ";
cin>>filename;
inFile.open(filename);//open the file
if (inFile.fail())//error handlig
{
cout<<"Input filename cannot be found ";
exit(1);
}
inFile>>count>>speed>>temperature;//get the details from file
while(inFile.good())
{
speedarray[count-1] = speed;
temparray[count-1]=temperature;
// cout<<count<<" "<<speed<<" "<<temperature<<endl;
inFile>>count>>speed>>temperature;
}inFile.close();//close the file after reading
for(int i=0;i<count;i++){
cout<<speedarray[i]<<" "<<temparray[i]<<endl;
avgspeed=avgspeed+speedarray[i];
avgtemp=avgtemp+temparray[i];
}
avgspeed=avgspeed/count;
avgtemp=avgtemp/count;
cout << fixed; //set the precision after decimal to 2 values
cout <<setprecision(2);
cout<<"The average of the wind speed in the file is "<<avgspeed<<endl;
cout<<"The average of the temperatures in the file is "<<avgtemp<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.