Run time error.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//I am needing some help figuring out what I am doing wrong.  
//Quadratic.h
//----------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// class Quadratic declaration
class Quadratic
{
 private:
     double a, b, c;
 public:
      //constructor to initialize the private variables
     //post: Initialize the values of a, b, and c with 0's
        Quadratic();
     
     //Function to set the value of the member variables
     //pre: The parameters are values to be assigned to variables a, b, and c
     //post: Initialize the values of a, b, and c with passed values
     
     void set(double cdblA, double cdblB, double cdblC);

     //Function to get the value of the member variable a
     //post: Return the value stored in variable a
     
     double get_a() const;

     //Function to get the value of the member variable b
     //post: Return the value stored in variable b
     
     double get_b() const;

     //Function to get the value of the member variable c
     //post: Return the value stored in variable c
 
     double get_c() const;

     //Function to evaluate the value of the quadratic expression
     //for given value of x
     //pre: The parameter is value x
     //post: Evaluate the quadratic expression and return the value
    
     double evaluate(double x) const;

     //Function to find the number of real roots to the quadratic equation
     //post: Return the number of real roots
     
     int real_root () const;

     //Function to find larger real root of the quadratic equation
     //post: Return the larger real root
    
     double larger_root () const;

    //Function to find smaller real root of the quadratic equation
    //post: Return the smaller real root

     double smaller_root () const;

     void display() const;

Quadratic operator + (const Quadratic & rhs);
Quadratic operator * (const double);
};



//------------------------------Quadratic.cpp Program------------------------------
//--------------------------------------------------------------------------------------
#include "Quadratic.h"
#include <assert.h>
#include <cmath>
//constructor to initialize the private variables
//pre: values to be assigned (zeros) to a, b, and c are passed
//post: Initialize the values of a, b, and c with 0's

Quadratic::Quadratic() : a(0), b(0), c(0) {}

//Function to set the value of the member variables
//pre: values to be assigned to a, b, and c are passed
//post: Initialize the values of a, b, and c with passed values

void Quadratic::set(double cdblA, double cdblB, double cdblC)
{
     a = cdblA;
     b = cdblB;
     c = cdblC;
}

//Function to get the value of the member variable a
//post: Return the value stored in variable a

double Quadratic::get_a() const
{
     return a;
}

//Function to get the value of the member variable b
//post: Return the value stored in variable b

double Quadratic::get_b() const
{
     return b;
}

//Function to get the value of the member variable c
//post: Return the value stored in variable c

double Quadratic::get_c() const
{
     return c;
}

//Function to evaluate the value of the quadratic expression
//for given value of x
//pre: The parameter is value x
//post: Evaluate the quadratic expression and return the value

double Quadratic::evaluate(double x) const
{
     return (a * x * x) + (b * x) + c;
}

//Function to add two quadratic expressions
//pre: The parameter the objects q1 and q2
//post: Add q1 and q2 and store the result in object q

Quadratic operator+(const Quadratic & q1, const Quadratic & q2)
{
     double a = q1.get_a() + q2.get_a();
     double b = q1.get_b() + q2.get_b();
     double c = q1.get_c() + q2.get_c();
     Quadratic q;
     q.set(a, b, c);
     return q;
}

//Function to multiply the quadratic expression with a constant
//pre: r has been assigned
//post: Multiply q with a constant r and store it in q1, return q1

Quadratic operator*(double r, const Quadratic & q )
{
     double a = r * q.get_a();
     double b = r * q.get_b();
     double c = r * q.get_c();
     Quadratic q1;
     q1.set(a, b, c);
     return q1;
}

//Function to find the number of real roots to the quadratic equation
//post: Return the number of real roots

int Quadratic::real_root() const
{
     double x1 = 0.0;
     double x2 = 0.0;
     int real_root;
     //If a, b and c are all zero, then every value of x is a real root.
     if(a == 0 && b == 0 && c == 0)
     {
          real_root = -1;
     }
     //If a and b are zero and c is non-zero, then there are no real roots.
     else if(a == 0 && b == 0 && c != 0)
     {
          real_root = 0;
     }
     //If a is zero and b is non-zero, then the only real root is x = -c/b.
     else if(a == 0 && b != 0)
     {
          real_root = 1;
     }
     //If a is non-zero and b^2 < 4ac, then there are no real roots.
     else if(a != 0 && b*b < (4*a*c))
     {
          real_root = 0;
     }
     //If a is non-zero and b^2 = 4ac, then there is one real root x = -b/2a.
     else if(a != 0 && b*b == (4*a*c))
     {
          real_root = 1;
     }
     //If a is non-zero and b2 > 4ac, then there are two real roots.
     else if(a != 0 && b*b > (4*a*c))
     {
          real_root = 2;
     }
     return real_root;
}

//Function to find larger real root of the quadratic equation
//post: Return the larger real root

double Quadratic::larger_root() const
{
     double larger;
     assert(real_root()>0);
     //If a is zero and b is non-zero, then the only real root is x = -c/b.
     if(a == 0 && b != 0)
          larger = -c/b;
     else if(a != 0 && b*b == (4*a*c))
          larger = -b/2*a;
     //If a is non-zero and b2 > 4ac, then there are two real roots.
     else if(a != 0 && b*b > (4*a*c))
          larger = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
     return larger; }

//Function to find smaller real root of the quadratic equation
//post: Return the smaller real root

double Quadratic::smaller_root()const
{      double smaller;
     assert(real_root()>0);
     //If a is zero and b is non-zero, then the only real root is x = -c/b.
     if(a == 0 && b != 0)
          smaller = -c/b;
     else if(a != 0 && b*b == (4*a*c))
          smaller = -b/2*a;
     //If a is non-zero and b2 > 4ac, then there are two real roots.
     else if(a != 0 && b*b > (4*a*c))
          smaller = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);
     return smaller; }

//Function to display quadratic equation
//post: display the quadratic equation in standard form

void Quadratic::display() const
{      cout << a << " X^2 + "<< b << " X + " <<c; }
Last edited on
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
//The driver for this:  driver.cpp
//----------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include "Quadratic.h"

//***************************************************************
// Function: main
//***************************************************************
int main()

{
     //create object of type class Quadratic
     Quadratic q,res;
     double a, b, c, value;
     char ch = 'y';
     do
     {
          //prompt the user to enter values of a, b, and c
          //read the values entered by the uses
          cout<<"Enter values of co-efficient of quadratic equation..."<<endl;
          cout<<"a: ";
          cin>>a;

          cout<<"b: ";
          cin>>b;

          cout<<"c: ";
          cin>>c;

          //assign the values
          q.set(a,b,c);

          //display quadratic equation
          cout<<"The quadratic equation is ";
          q.display();
          cout<<" ...!"<<endl;

          //display roots
          cout<<"Larger root is : "<<q.larger_root()<<endl;
          cout<<"Smaller root is : "<<q.smaller_root()<<endl;
          cout<<"Do you want to find roots to another equation "
              <<"(press 'y' to continue) : ";
          cin>>ch;
     }
          while(ch=='y'||ch=='Y');

     //display quadratic equation
     cout<<"The quadratic equation is ";
     q.display();
     cout<<"!"<<endl;
     cout<<"Enter a value to multiply : ";
     cin>>value;
     res = value*q;
     cout<<"The resultant quadratic equation is ";
     res.display();
     cout<<" ...!"<<endl;
     cout<<"Enter a value to evaluate : ";
     cin>>value;
     cout<<"The result is "<<q.evaluate(value)<<endl;

     //to halt the system
     system("PAUSE");
     return 0;
}
Last edited on
What error message are you getting?

You have this assert statement assert(real_root()>0); that the real_root function should return a value greater than 0, but that function will actually return 0 in some cases. That might be where you're seeing the error.

In main I'd include the .h file, not the .cpp file.

If you can edit your post, highlight the code part, then click the <> button in the Format palette to the right of the post, that will format the code and make it a lot easier to read :)
Last edited on
I am needing some help figuring out what I am doing wrong.

That's not a very helpful description of your problem.
Exactly what is not working correctly? Be specific.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Thanks for the formatting tip!

I have changed the the main to the .h instead of the .cpp and now I am getting the following error:

[Linker error] undefined reference to `Quadratic::Quadratic()'
[Linker error] undefined reference to `Quadratic::Quadratic()'
[Linker error] undefined reference to `Quadratic::set(double, double, double)'
[Linker error] undefined reference to `Quadratic::display() const'
[Linker error] undefined reference to `Quadratic::larger_root() const'
[Linker error] undefined reference to `Quadratic::smaller_root() const'
[Linker error] undefined reference to `Quadratic::display() const'
[Linker error] undefined reference to `operator*(double, Quadratic const&)'
[Linker error] undefined reference to `Quadratic::display() const'
[Linker error] undefined reference to `Quadratic::evaluate(double) const'
ld returned 1 exit status
You didn't quite get the code tags right. Don't put your code after the --- separator. Doing so turns off the keyword highlighting. Simply use
[code]

// your code here
[/code]


Your linker errors indicate you did not compile quadratic.cpp.

I have changed the the main to the .h instead of the .cpp

Huh? Your main should never be in a .h file.

Lines 69-69 in first snippet, shouldn't these operator declarations be inside your class declaration for Quadratic?



Last edited on
I have fixed the formatting.

my main was not in the .h. I had included the .cpp instead of the .h in the main driver program. I have corrected that.

Thanks for fixing the formatting.

What problem are you having now?
Did you get the linker errors fixed?

Compiles fine for me.

Enter values of co-efficient of quadratic equation...
a: 1
b: 2
c: 3
The quadratic equation is 1 X^2 + 2 X + 3 ...!
Larger root is : 2.06919e+238
Smaller root is : 2.06919e+238

Outputs garbage values for larger_root and smaller_root.

Note that in larger_root() lines 202, 204, 207 and smaller_root() lines 218, 220, 223, all three if statements can be false. When this is the case, larger (or smaller) is returned, but the value is uninitialized resulting in garbage being displayed.
I am getting this now....


I even re copied what was on here and re complied it... I am at a lost now....

any suggestions? and thanks for taking the time to help me..

C:\Users\aurjagui\Desktop\Program\driver.cpp In function `int main()':
14 C:\Users\aurjagui\Desktop\Program\driver.cpp `Quadratic' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
14 C:\Users\aurjagui\Desktop\Program\driver.cpp expected `;' before "q"
21 C:\Users\aurjagui\Desktop\Program\driver.cpp `cout' undeclared (first use this function)
21 C:\Users\aurjagui\Desktop\Program\driver.cpp `endl' undeclared (first use this function)
32 C:\Users\aurjagui\Desktop\Program\driver.cpp `q' undeclared (first use this function)
54 C:\Users\aurjagui\Desktop\Program\driver.cpp `res' undeclared (first use this function)
As I said above, your code compiled and ran for me.

From the errors you posted, it looks like you're missing some of the header files in driver.cpp. Since cout and endl are undefined, it would seem line 3 is missing. Since Quadratic is undefined, is would seem line 7 is missing.
Line numbers refer to the driver.cpp you posted above.

When I ran this yesterday, it crashed on these values - a=3, b=5, c=125. That falls under this part of the if/else section in the real_root() function that assigns 0 to the real_root variable that's returned to the calling function.

lines 177-180 above
1
2
3
4
     else if(a != 0 && b*b < (4*a*c))
     {
          real_root = 0;
     }


1
2
3
4
double Quadratic::larger_root() const
{
     double larger;
     assert(real_root()>0);


The larger_root() and smaller_root() functions assert that the value returned from real_root() is going to be greater than 0, so if the value returned is 0, that assert expression is going to be false. It's my understanding that if the assert expression is false - the program will terminate.

http://www.cplusplus.com/reference/cassert/assert/
Evaluate assertion
If the argument expression of this macro with functional form compares equal to zero (i.e., the expression is false), a message is written to the standard error device and abort is called, terminating the program execution.


If the larger_root() and smaller_root() functions should be able to handle a value of 0, maybe the assert statement needs to change?
I am getting these errors:

5 C:\Users\aurjagui\Desktop\Program\driver.cpp In file included from C:\Users\aurjagui\Desktop\Program\driver.cpp
97 C:\Users\aurjagui\Desktop\Program\Quadratic.h `Quadratic Quadratic::operator+(const Quadratic&, const Quadratic&)' must take either zero or one argument
99 C:\Users\aurjagui\Desktop\Program\Quadratic.h `Quadratic Quadratic::operator*(double, const Quadratic&)' must take either zero or one argument
C:\Users\aurjagui\Desktop\Program\driver.cpp In function `int main()':
84 C:\Users\aurjagui\Desktop\Program\driver.cpp no match for 'operator*' in 'value * q'
If you're overriding a binary operator as a free function, it takes two arguments.

If you're overriding it as a member of the class, it only takes one argument - because this is the first operand.
It looks like you moved the declarations for overloading the + and * operators inside your class declaration (lines 63-64).

As MikeyBoy pointed out, when overloading operators inside a class, your operator functions should be:

1
2
3
4
5
6
7
class Quadratic
{
public: 
...
 Quadratic operator + (const Quadratic & rhs);
 Quadratic operator * (const double);
};


Don't forget to change your function implementations accordingly.


Last edited on
well that helped now I am seeing only one error:

C:\Users\aurjagui\Desktop\Program\driver.cpp In function `int main()':
84 C:\Users\aurjagui\Desktop\Program\driver.cpp no match for 'operator*' in 'value * q'
Hi,

Be very careful with double variables, they are not stored exactly (they are basically binary fractions) so lines 202 and similar will not give the desired results.

To check whether some variable equals 0.0, decide on some precision value that is suitable - 0.001 say - then check whether the value is between -0.001 and + 0.001, then it is "equal" to zero. Do something similar to check equality on other values.

This might help when you do the assert statements.

Also investigate the use of initialiser lists for the constructors - might save on all those set functions, and can get rid of Quadratic::set . Have another constructor which takes arguments.

Don't have using namespace std; Google why this is so.

Got to go !
Last edited on
Topic archived. No new replies allowed.