# Thread: For loop and case help

1. ## 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.

evaluate y for scanned x in each of the above three cases.

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?  Popular pages Recent additions 