# Any help would be appreciated.....

• 04-30-2003
SprinterSteve
Any help would be appreciated.....
I'm having difficulties with this assignment. I thought I was doing it correctly but unfortuneatley I'm receiving parse errors. I'm writing this program in SSH Secure Shell.

Here is my current code;

Code:

```#include <stdio.h> #include <math.h> #define pi 3.141592 main() {       int degree, q, a, b, c, d;       double x, y, y2, z;       printf("Enter angle in degrees:\n") ;       scanf("%d", &degree) ;       x = ((pi*degree)/180) ;       y=cos(x);       printf("True value of cos(x) is %d\n",y);       char ch;       do       {       printf("Enter a character; 0 to exit\n");       ch = getchar();       getchar();       switch(ch)         {             case 'a':               y2=(y-((x*x*x*x*x*x)/(6*5*4*3*2*1)));             case 'b':               y2=(y+((x*x*x*x)/(4*3*2*1)));             case 'c':               y2=(y-((x*x)/(2*1)));             case 'd':               y2=(y);           } if (ch = a) printf("4 term approximation\nApproximate cos(x) = %f\nRelative erro r = 20 percent", y2);       }       while (ch != 'q');       exit(z); }```
It seems as though my looping is not working correctly....

My Parse Errors;

Code:

``` Enter angle in degrees: 45 True value of cos(x) is -1073081832 Enter a character; 0 to exit 0 Enter a character; 0 to exit 0 Enter a character; 0 to exit 0 Enter a character; 0 to exit 0 Enter a character; 0 to exit 4 Enter a character; 0 to exit 3 Enter a character; 0 to exit 2 Enter a character; 0 to exit 1 Enter a character; 0 to exit Stopped```
And last but not least this is my assignment....

Code:

```  Scan the angle in degrees x_deg (=45). Express this angle in   radians by using x=PI*x_deg/180, and calculate Y=cos(x) by   using the math.h library of functions.   Your objective in this quiz is to compare the so caclulated   value of Y=cos(x) with the approximate value y obtained by   using 1, 2, 3, and 4 leading terms of the Taylor series:       cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...   Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 .   Use a do/while loop in conjunction with a switch statement   switch(ch). Enter the character '1' for one term approximation   '2' for two terms, etc., by using getchar() function. Use   '0' to exit the program.   Let your opening case calculate the fourth term x^6/6!, let the   subsequent case calculate the x^4/4! term, etc. (In this way,   you wan't need to use a break statement within your switch).   Evaluate the corresponding Y , print the result in one line,   and the relative error (Y-y)/Y in another line.   Execute your program for all four cases (with corresponding   printouts).                               Note:   To avoid printing on the screen:       Enter a character; 0 to Exit   twice in a row, insert the line       getchar();   after each of the lines     scanf("%lf",&x_deg);     and     n_term=getchar();     in your program. ............................................ Your output should look like this: True value of cos(x) = 0.707107 Enter a character; 0 to Exit 1 1 term approximation Approximate cos(x) = 1.000000 Relative error = -41.421356 percent Enter a character; 0 to Exit 2 2 terms approximation Approximate cos(x) = 0.691575 Relative error = 2.196545 percent Enter a character; 0 to Exit 3 3 terms approximation Approximate cos(x) = 0.707429 Relative error = -0.045598 percent Enter a character; 0 to Exit 4 4 terms approximation Approximate cos(x) = 0.707103 Relative error = 0.000504 percent Enter a character; 0 to Exit 0 */```

Thanks for any feedback,
-Steve
• 04-30-2003
quzah
Code:

```printf("True value of cos(x) is %d\n",y); char ch;```
This is not valid C code. You cannot declare variables in the middle of functions. They must be at the beginning of the scope block.

A "parse error" is not when the function doesn't give the right output. A parse error is generated at compile time.
Code:

`if (ch = a)`
You are assigning the value of a to 'ch'. This is not what you want. Use ==.

Quzah.
• 04-30-2003
stumon
Is this a snippet of code? If not, you have some things missing, some declarations and so forth. Also, getchar(), also why do you have 2 of them? get rid of the second getchar().---edited something out --- my mistake. Also, what in the world is this, It wont work!
Code:

`      scanf("%d", °ree);`
Even when I put a & where it needs to be, you never declare ree, so that needs to be done. There are quite a bit of things you need to change to get this to work. Start by running a debug and getting rid of all errors, then look at the logic errors.

--edit--

You will also want to put some break statements to get out of the case's so that they are not all done, only the one you want, then it will break out.
• 04-30-2003
quzah
Quote:

Originally posted by stumon
Also, getchar() returns int, also why do you have 2 of them? get rid of the second getchar().

