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;
}

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.