Thread: C Program to evaluate y=exp(x); Novice, HELP PLEASE!! :)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    C Program to evaluate y=exp(x); Novice, HELP PLEASE!! :)

    Hi! I have written a part of the program but don't know if I'm going in the right direction. I was also wondering how to use getchar() because I've never used that before. I'm quite a novice at this so any help would be greatly appreciated. Thanks!


    Scan a double variable x and evaluate y=exp(x) by
    using the math.h library of functions.

    Your objective in this homework is to compare the so calculated
    value of y with the approximate value Y obtained by
    using 1, 2, 3, 4, and 5 leading terms of the Taylor series:

    exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + x^4/4! + ....

    Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 and 0!=1.

    Use a do/while loop in conjunction with a switch statement
    switch(). Enter a character 1 for one term approximation,
    2 for two terms, etc., by using getchar() function. Use
    0 to exit the program. Within switch, use default:
    if (char != '0') => unrecognized operator.

    Let your opening case calculate the fourth term x^4/4!, let the
    subsequent case calculate the x^3/3! term, etc. (In this way,
    you wan't need to use repeatedly 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 five cases (with corresponding
    printouts).


    ............................................
    Your output should be like this:


    Enter x: 1.25


    True value of exp(1.2500) = 3.490343

    Enter a character 1-5 (0 to exit):
    1
    1 term(s) approximation
    Approximate exp(1.2500) = 1.000000
    Relative error = -71.349520 percent

    Enter a character 1-5 (0 to exit):
    2
    2 term(s) approximation
    Approximate exp(1.2500) = 2.250000
    Relative error = -35.536421 percent

    Enter a character 1-5 (0 to exit):
    3
    3 term(s) approximation
    Approximate exp(1.2500) = 3.031250
    Relative error = -13.153233 percent

    Enter a character 1-5 (0 to exit):
    4
    4 term(s) approximation
    Approximate exp(1.2500) = 3.356771
    Relative error = -3.826905 percent

    Enter a character 1-5 (0 to exit):
    5
    5 term(s) approximation
    Approximate exp(1.2500) = 3.458496
    Relative error = -0.912428 percent

    Enter a character 1-5 (0 to exit):
    6
    unrecognized operator

    Enter a character 1-5 (0 to exit):
    0




    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    /* Scanning a double variable x and evaluating y=exp(x) by
    using the math.h library of functions */
    
    int main() {
    
    /* Defining variables */
    int k, N;
    char ch;
    double x, y, Y;
    
    /* Two empty lines and data input + true value of exp(x) at beginning */
    printf("\n\nEnter x:\n\n");
    scanf("%lf", &x);
    
    y=exp(x);
    printf("True value of exp(%lf) = %9.6f\n", x, y);
    
    
    /* Data input: do/while statement with a switch statement within */
    do {
            printf("Enter x:\n");
            scanf("%d", &N);
            y=0;
    
            switch(N) {
                    case 5;
                            y-=
    
    
    
    
    }
    }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're fine until your do-while loop.

    Code:
    do
        get N
        switch N
            case 0:
                // do nothing
            case 1-5:
                call exp_approx(N)
            default:
                print "unrecognized operator"
    while N is not 0
    You will also need to define the exp_approx() function, and probably a factorial() function.

    EDIT: Duh, you need a switch

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Thank you!!
    Is there any specific way to use getchar() ?
    Is that kind of like scanf?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    getchar() just returns a single character from the standard input device, not like scanf, which does a myriad of things. Check your docs for specifics or google for examples. Also note that I didn't read your directions very well. You need to let your case statements for 5, 4, 3, 2 and 1 fall through to each other and total the parts of the approximation as you fall through.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Thanks so much! That clarifies a lot.

    So I wrote the program and it compiled but it wouldn't run for some reason.
    Then I realized that I needed to edit this:



    Code:
    printf("Enter a character 1-5 (0 to exit):\n");
            scanf("%lf", &ch);
            y=0;

    And turn it into this (b/c I'm supposed to use getchar()):


    Code:
    printf("Enter a character 1-5 (0 to exit):\n");
            getchar("%lf", &ch);
            y=0;
    But now I'm getting an error:


    Code:
    109:20: error: macro "getchar" passed 2 arguments, but takes just 0
    What does that even mean??

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That means you gave getchar two arguments ("%lf" and &ch), but getchar doesn't take any arguments at all (0). You should look at the manual for getchar to see how to use it.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    When I put (0), I got:

    error: macro "getchar" passed 1 arguments, but takes just 0

    So I left it blank: getchar()

    I don't know if that's right but it compiled and I got in the assignment right on time so thank you!!
    I'll definitely read up on it.

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    that is because getchar(0) is the same thing as getchar(void) and getchar()

    ch = getchar() ; would be how to do it
    You ended that sentence with a preposition...Bastard!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice program, plz help
    By Johnta in forum C Programming
    Replies: 12
    Last Post: 02-22-2008, 01:53 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM