473,756 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to stop program after input

22 New Member
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 4792
arne
315 Recognized Expert Contributor
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
wuzertheloser
22 New Member
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 Recognized Expert Contributor
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
wuzertheloser
22 New Member
Thanks! Program works with loop now! if i have any other questions, i'll post =]
Oct 19 '06 #5
wuzertheloser
22 New Member
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 Recognized Expert Contributor
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
wuzertheloser
22 New Member
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
1647
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 be easy to do, but when it waits for input the application apparently works in the background and when I hit the right key the loop was already processed several times instead of starting a new loop. Are the input and the loop working in...
26
4697
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
9459
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 the application is awaiting some key from the keyboard to proceed further. I do not recall how to construct this instruction. I know it is very simple,, but I just can not remmeber....
7
2890
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 <stdio.h> /* count lines in input */ main() { int c, nl;
4
3880
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, and search engines on PHP, I can't figure out how to stop PHP from rounding. I am trying to create a calculator that calculates a diameter * pi, and I got it, but I can't stop it from rounding to a few digits, which is very annoying. I'm...
1
2261
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 , int); void withdrawal(int , double , int); void deposit(int , double , int); int new_acct(int , double , int);
3
2169
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 of the input file is to be given on the command line. * All negative values in the vector shall then be replaced with their positive counterpart, using the standard algorithm for_each.
1
1472
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 then i input 2 again the output will be error. please help!
8
12617
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 an option to start, stop and reset the marquee. Now I have most of this done already, what I'm having problems with is that when i start the marquee it moves to the right, but i need to have it move from the bottom, upwards. heres my code (I'm not...
4
2094
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 is the logic of this? Here is the program: <html> <head> <script type="text/javascript"> var c=0; var t; function timedCount()
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.