Quadratic Formula Program


Recommended Posts

Well, I wanted to see how far i had gotten with my learning, so i tried to make a quadratic formula program (in C++). However, the problem is, it is messing up (as always :)). Here is the program:

#include <iostream>

using namespace std;

int main()

{

int a; //Listing all the integers i'll be needing (can i lose a few?)

int b;

int c;

double d;

int d2;

double x;

double x2;

x = (-b + d)/2*a; //Listing what the doubles are (i wanted to use doubles

x2 = (-b - d)/2*a; //so i would know if it wasn't a perfect square).

d = (b*B)-(4*a*c); //Something is definately wrong here.

for (; ; ) {cout<<"Please enter the value of a\n";

cin>> a;

cin.ignore();

cout<<"Please enter the value of b\n";

cin>> b;

cin.ignore();

cout<<"Please enter the value of c\n";

cin>> c;

cin.ignore();

cout<<"The discriminant is "<< d <<"."; /*Always comes out as -18866095, regardless what the values of A, B, or C were.*/

cout<<"Please enter the square root of the discriminant.\n";

cin>> d2;

cin.ignore();

cout<<"X equals "<< x <<"and "<< x2 <<".\n";

cout<<"Press enter to enter new values\n";

cin.get();}

}

Whenever i get the output for x, x2, i get 2088809588 and -2088809762. Anyone see any flaws in the program? Also, one comment, i had to ask for the square root because i don't know how to "say" square root in C++, or even on the keyboard for that matter. For those who don't know, the quadratic equations is

-b +\- Sqrt<(b^2)-(4*a*c)>

or, in words, negative B plus and minus the square root of (B squared) minus (4 times A times C).

Thanks, i look forward to fixing the program.

Link to post
Share on other sites

Your first problem is that C++ doesn't have logic variables.

The operations in the program are evaluated in more-or-less the order written. That means that this block:

    int a; //Listing all the integers i'll be needing (can i lose a few?)
   int b;
   int c;
   double d;
   int d2;
   double x;
   double x2;
   x = (-b + d)/2*a; //Listing what the doubles are (i wanted to use doubles
   x2 = (-b - d)/2*a; //so i would know if it wasn't a perfect square).
   d = (b * b)-(4*a*c); //Something is definately wrong here.

assigns the values of x, x2, and d right there, using the current values of a, b, and c. The variables a, b, and c are uninitialized, so their values are random(-ish) and indeterminant. Basically whatever happens to be sitting in those memory addresses. So you're really generating low-quality pseudo-random numbers. The values happen to be consistent because of a quirk of the platform; on some platforms you would get different results each time the program was executed.

What you want to do is the move the calculations down into the loop, after the user input, so that the correct values will be used. In general form:

declare the variables
loop forever
   read input
   calculate solutions
   output
done

Your program would in fact work correctly in some languages (at least for one iteration). With some considerable effort it could be made to work in C++, but it would be unnatural. This is one of the fundamental features of procedural or imperative languages like C++, in which programs are sequences of operations, as opposed to, for example, sets of rules for solving equations. In short, it ain't math, so you can't get away with nearly as much.

Edited by jcl
Link to post
Share on other sites

Thanks....I finally finished it. I did what you said (and also did something smart and consolidated all the declarations into a few lines. I finally tested it with my homework, and it works perfectly....Now, homework will be a breeze...i won't have to do all the work in my head....

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...