468,457 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

how to stop program after input

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?
Oct 19 '06 #1
7 4516
arne
315 Expert 100+
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?
You can check if 0<x<1 by
Expand|Select|Wrap|Line Numbers
  1. if( 0<x && x<1 ) { ... }
  2.  
but since you are already in the else branch you already know that 0<x. So,
Expand|Select|Wrap|Line Numbers
  1. if( x<1 ) {} 
  2.  
is sufficient here.
The same holds true for your last check:
Expand|Select|Wrap|Line Numbers
  1. if(x>=1) {...}
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. if( x<0 ) {
  2.  
  3.     ...
  4.  
  5. } else if ( x<1 ) {
  6.  
  7.     ...
  8.  
  9. } else {
  10.  
  11.     ...
  12. }
  13.  
If you need to have visited each branch at least once before you quit, you may introduce flags like (name them like you want):
Expand|Select|Wrap|Line Numbers
  1. int smaller0 = 0;
  2. int smaller1 = 0;
  3. int greater1 = 0;
  4.  
Whenever you visit one of the branches you set the corresponding flag to 1. Put a loop around your if-construct and let it run until all flags are set to 1.

One more thing: the if-condition
Expand|Select|Wrap|Line Numbers
  1. if( x=x ) {}
  2.  
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 ;-)
Oct 19 '06 #2
thanks for the help. i'll revise the if/else statements later because it seems to be just a cosmetic fix-up. 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);
}
Oct 19 '06 #3
arne
315 Expert 100+
thanks for the help. i'll revise the if/else statements later because it seems to be just a cosmetic fix-up. 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:

Expand|Select|Wrap|Line Numbers
  1. do {
  2.  
  3.     if( x<0 ) {
  4.  
  5.         ...
  6.                 smaller0 = 1; 
  7.  
  8.     } else if ( x<1 ) {
  9.  
  10.                 ...
  11.                 smaller1 = 1;
  12.  
  13.     } else {
  14.  
  15.                 ...
  16.                 greater1 = 1;
  17.     }
  18.  
  19. } while( smaller0 == 0 || smaller1 == 0 || greater1 == 0 );
  20.  
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...
Oct 19 '06 #4
Thanks! Program works with loop now! if i have any other questions, i'll post =]
Oct 19 '06 #5
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.
Oct 19 '06 #6
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.

Expand|Select|Wrap|Line Numbers
  1. if( 1 == smaller0 ) { 
  2.  
  3.             printf( "Please use another interval\n" );
  4.             continue;
  5. }
  6. ... // the calculation comes here
  7.  
This coding should be placed inside the corresponding if-branch. Note that you have to change the name of the flag for the other two branches :-)
Oct 19 '06 #7
program works exactly the way it should now
thank you sooooo much =]
Oct 19 '06 #8

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
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.