Hey so my code below works. It finds the roots for values that are in the quadratic equation from a file.

What i want to do is write a function that will count the number of positive roots. What would be the easiest way to do this?

I was thinking i could add

``````if (root1 >= 0)

{
counter++
else if (root2 >= 0)

counter++

}``````

and then that would give me the total positive roots.

how would i do this in a function though?

here is the original code.

``````#include <fstream> // Header File for File Reading
#include <iostream> // Header File for General I/O-put
#include <math.h>
using namespace std; // Use namespacing

int main()
{

ifstream in_stream;
ofstream out_stream;

int a(0), b(0), c(0), Lasta(0), Lastb(0), Lastc(0);
double bsqrd, fourac, root, root1, root2;

in_stream.open("inputfile.txt");
while (1)
{
Lasta=a; Lastb=b; Lastc=c;

in_stream >> a >> b >> c;
if (a==Lasta && b==Lastb && c==Lastc){break;}

// calculations

bsqrd = b*b;
fourac = 4*a*c;
root = sqrt(bsqrd - fourac);
root1 = (-b + root)/4;
root2 = (-b - root)/4;

cout << "the three values for this quadratic are" << " " << a << " " << b << " " <<c << endl;
cout << "The value of x1 is: " << root1 << endl;
cout << "The value of x2 is: " << root2 << endl;

}

in_stream.close();

return 0;
}``````
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Ponomous

I don't understand the problem...

``````int NumberOfPositiveRoots(root1, root2)
{
int counter = 0;
if (root1 >= 0)
{
counter++;
}
if (root2 >= 0)
counter++;
}
return counter;
}``````

ok so i clearly am an idiot and did missed a letter when re typing the function and thats why it wasnt working for me.... sigh.... thanks for the help tho

I don't understand the problem...

``````int NumberOfPositiveRoots(root1, root2)
{
int counter = 0;
if (root1 >= 0)
{
counter++;
}
if (root2 >= 0)
counter++;
}
return counter;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.