/* Print fake pulsar data program */ #include #include using namespace std; #include #include #define pi 3.14159265305 int imax; float frequency; void readinvalues(int *imax,float *frequency) { /*This subroutine inputs the user for the */ /*the number of values of the sine wave to compute*/ /*and the frequency of the sine wave */ int ivalue; float freqvalue; cout<<"How many values of the sine wave do you wish to compute?\n"; cin>> ivalue; cout<<"OK -- running for "<< ivalue << "values.\n"; cout<<"What is the frequency of the sine wave?\n"; cin >> freqvalue; *imax=ivalue; *frequency=freqvalue; /*When we are changing the variables within a subroutine in C*/ /*we need to use pointers, rather than just the variable name*/ return; } double r2() { return (double)rand() / (double)RAND_MAX ; /* Returns a random number from 0 to 1, since rand gives a number up to rand _max*/ } void printoutvalues(int imax,float frequency) { /*This subroutine prints out the value of */ /*the sine wave for the different values of time, or */ /*whatever the x-axis is*/ int i; float x,y; ofstream myfile; /*ofstream tells it it's an output file */ myfile.open("sines.txt"); for (i = 0;i < imax; i=i+1 ) { x = float(i)*frequency*2.0*pi/100.0; y =sin(x)+3.0*r2(); myfile << x/(2.0*pi) <<" " << y << endl; /*In the line above, we do everything like with cout, except that we use myfile in place of cout. The syntax for how to set up output is the same. When we want to do input, we use ifstream to define the thing that myfile does here. Then we use myfile or whatever else we want to call it instead of cin, but otherwise keep the syntax the same. */ } /* By dividing by 2*pi in the previous line, we get the x-variable*/ /* to be in units of periods, rather than radians*/ return ; } main() { int i,imax; float frequency; readinvalues(&imax,&frequency); printoutvalues(imax,frequency); }