Hi every I'm new here and new to programming. : )

Here is a practice assignment I have completed but seemingly not correctly. Can anyone offer advice as to my error? It compiles correctly but using some test numbers I only ever recieve that the circle has no x intercepts

Text Problem
The standard form of an equation for a circle is (x − h)^2 + (y − k)^2 = r^X2 where (h,k) represents the center of the circle and r is the radius. The y-value of the equation becomes zero at the point of intersection with the x-axis. When the value of 0 is substituted for y, the equation can be simplified to a quadratic equation in standard form. From here, the value(s) of x can be resolved using the quadratic formula. Write a function that accepts the center point and radius of a circle and returns how many times the circle crosses the x-axis, if at all. If an intersection occurs, the function should return the x-value(s) as well. This new function should call the a quadraticRoots function you wrote(return the # of real roots and the assign root values to x1 and x2) to determine
My Code:

int quadraticRoots      (double a, double b, double c, double& x1, double& x2);
int circleIntersections (double h, double k, double r, double& x1, double& x2);


int quadraticRoots (double a, double b, double c, double& x1, double& x2)
 {
    int numberOfroots;
    double discriminant = ( (b * b) - (4.0 * a * c) );     //will tell how many real roots       


    if( discriminant < 0)
    {
        numberOfroots=0;  
    }

   else if( discriminant == 0)
    {
        numberOfroots=1;
        x1= (-b + ( sqrt( (b*b)-(4*a*c) ) ) ) /(2*a);

    }

    else 
    {
        numberOfroots=2;
        x1= (-b + ( sqrt( (b*b)-(4 * a * c) ) ) ) /(2*a);
        x2= (-b - ( sqrt( (b*b)-(4 * a * c) ) ) ) /(2*a);

    }
    return numberOfroots;

 }

int circleIntersections (double h, double k, double r, double& x1, double& x2)
 {

    if(quadraticRoots(h,k,r,x1,x2)==0)
        cout<<"No x intercepts occurs in this circle.";

    else if(quadraticRoots(h,k,r,x1,x2)<0)
        cout<<"There is one x intercept and it is: \n"<<x1;
        //(x - h)^2 + k^2= r^2

    else if(quadraticRoots(h,k,r,x1,x2)>0)
      {
        cout<<"There are two x intercepts: \n\n";
        cout<<x1<<" & "<<x2;
      }
    return 0;
 }
 int main()
 {
    double a=3, b=2,c=3,x1,x2,h,k,r;
    cout<<"The number of real roots are: ";
    cout<<quadraticRoots(a,b,c,x1,x2)<<"\n";

    cout<<"Real roots: "<<x1<<" & "<<x2<<"\n\n";
    cout<<"To determine the zero-points of a circle....\n"
        <<"Please enter '(h,k)' coordinates that are the circle's centerpoint: ";
        cin>>h>>k;
    cout<<"And please enter the circle's radius: ";
        cin>>r;

    circleIntersections (h,k,r,x1,x2);

    return 0;

 }

Your equality comparisons makes so sense in circleIntersections(). On line 40, your condition is else if(quadraticRoots(h,k,r,x1,x2)<0). Why are you checking to see if the number of roots is less than zero? Because quadraticRoots() only ever returns values of 0, 1, and 2, this if statement is never true. You probably should be checking to see if it's equal to 1.

You make a similar mistake on line 44. else if(quadraticRoots(h,k,r,x1,x2)>0). This statement is asking if there is more than zero roots, which will hold true if quadraticRoots() returns one or two. Instead, you want to check if the returned value is 2, if you want the code to work correctly.

Thank-you Tumlee

That was kind of an obvious mistake. I think I had forgot I was intending to compare the return of the quadraticRoots() with the h,k,r variables as parameters and instead tried to compare with the value of x1 in the the quadraticRoots(). I don't why though.Thanks for that. I think it works now. Does any one notice any other inconstences that might malign my output?

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.