Hello, my C++ algorithm prints out a list of random wind speedthough out a perio
ID: 3610234 • Letter: H
Question
Hello, my C++ algorithm prints out a list of random wind speedthough out a period of 3600 seconds in a data file. My question is,how can I add two headers, "Time" and "Speeds" in the datafile.#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<time.h>
using namespace std;
double WINDSPEED (double i, double k);
int main()
{
string filename;
ofstream KKK;
double windspeed;
bool storming=false;
int time1, i, k,storm,stoptime=-1;
unsigned seed=time(0);
srand(seed);
i=8;
k=12;
cout << "Please enter the filename for output: ";
cin >> filename;
KKK.open(filename.c_str());
cout.setf(ios::dec);
cout << fixed << setprecision(4);
for (time1=0; time1<3601; time1=time1+10)
{
storm=rand()%200+1; //.5% is 1 in 200
if(storm==200)
{storming=true; //sayit's storming
stoptime=time1+300; //and gettime storm will stop
}
if(storming&&time1>=stoptime) //storm over
storming=false;
windspeed=WINDSPEED(i, k);
if(storming)
windspeed+=10; //if stormingincrease wind speed
KKK << setw(8) << time1 << setw(8) <<windspeed << endl;
}
KKK.close();
system ("pause");
return 0;
}
double WINDSPEED (double i, double k)
{
return(((double) rand()/RAND_MAX)*(k-i)+i);
} Hello, my C++ algorithm prints out a list of random wind speedthough out a period of 3600 seconds in a data file. My question is,how can I add two headers, "Time" and "Speeds" in the datafile.
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<time.h>
using namespace std;
double WINDSPEED (double i, double k);
int main()
{
string filename;
ofstream KKK;
double windspeed;
bool storming=false;
int time1, i, k,storm,stoptime=-1;
unsigned seed=time(0);
srand(seed);
i=8;
k=12;
cout << "Please enter the filename for output: ";
cin >> filename;
KKK.open(filename.c_str());
cout.setf(ios::dec);
cout << fixed << setprecision(4);
for (time1=0; time1<3601; time1=time1+10)
{
storm=rand()%200+1; //.5% is 1 in 200
if(storm==200)
{storming=true; //sayit's storming
stoptime=time1+300; //and gettime storm will stop
}
if(storming&&time1>=stoptime) //storm over
storming=false;
windspeed=WINDSPEED(i, k);
if(storming)
windspeed+=10; //if stormingincrease wind speed
KKK << setw(8) << time1 << setw(8) <<windspeed << endl;
}
KKK.close();
system ("pause");
return 0;
}
double WINDSPEED (double i, double k)
{
return(((double) rand()/RAND_MAX)*(k-i)+i);
}
Explanation / Answer
please rate - thanks I also took the liberty of separating the data a little bit. In the file they were separated by 1 space, you could barely tellwhere 1 ended and the other started. I separated them alittle. If it was me I would separate them some more changes in red #include #include #include #include #include #include using namespace std; double WINDSPEED (double i, double k); int main() { string filename; ofstream KKK; double windspeed; bool storming=false; int time1, i, k,storm,stoptime=-1; unsigned seed=time(0); srand(seed); i=8; k=12; cout > filename; KKK.open(filename.c_str()); cout.setf(ios::dec); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.