math program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 2
Reputation: LilLady is an unknown quantity at this point 
Solved Threads: 0
LilLady LilLady is offline Offline
Newbie Poster

math program

 
1
  #1
Nov 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: math program

 
0
  #2
Nov 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: math program

 
0
  #3
Nov 10th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: LilLady is an unknown quantity at this point 
Solved Threads: 0
LilLady LilLady is offline Offline
Newbie Poster

Re: math program

 
0
  #4
Nov 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: math program

 
0
  #5
Nov 10th, 2007
Originally Posted by LilLady View Post
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: math program

 
0
  #6
Nov 11th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 834 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC