31. Write a program to generate a data file named wind.dat that contains 1 hour
ID: 674155 • Letter: 3
Question
31. Write a program to generate a data file named wind.dat that contains 1 hour of simulated wind speeds. Each line of the data file should contain the time in seconds and the corresponding wind speed. The time should start with 0 seconds. The increment in time should be 10 seconds and the final line of the data should correspond to 3600 seconds. The user should be prompted to enter the range of wind speed.
32. In problem 31, assume that we want the flight simulator wind data to include a 0.5% possibility of encountering a small storm at each time step. Therefore, modify the solution to problem 31 so that the average wind speed is increase by 10 mph for a period of 5 minutes when a storm is encountered.
The program should
(1) set the precision to 4 decimal places,
(2) apply srand() function,
(3) use module to generate windspeeds
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<iomanip>
using namespace std;
double windspeed(double a,double b);
int main()
{
double a, b, storm;
ofstream datafile;
datafile.open("wind.dat");
srand(time(0));
cout<<"Enter the range of windspeed (a and b):"<<endl;
cin>>a>>b;
datafile << setw(20) << "Time(sec)" << setw(20) << "Windspeed(mph)" <<endl;
for (int time=0; time<3601; time+=10)
{
storm=windspeed(0,100);
if(storm <=0.5)
// storm case
{
for (int i=0;i<=300;i+=10)
{
datafile << fixed;
datafile << setw(20) << time << setw(20) << 10+ windspeed(a,b)<< setprecision(4) << endl;
}
}
else
// normal windspeed
{
datafile << setw(20) << time << setw(20) << windspeed(a,b)<<endl;
}
}
datafile.close();
// system("pause");
/* Exit program. */
return 0;
}
double windspeed(double a,double b)
{
return (((double)rand()/RAND_MAX)*(b-a)+a);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.