So, I'm 2 weeks into my first computer science course and we have an assignemt to write a program which accepts 3 real coefficients a, b, c of the quadratic equation: ax^2 + bx + c = 0 and outputs the real roots of the equation without failing using the quadratic formula:
x = (-b + or - sqrt(b^2 -4ac))/2a
So, I've come up with 5 cases where I need to give the input special attention:
case 1: a=0 and b=0 Here, ax^2 + bx + c = 0 can't be solved for x
case 2: a =0 and b !=0 Here, ax^2 + bx + c = 0 becomes a linear equation bx + c = 0 which can be solved by letting x = -c/b
case 3: a !=0 and b*b < 4ac Here, x would have complex roots which we're not messing with today!
case 4:if b is too big, b*b-4ac might equal b*b cuz if a real number is only allowed to have so many digits in c++ when you
subtract a very small number from it the only diffrence from b*b might be in the last digit and the computer will have cut it off. Our professor said that to avoid this, we should choose b such that b*b<=40ac.
case 5: a!=0 and b*b -4ac >= 0 and b*b<=40ac Here, everything is good to go and we can apply the quadratic formula to get 2 real roots.
So, to deal with these cases, I try to use just if statements. Everything complies, the program even outputs the right answers if you give it the correct input. The problem is that sometimes, when you give input that produces a case 1, 3 or 4 situation, it'll ask you to give it new input but then won't know what to do with it and the program just ends. Is it because I'm using just if statements and not if...else statements? Does the sequence of the statements matter if you're only using if statements? Sorry if this is really too much to be asking but I'm very stuck here! I thank everybody so much for trying to help me on this.
Here's the code:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
double a, b, c, x1, x2;
cout<<"Enter 3 real numbers from the keyboard."<<endl;
cin>>a>>b>>c;
if(a==0 && b==0)
{
cout<<"You have entered coefficients which cannot be solved for x."<<endl;
cout<<"So enter two new real numbers for a and b."<<endl;
cin>>a>>b;
}
if(a==0 && b!=0)
{
x1 = -c/b;
x2 = x1;
cout<<"x = "<<x1<<endl;
}
if((b*b-4*a*c)<0)
{
cout<<"The coefficients you entered would yield complex roots."<<endl;
cout<<"Try again by entering new values of a, b, and c."<<endl;
cin>>a>>b>>c;
}
if(b*b > abs(40*a*c))
{
cout<<"The value you entered for b is too large for the computer to"<<endl;
cout<<"produce an accurate answer. So enter a new value for b."<<endl;
cin>>b;
}
if(a!=0 && (b*b-4*a*c)>=0 && b*b<= abs(40*a*c))
{
x1 = (-b + sqrt(b*b - 4*a*c))/2*a;
x2 = (-b - sqrt(b*b - 4*a*c))/2*a;
cout<<"x = "<<x1<<endl<<"and"<<endl<<"x = "<<x2<<endl;
}
return 0;
}
I would strongly recommend that you put the input within a do while loop. Otherwise, you can provide enough bad input to never get caught.
I don't know what a "do while loop" is. I've only been at programming for around a few weeks. Can this program be written using just 'if' and/or 'if..else' statements and perhaps user-defined functions? If so, how do I re-enter the input if my original choices for a, b, and c won't allow the equation to be solved for x for whatever reason?
Your program executes sequentially. Once it reaches the first case the user is prompted for more input, and continues to the end of the program. There is nothing causing the first case to be reevaluated since, at this point, it was already evaluated. I suppose you could do it recursively (with user defined functions), but a loop would be the easiest. You cannot do this with just if/else statements.
If you don't know how to use loops, the other option is to perform the evaluation appropriate for the provided input (if possible). For example, if a=0 and b!=0 then x=-c/b is an easy calculation.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks