/* Compute sine waves program */ #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; printf("How many values of the sine wave do you wish to compute?\n"); scanf("%i",&ivalue); printf("OK -- running for %i values.\n",ivalue); printf("What is the frequency of the sine wave?\n"); scanf("%f",&freqvalue); printf("OK -- running for frequency %f.\n",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; } void printoutsines(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; FILE *fp; printf("Running for %i and frequency %f",imax,frequency); fp = fopen ("out_sines", "w"); for (i = 0;i < imax; i=i+1 ) { x = float(i)*frequency*2.0*pi/100.0; y =sin(x); fprintf(fp,"%f %f \n",x/(2.0*pi),y); } /* 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); printoutsines(imax,frequency); }