944,000 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 989
  • C RSS
Nov 10th, 2007
1

math program

Expand Post »
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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #define PI = (3.14)
  7.  
  8. int main(int argc, char *argv[]) {
  9.  
  10. int count = 1;
  11. int cycle;
  12. int sample;
  13. int frequency;
  14. float amplitude;
  15. float phase;
  16. double *array = 0;
  17. char reply[250];
  18. char filename[250];
  19. char name[50];
  20. FILE *f;
  21.  
  22. printf("Name \n");
  23.  
  24. printf("How many cycles? \n");
  25. scanf("%d", &cycle);
  26.  
  27. printf("How many samples per cycle?\n");
  28. scanf("%d", &sample);
  29.  
  30. array = (double *) malloc(count*sizeof(double));
  31. array[0] = sample*cycle;
  32. printf("%lf\n", array[0]);
  33. n++;
  34.  
  35. int ok = 0;
  36. while (!ok) {
  37. ok = 1;
  38. printf("Please enter the frequency, amplitude, and phase:\n");
  39. scanf("%d %f %f", &frequency, &amplitude , &phase);
  40. printf("Amplitude: %d, frequency: %f, Phase: %f\n", frequency, amplitude , phase);
  41.  
  42. 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*/
  43.  
  44. printf("Would you like to add another sinusoid?\n");
  45. scanf("%s", reply);
  46. reply [0] = tolower(reply[0]);
  47. if (reply[0] == 'y') {
  48. count ++;
  49. ok=0;
  50. }
  51. if (reply[0] != 'y') {
  52. printf("Please enter a name for the output file:\n");
  53. scanf ("%s", name); //gets name
  54. sprintf(filename,"%s", &name); //makes filename into the students.ans
  55. printf( " %s\n", filename );
  56. f= fopen(filename, "w");
  57. if (array[count] != '\0'){
  58. fprintf(f, "%lf\n", array[0]);
  59. }
  60. fclose(f);
  61. printf("Calculator exiting...\n");
  62. return 0;
  63. }
  64. }
  65. free(array);
  66. return 0;
  67. }
Last edited by Ancient Dragon; Nov 10th, 2007 at 8:01 am. Reason: add line numbers
Similar Threads
Reputation Points: 22
Solved Threads: 0
Newbie Poster
LilLady is offline Offline
2 posts
since Nov 2007
Nov 10th, 2007
0

Re: math program

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 10th, 2007
0

Re: math program

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?
Last edited by Ancient Dragon; Nov 10th, 2007 at 8:03 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 10th, 2007
0

Re: math program

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.
Last edited by LilLady; Nov 10th, 2007 at 9:23 pm.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
LilLady is offline Offline
2 posts
since Nov 2007
Nov 10th, 2007
0

Re: math program

Click to Expand / Collapse  Quote originally posted by LilLady ...
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
Last edited by Aia; Nov 10th, 2007 at 10:03 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 11th, 2007
0

Re: math program

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.)

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Alphabetizing
Next Thread in C Forum Timeline: arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC