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, &amplitude , &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;
}
Salem commented: Thanks for using code tags, and welcome to DW +12

Recommended Answers

All 5 Replies

1. If you say [code=c], we get line numbers, which makes it much easier to refer to specific parts of the code which are causing you trouble.

2. #define PI = (3.14)
This should be just
#define PI (3.14)
Though you might want to be more precise.

Ok I added the line numbers for you, which compiler are you using and what line number is it complaining about ? Probably line 6 as Salem mentioned?

ok i changed the the define PI part, but it is line 42 that is giving me trouble.The compiler that i am using is pelles c.

ok i changed the the define PI part, but it is line 42 that is giving me trouble.The compiler that i am using is pelles c.

Missing a parenthesis
array[count] = amplitude * cos(2 * PI * (count * frequency / sample + phase) );

Line 33 n++; /* undeclared */
Line 54 sprintf(filename,"%s", &name); /* remove the & */


Comment:
array = (double *) malloc(count*sizeof(double));
You don't need to cast malloc as (double *).
Count is set to 1 therefore it would be the same that malloc( sizeof ( double ) ). If count would be set to other value then it would make a difference.

scanf is a bad choice for obtaining a string from user. Check this link to learn more

All these little things are important, but I think you need to spend more time thinking about how you are going to solve the problem. The computer is too stupid to do it, so you have to figure it out yourself first.

I say this all the time but I don't think anyone has ever done it that I haven't sat next to and forced them to: get out a piece of paper and a pencil and write down and draw what you understand about the problem. Don't stop until you can solve the problem on paper. Once you know what steps you took to solve the problem --only then can you tell the computer how to do it.

For this problem, the first thing I would do is draw a little sinusoid and mark it up. (Appologies for the badly GIMPed image.)
[IMG]http://home.comcast.net/~michaelthomasgreer/temp/samples_per_cycle.gif[/IMG]
I drew three cycles, and I imagined that I wanted four samples per cycle. I then counted how many samples total there were: 12. If I want to take a sample at x=0 also then I would say 13 samples total. To recap for this example:
- the user asked for 3 cycles
- the user asked for 4 samples per cycle
- therefore, I need to write 3 * 4 (and maybe one extra for the beginning sample) to file.

Now I need to think about a little math. Each orange mark is a sample. That is, for some specific x I can use my sinusoid function to get some specific y, which the instructions say should be saved to file. Once I have asked the user for all the variables (frequency, amplitude, phase, and x), I can use these to make an equation that solves for y. Since I must do it multiple times that suggests a loop (for step 7). The first three variables never change between samples. But x must change every sample. Therefore, I should first calculate a new x every time through the loop and then calculate the y, and save y in the array.

Now I think about how I am going to save each sample: store it in an array. However, the professor asked that the array be created before each collection of variables for the curve and that the array of samples be written to file after going through the loop an unknown number of times (until the user answers 'n'), so where do I put each new collection of samples? You need to ask your professor if the loop isn't better put from steps 4 through 12 (instead of the current 5 through 9), or if each time through the loop the previous samples are to be lost (overwritten with the new samples for the new equation).

Hope this helps.

BTW. Tell your professor not to have you name arrays "array". You can get away with it in C (and some other languages too), but it is always a bad idea. Use a better name, like at least ArrayOfSamples or SampleArray.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.