Thread: For loop and case help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    For loop and case help

    I am a newbie; I know as far as we have covered in class... so if there are any advanced commands ( and their not avoidable ) , please post what they do... I would love to learn


    Assignment:

    Code:
                      
    A mathematical relationship between x and y is described by
    the following expressions:
    
    y= A*x^3+B*x-1        if  x<=0   (Case 1)
    
    y=B+C*x^2             if  0<x<1  (Case 2)
    
    y=B+C/x               if  x>=1   (Case 3)
    
    where A, B, and C are constants. Write a C program that reads
    the double values of the constants A,B,C, and the argument x
    (by using scanf), and computes the corresponding value of y.
    Print x and y by using %f format with 4 decimal places.
    
    Use the pow(a,b) function to calculate x^2 and x^3, and if/else
    statements to choose the proper expression for y, corresponding
    to selected x.
    
    After reading A,B,C, your program should use a for loop to
    evaluate y for scanned x in each of the above three cases.
    
    Your output should look like:
    
    Enter (double) A, B, C:
    1. 2. 3.
    
    Enter (double) x:
    0.5
    Case 2
    x value is = 0.5000  and  y value is = 2.7500
    
    Enter (double) x:
    -1.
    Case 1
    x value is = -1.0000  and  y value is = -4.0000
    
    Enter (double) x:
    2.
    Case 3
    x value is = 2.0000  and  y value is = 3.5000
    
    */
    What I have so far:
    Code:
    */
    #include<stdio.h>
    #include<math.h>
    main()
    {
    
    int k;
    char a;
    double A,B,C,x,y;
    printf("Enter (double) A, B, C:\n");
    scanf("%lf,%lf,%lf",&A, &B, &C);
    
    for(k=1;k<=3;k++)
    {
    
    printf("Enter (double) x:\n");
    scanf("%lf",&x);
    printf("\n");
    if (x<=0)
    {
            a='X';
    }
    else if(0<x<1)
    {
            a='Y';
    }
    else if(x>=1)
    {
            a='Z';
    }
    switch(a)
            {
            case 'X':
                    y=A*pow(x,3)+B*x-1;
                    printf("Case 1\n");
                    printf("x value is = %.4f and y value is = %.4f\n",x,y);
                    break;
            case 'Y':
                    y=B+C*pow(x,2);
                    printf("Case 2\n");
                    printf("x value is = %.4f and y value is = %.4f\n",x,y);
                    break;
            case 'Z':
                    y=B+C/x;
                    printf("Case 3\n");
                    printf("x value is = %.4f and y value is = %.4f\n",x,y);
                    break;
            }
    }
    }

    Output so far:
    Code:
    Enter (double) A, B, C:
    1. 2. 3.
    Enter (double) x:
    
    Case 3
    x value is = 2.0000 and y value is = 0.0000
    Enter (double) x:
    
    Case 3
    x value is = 3.0000 and y value is = 0.0000
    Enter (double) x:
    0.5
    
    Case 3
    x value is = 0.5000 and y value is = 0.0000
    Problems so far:
    After running the printf command for "Enter (double) x:", the program does not allow an input of x, rather it just continues on and runs the switch. THEN the loop takes charge and then runs the scanf for x.

    Whats wrong?

    Thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You have junk in the input buffer, 'cos you're using scanf() to get the number. Two choices:

    Change the way you get the number:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Flush the buffer:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 09:27 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM