473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 4767
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

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: S. Nurbe | last post by:
Hi, I have programmed a while loop. At the end of this loop I want to stop it until someone hit a key. When someone hits the right key the loop shall start again. Actually I thought this would...
26
by: Ricardo | last post by:
I made a program that generate random numbers and put it in a listbox when the user click go. The problem is: how can i made a button stop, to stop the method that is running??? s...
1
by: Jim Heavey | last post by:
I am wanting to stop my console application and see the information which has been written to the console at a certain point in my program. I have seen people do some sort of keyboard prompt when...
7
by: randomtalk | last post by:
hello, recently i am reading "The C Programming Language, Second Edition", and for some reason, the sample program in there doesn't work as expected under GCC.. here is the code: #include...
4
by: Jackson Peebles | last post by:
Hey everyone! I'm a complete newbie to PHP, and am trying to teach myself how to make some scripts. So far I've done pretty good, but even after searching through all my books, articles, manuals,...
1
by: Tigerlily | last post by:
// Program to create new accounts, perform deposits, withdrawals, and bank //inquiries #include<iostream> #include<fstream> using namespace std; void menu(); int read_accts(int , double ,...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
1
by: rebellion | last post by:
write a c++ program that will continuously accept a number. the loop will stop if the user enter a number which is less of equal than the previous. ex. if i input 2 then my next input is 4 ...
8
praclarush
by: praclarush | last post by:
Ok, I'm new to JavaScript and I'm taking a class for it the assignment in it I'm supposed to create edit a pre-made page to display a marquee that automatically scrolls for the user, as well as give...
4
by: Rajneesh Chellapilla | last post by:
I wrote this program. Its kinda of strange when I make a reset function reset(){c=0} its doest reset the setTimeout. However if I directly pass c=0 to the onclick button it does reset the timer. What...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.