Help With Quadratic Code

Hey there and thanks for any help.
so I've just started programming kinda and i wanted to make a function so i thought a program that does the quadratic formula to find the roots of a polynomial would be cool. so the code takes in A B and C and submits them in the form of Ax^2 + Bx + C. Right now its in very early for so its not cluttered with cout prompts, so it just ask A? B? C? with input each time. I then run it through a function with an if else loop to test if the upper half of the quadratic formula is greater than 0 so it can be square rooted. The the rest of the program runs it through a B + sqrt and B - sqrt and returns a number.

It looks ok to me but i get an error at thread 1 breakpoint 1?
> 0x100000e2d <+13>: movl -0xc(%rbp), %edx
and says its at the SqrtTest(int, int, int) function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
//  main.cpp
//  QuadraticProjct
//
//  Created by WIlliam on 5/9/17.
//  Copyright © 2017 WIlliam. All rights reserved.
//

#include <iostream>
#include <cmath>
using namespace std;

int SqrtTest(int, int, int);
int QuadFnX1(int, int, int);
int QuadFnX2(int, int, int);

int SqrtTest(int ATest, int BTest, int CTest)
{
   int SqrtMath;
    SqrtMath = ((BTest * BTest)-(4*ATest*CTest));
  
    if ( SqrtMath < 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int QuadFnX1(int A, int B, int C)
{
    int SqrtMath, BSqrtMath,  A2, XPlus;
    SqrtMath = sqrt((B*B) - (4*A*C));
    cout<<SqrtMath<<endl;
    BSqrtMath = (-B + SqrtMath);
    cout<<BSqrtMath<<endl;
    A2 = (2*A);
    cout<<A2<<endl;
    XPlus = (BSqrtMath / A2);
    cout<<XPlus<<endl;
    return XPlus;
}

int QuadFnX2(int A, int B, int C)
{
    int SqrtMath, BSqrtMath, A2, XMinus;
    SqrtMath = sqrt((B*B) - (4*A*C));
    BSqrtMath = (-B - SqrtMath);
    A2 = (A * 2);
    XMinus = (BSqrtMath / (A2));
    return XMinus;
}


int main()
{
    int A, B, C;
    cout<<"A : ";
    cin>> A;
    cout<<endl<<"B : ";
    cin>>B;
    cout<<endl<<"C : ";
    cin>>C;
   
    if (SqrtTest(A, B, C) == true)
        {
        cout<<"sorry"<<endl;
        return 0;
        }
  
    else
    if (SqrtTest(A, B, C) == false)
        {
            int X1, X2;
            X1 = QuadFnX1(A, B, C);
            X2 = QuadFnX2(A, B, C);

            cout<<" ( X + "<<X1<<" )( X + "<<X2<<" )"<<endl;
            return 0;
        }
    
    return 0;
}
make the test function type bool, not int. This isnt your error, but its cleaner.

I don't see an error... it worked for me, both cases (failed test and passed it).

It won't give the right answer until you use double instead of int, though.
And consider complex soon, its a type that you CAN take the sqrt of even if negative...

Last edited on
Hello willmann,

based on what jonnin said and expanding on that this is what you could do with your program. Rather than a long drawn out message I made comments in the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cmath>

using namespace std;  //  Helpful in the beginning, not good practice

// These prototypes are not needed with the functions defined before main. Useful if functions defined after main.
//int SqrtTest(int, int, int);
//int QuadFnX1(int, int, int);
//int QuadFnX2(int, int, int);

// <--- Changes all "int"s to "bool"s except next function and main.

bool SqrtTest(double ATest, double BTest, double CTest)  // <--- Changes to bool.
{
	double SqrtMath;
	SqrtMath = ((BTest * BTest) - (4 * ATest * CTest));

	if (SqrtMath < 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

double QuadFnX1(double A, double B, double C)
{
	double SqrtMath, BSqrtMath, A2, XPlus;
	SqrtMath = sqrt((B*B) - (4 * A*C));
	std::cout << SqrtMath << std::endl;
	BSqrtMath = (-B + SqrtMath);
	std::cout << BSqrtMath << std::endl;
	A2 = (2 * A);
	std::cout << A2 << std::endl;
	XPlus = (BSqrtMath / A2);
	std::cout << XPlus << std::endl;
	return XPlus;
}

double QuadFnX2(double A, double B, double C)
{
	double SqrtMath, BSqrtMath, A2, XMinus;
	SqrtMath = sqrt((B*B) - (4 * A*C));
	BSqrtMath = (-B - SqrtMath);
	A2 = (A * 2);
	XMinus = (BSqrtMath / (A2));
	return XMinus;
}

int main()
{
	double A, B, C;
	std::cout << "\n A : ";
	std::cin >> A;
	std::cout << "\n B : ";  // <--- Changes the "endl" and used the "\n".
	std::cin >> B;
	std::cout << "\n C : ";
	std::cin >> C;

	std::cout << std::fixed << std::showpoint << std::setprecision(8);  // <---Added to adjust number of decimal points.

	if (SqrtTest(A, B, C))  // <--- Since a bool is returned the "== true" is not needed.
	{
		std::cout << "\n sorry" << std::endl;
		// <--- The "return 0;" here is redundant because when the block ends it goes to the end of the program. Same for the else part.
	}

	else
	//  <--- The if statement that was here is redundant and not needed.
	{
		double X1, X2;
		X1 = QuadFnX1(A, B, C);
		X2 = QuadFnX2(A, B, C);

		std::cout << " ( X + " << X1 << " )( X + " << X2 << " )" << std::endl;
	}

	std::cin.get();  // <--- Used to pause before program ends.
	return 0;
}


This is just another way to write your program. There is nothing wrong with most of the code you have except what jonnin has noted.

Hope that helps,

Andy

P.S. If you have some test and output it would be helpful to see if the program is working correctly.
Last edited on
Thanks for all the help guys, I changed the int to doubles which is how i had it initially but thought it might have been the problem. idkwhy.
why change to bool? is it because it returns a true/false? The function works now after changing to bool and back to double so maybe just my compiler Xcode wasn't doing something right. ty again though
Yes, if you are using the keywords "true" and "false" that should be tied to the type bool. On every compiler I have seen, those are just integers (usually 1 and 0 respectively) but its more correct to keep it tied to the bool type. I am not a standards guru, what you did could be undefined behavior, which includes crashing and working as 2 extremes of "undefined".

Glad it worked for you!

Topic archived. No new replies allowed.