A mathematical relationship between x and y is described by
the following expressions:
y= A*x^3-B*x+3/4 if x<=0 (Case 1)
y=1-B-C*x^4 if 0<x<1 (Case 2)
y=A*log10(x)+B/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 by using %f format with 3 decimal places,
and print y by using %f format with 5 decimal places.
Use the pow(a,b) function to calculate x^3 and x^4, 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 do/while loop to
evaluate y for scanned x in each of the above three cases. If you
scan x from the same interval again, your program should ask you
to scan x from another interval, until you scan x once from each
interval.
Your output should look like:
iacs5.ucsd.edu% gcc hw4.c -lm
iacs5.ucsd.edu% a.out
Enter (double) A, B, C:
1.5 -2.1 0.5
Enter (double) x:
-0.75
Case 1
x value is = -0.750 and y value is = -1.45781
Enter (double) x:
0.4
Case 2
x value is = 0.400 and y value is = 3.08720
Enter (double) x:
0.75
Enter x from another interval!
Enter (double) x:
1.5
Case 3
x value is = 1.500 and y value is = -1.13586
iacs5.ucsd.edu%
*/
This is what i have so far
#include <stdio.h>
#include <math.h>
main()
{
double A, B, C, x, y;
printf("\nEnter (double) A, B, C:\n");
scanf("%lf %lf %lf", &A, &B, &C);
do{
printf("\nEnter (double) x:\n");
scanf("%lf", &x);
if(x<=0){
y =(A * pow(x,3.)) - (B * x) + 0.75;
printf("Case 1\n");}
else{
if(0<x, x<1){
y = 1 - B - C * pow(x,4.);
printf("Case 2\n");}
else{
if(x>=1){
y = A * log10(x) + B / x;
printf("Case 3\n");}
}
}
printf("x value is = %.3f and y value is = %.5f\n", x, y);
} while(x=x);
exit(0);
}
I can't figure out how to make the program detect if it is part of the same interval. I'm pretty sure it has something to do with the for loop. But wouldn't that end the program? I need the program to run until all three intervals are used. Am I on the right track? Or do I need to use another method to execute this program?
the following expressions:
y= A*x^3-B*x+3/4 if x<=0 (Case 1)
y=1-B-C*x^4 if 0<x<1 (Case 2)
y=A*log10(x)+B/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 by using %f format with 3 decimal places,
and print y by using %f format with 5 decimal places.
Use the pow(a,b) function to calculate x^3 and x^4, 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 do/while loop to
evaluate y for scanned x in each of the above three cases. If you
scan x from the same interval again, your program should ask you
to scan x from another interval, until you scan x once from each
interval.
Your output should look like:
iacs5.ucsd.edu% gcc hw4.c -lm
iacs5.ucsd.edu% a.out
Enter (double) A, B, C:
1.5 -2.1 0.5
Enter (double) x:
-0.75
Case 1
x value is = -0.750 and y value is = -1.45781
Enter (double) x:
0.4
Case 2
x value is = 0.400 and y value is = 3.08720
Enter (double) x:
0.75
Enter x from another interval!
Enter (double) x:
1.5
Case 3
x value is = 1.500 and y value is = -1.13586
iacs5.ucsd.edu%
*/
This is what i have so far
#include <stdio.h>
#include <math.h>
main()
{
double A, B, C, x, y;
printf("\nEnter (double) A, B, C:\n");
scanf("%lf %lf %lf", &A, &B, &C);
do{
printf("\nEnter (double) x:\n");
scanf("%lf", &x);
if(x<=0){
y =(A * pow(x,3.)) - (B * x) + 0.75;
printf("Case 1\n");}
else{
if(0<x, x<1){
y = 1 - B - C * pow(x,4.);
printf("Case 2\n");}
else{
if(x>=1){
y = A * log10(x) + B / x;
printf("Case 3\n");}
}
}
printf("x value is = %.3f and y value is = %.5f\n", x, y);
} while(x=x);
exit(0);
}
I can't figure out how to make the program detect if it is part of the same interval. I'm pretty sure it has something to do with the for loop. But wouldn't that end the program? I need the program to run until all three intervals are used. Am I on the right track? Or do I need to use another method to execute this program?
Comment