The assignment is as follows:
1. Print name
2. Ask user "How many cycles?" and accept integer answer.
3. Ask user "How many samples per cycle?" and accept integer answer.
4. Calculate total number of samples and allocate an array of doubles to hold
them. Initialize doubles to zero. Hint: if you have double *array; then
array = (double *) malloc(n * sizeof(double)); where n is the size of the array.
5. Ask the user to enter frequency, amplitude and phase for a sinusoid. Frequency
will be an integer, amplitude a float, and phase a float. Phase = 0 is a cosine.
6. Accept the user inputs, all on one line.
7. Generate samples of the requested sinusoid and add them to the allocated
array of doubles. The formula is:
a * cos(2*PI*(i*f/spc + phi)) where a is the amplitude, pi you must define, i is
your loop counter, f is the frequency, spc is the samples per cycle, and
phi is the phase in fractions of a cycle. You must #include<math.h> for
cos().
8. Ask the user if they want to add another sinusoid.
9. If the response starts with 'y' or 'Y' loop back to step 5.
10. Otherwise, ask the user to enter a name for the output file.
11. Open the file the user specified.
12. Write the array of doubles to the file, one value per line
13. Write "Program exiting..." and quit.
I am having trouble doing #7 when i run the compiler it says unrecognized declaration...any ideas would be helpful...and if u see any problems with my program please feel free to point it out. Thank you
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#define PI = (3.14)
int main(int argc, char *argv[]) {
int count = 1;
int cycle;
int sample;
int frequency;
float amplitude;
float phase;
double *array = 0;
char reply[250];
char filename[250];
char name[50];
FILE *f;
printf("Name \n");
printf("How many cycles? \n");
scanf("%d", &cycle);
printf("How many samples per cycle?\n");
scanf("%d", &sample);
array = (double *) malloc(count*sizeof(double));
array[0] = sample*cycle;
printf("%lf\n", array[0]);
n++;
int ok = 0;
while (!ok) {
ok = 1;
printf("Please enter the frequency, amplitude, and phase:\n");
scanf("%d %f %f", &frequency, &litude , &phase);
printf("Amplitude: %d, frequency: %f, Phase: %f\n", frequency, amplitude , phase);
array[count] = amplitude * cos(2 * PI * (count * frequency / sample + phase); /*this doesnt work it returns errors, but i need this calculation to generate samples of the requested sinusoid-#7*/
printf("Would you like to add another sinusoid?\n");
scanf("%s", reply);
reply [0] = tolower(reply[0]);
if (reply[0] == 'y') {
count ++;
ok=0;
}
if (reply[0] != 'y') {
printf("Please enter a name for the output file:\n");
scanf ("%s", name); //gets name
sprintf(filename,"%s", &name); //makes filename into the students.ans
printf( " %s\n", filename );
f= fopen(filename, "w");
if (array[count] != '\0'){
fprintf(f, "%lf\n", array[0]);
}
fclose(f);
printf("Calculator exiting...\n");
return 0;
}
}
free(array);
return 0;
}