This gets rid of the trailing newline. There is nothing wrong with using getchar to assign a value to a character. It's common practice. The only difference is if you were actually looking for EOF.

Quote:

Originally posted by stumon
For the first, you then use a switch:case statement comparing them to characters, this will not work, cause you are comparing ints to chars, they are represented differently when doing that.

No they aren't.

Quote:

Originally posted by stumon
Also, what in the world is this, It wont work!
Code:

`      scanf("%d", °ree);`
Even when I put a & where it needs to be, you never declare ree, so that needs to be done. There are quite a bit of things you need to change to get this to work.

This is your browser freaking out. The code is actually:

scanf("%d" & degree );

Your browser is htmling the code and putting the actual degree symbol in for some reason. His code is fine there in my browser.

I have already pointed out the only two problems with his code.

Quzah.
• 04-30-2003
stumon
Quote:

Originally posted by quzah
For the first, you then use a switch:case statement comparing them to characters, this will not work, cause you are comparing ints to chars, they are represented differently when doing that.

--------------------------------------------------------------------------------
No they aren't.[/B]
I realized that before your reply, i edited it out also. i was thinking of something else when typing that.

Also, i have never had my browser screw code up like that, i figured thats what code tags are for, to get rid of that problem, learn something new everyday.
• 04-30-2003
swisner
ok i tried changing the ch = a to ch == a and it still isnt working, so i changed the a to 4 the b to 3 the c to 2 and the d to 1 and left it as ch = 4 and that sort of worked, also i dont understand about the declaring a variable in the middle. heres my new code and the errors im getting:

Code:

```#include <stdio.h> #include <math.h> #define pi 3.141592 main() {         int degree, q, a, b, c, d;         double x, y, Y, z, error;         printf("Enter angle in degrees:\n") ;         scanf("%d", &degree) ;         x = ((pi*degree)/180) ;         Y=cos(x);         printf("True value of cos(x) is %d\n",Y);         error = ((Y-y)/Y);         char ch;         do         {         printf("Enter a character; 0 to exit\n");         ch = getchar();         getchar();         switch(ch)           {             case '4':                 y=(y-((x*x*x*x*x*x)/(6*5*4*3*2*1)));             case '3':                 y=(y+((x*x*x*x)/(4*3*2*1)));             case '2':                 y=(y-((x*x)/(2*1)));             case '1':                 y=(y);           } if (ch = '4') printf("4 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error); if (ch = '3') printf("3 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error); if (ch = '2') printf("2 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error); if (ch = '1') printf("1 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error);         }         while (ch != '0'); exit(0); }```
Code:

```Enter angle in degrees: 45 True value of cos(x) is -1073081832 Enter a character; 0 to exit 3 4 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent 3 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent 2 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent 1 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent Enter a character; 0 to exit 0 4 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent 3 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent 2 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent 1 term approximation Approximate cos(x) = 0.000000 Relative error = 1.000000 percent Enter a character; 0 to exit```
the answer for what cos equals is wrong and the loop repeats with out exit and gives the same result no matter what ch i put in,
thanks for any feedback
• 05-01-2003
Dave_Sinkula
Avoiding the problem with the loop repeating was given in the assignment description.
Code:

```#include <stdio.h> #include <math.h> #define pi 3.141592 int main(void) {   int degree, /*q, a, b, c, d*/ch; /* getchar() returns an int */   double x, y, Y, /*z,*/ error;   printf("Enter angle in degrees:\n") ;   scanf("%d", &degree) ;   getchar(); /* get rid of the trailing '\n' */   x = ((pi*degree)/180) ;   Y=cos(x);   printf("True value of cos(x) is %f\n",Y);   /* at this point 'y' is not initialized, so don't do any calculations with it   error = ((Y-y)/Y);   char ch;   */   do   {       y = 0.0; /* clear approximation each time throught the loop */       printf("Enter a character; 0 to exit\n");       ch = getchar();       getchar();       switch(ch)       {         case '4':             y=(y-((x*x*x*x*x*x)/(6*5*4*3*2*1)));         case '3':             y=(y+((x*x*x*x)/(4*3*2*1)));         case '2':             y=(y-((x*x)/(2*1)));         case '1':             y=(y+1.0);             error = ((Y-y)/Y);             printf("%c term approximation\n"                   "Approximate cos(x) = %f\n"                   "Relative error = %f percent\n", ch, y, error*100.0);       }       /* if (ch = '4') printf("4 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error); if (ch = '3') printf("3 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error); if (ch = '2') printf("2 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error); if (ch = '1') printf("1 term approximation\nApproximate cos(x) = %f\nRelative er ror = %f percent\n", y, error);       */   } while (ch != '0');   return 0; /* exit(0); */ }```