A mathematical relationship between x and y is described by
the following expressions:
y= A*x^3B*x+3/4 if x<=0 (Case 1)
y=1BC*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?
7 4620 arne 315
Expert 100+
A mathematical relationship between x and y is described by
the following expressions:
y= A*x^3B*x+3/4 if x<=0 (Case 1)
y=1BC*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?
You can check if 0<x<1 by
but since you are already in the else branch you already know that 0<x. So,
is sufficient here.
The same holds true for your last check:
If you come to this point, you already know that this condition is true. Otherwise you wouldn't come to this point :).
I would propose the following structure: 
if( x<0 ) {


...


} else if ( x<1 ) {


...


} else {


...

}

If you need to have visited each branch at least once before you quit, you may introduce flags like (name them like you want): 
int smaller0 = 0;

int smaller1 = 0;

int greater1 = 0;

Whenever you visit one of the branches you set the corresponding flag to 1. Put a loop around your ifconstruct and let it run until all flags are set to 1.
One more thing: the ifcondition
is probably not what you want, since this is an assignment (=) not a comparison (==). Your compiler should warn you about this one. If not, get a better one ;)
thanks for the help. i'll revise the if/else statements later because it seems to be just a cosmetic fixup. your way looks alot neater =]
as far as the flags go. would i put them in a for statement before each if statement?
i declared the flags and my program looks like so.
#include <stdio.h>
#include <math.h>
main()
{
int smaller0=0, smaller1=0, greater1=0;
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(smaller0 == 1, smaller1 == 1, greater1 == 1);
exit(0);
}
arne 315
Expert 100+
thanks for the help. i'll revise the if/else statements later because it seems to be just a cosmetic fixup. your way looks alot neater =]
as far as the flags go. would i put them in a for statement before each if statement?
i declared the flags and my program looks like so.
#include <stdio.h>
#include <math.h>
main()
{
int smaller0=0, smaller1=0, greater1=0;
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(smaller0 == 1, smaller1 == 1, greater1 == 1);
exit(0);
}
You would put them _into_ the corresponding if branch: the flags are used to remember that you have been in this branch.
If I understood correctly what the program should do, I would propose the following structure: 
do {


if( x<0 ) {


...

smaller0 = 1;


} else if ( x<1 ) {


...

smaller1 = 1;


} else {


...

greater1 = 1;

}


} while( smaller0 == 0  smaller1 == 0  greater1 == 0 );

This should ensure that the user has visited each branch at least once before the program finishes.
Note that you have to specify the logical connection between different conditions in the while brackets. Separating them by comma will not compile...
Thanks! Program works with loop now! if i have any other questions, i'll post =]
Thanks! Program works with loop now! if i have any other questions, i'll post =]
one last question. now that i have the loop to work, i need the program to recognize whether or not the value inputed has been used in an interval already. If it has, then the program needs to tell the user to input from another interval like so:
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
Would I put something like this after I end the do/while loop with an if statement, or does this go inside the do/while loop as well? The program should bypass the second calculation. and go straight back to the beginning of the program if a repeat in intervals occur.
arne 315
Expert 100+
one last question. now that i have the loop to work, i need the program to recognize whether or not the value inputed has been used in an interval already. If it has, then the program needs to tell the user to input from another interval like so:
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
Would I put something like this after I end the do/while loop with an if statement, or does this go inside the do/while loop as well? The program should bypass the second calculation. and go straight back to the beginning of the program if a repeat in intervals occur.
If you wanted to keep the current structure, you could add a check if you have already visited the branch. If yes, print out a message and continue with the next iteration. 
if( 1 == smaller0 ) {


printf( "Please use another interval\n" );

continue;

}

... // the calculation comes here

This coding should be placed inside the corresponding ifbranch. Note that you have to change the name of the flag for the other two branches :)
program works exactly the way it should now
thank you sooooo much =]
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
14 posts
views
Thread by S. Nurbe 
last post: by

26 posts
views
Thread by Ricardo 
last post: by

1 post
views
Thread by Jim Heavey 
last post: by

7 posts
views
Thread by randomtalk 
last post: by

4 posts
views
Thread by Jackson Peebles 
last post: by
 
3 posts
views
Thread by Eric Lilja 
last post: by
             