Home > Software engineering >  How can I return additional x1 and x2 values in quadratic equation function after already returning
How can I return additional x1 and x2 values in quadratic equation function after already returning

Time:05-11

Good morning,

I'm so stuck solving this problem. Here's content of the task:

"Create a static method double SolvingSquare (double a, double b, double c,? x1,? x2) returning the number of solutions, and in x1 and x2 possible solutions."


I already made it to return number of solutions, but I don't know how to return x1 and x2 after already returning number of solutions. I tried to write "? x1, ? x2" arguments, but then red underline appears. I'm so confused with this one.

static void Main(string[] args)
{

Console.WriteLine($"Number of Solutions: {Method.SolveSquare(1, 3, 1)}");

}
class Method
    {
        public Method()
        {}

        public static double SolveSquare(double a, double b, double c)
        {
            double delta = (b * b) - (4 * a * c);              
            double squareDelta = Math.Sqrt(delta);
            double x1 = (-b   squareDelta) / (2 * a);
            double x2 = (-b - squareDelta) / (2 * a);

            if(delta < 0)
            {
                return 0;

            } return (delta == 0) ? 1 : 2;
}

CodePudding user response:

In order to not spoil this completely, but still help you:

You probably want something like this ...

double solutions = Solve(1,3,1, out double x1, out double x2);
Console.WriteLine($"We have {solutions} solutions, which are x1={x1} and x2={x2}");

// And the Solve Function:
public static double Solve(double a, double b, double c, out double x1, out double x2)
{
    // Implementation up to you, but somewhere must be:
    x1 = // Solution for x1;
    x2 = // Solution for x2;
}

Another unrelated tip: if delta is <0, you don't need to do the rest of the calculations, actually. You can set x1 and x2 to double.NaN and return 0.

https://dotnetfiddle.net/VJch0u

  • Related