-1

I have to add the roots to an custom datatype which is roots in this case. (This is what I think about this issue)

So how do I do this? Here is my code:

#include <math.h>

typedef struct { double m, n; } roots;

roots *quadratic_formula(int a, int b, int c) {

    double root1,root2;
  
    root1 = ((-b)+sqrt((b*b)-4*a*c))/(2*a);
    root2 = ((-b)-sqrt((b*b)-4*a*c))/(2*a);
    
    return ;
}
  • Simply declare an instance of type `roots` and initialize the members. – Harith Aug 29 '23 at 19:03
  • I dont actually know anything about typedef , it will be easier to understand if you can provide the code. – Igneous Chunk Aug 29 '23 at 19:06
  • 1
    @ikegami already did. But this might help: https://stackoverflow.com/q/1675351/20017547 – Harith Aug 29 '23 at 19:07
  • 1
    You can't return a pointer to a `struct` here unless you allocate dynamic memory for it. For a small `struct` like that it would be easier to have say `roots result; ... return result;` and remove the `*` from the function definition. – Weather Vane Aug 29 '23 at 19:07
  • It might be better to pass a pointer argument to the caller's `struct` and make the function type `bool` so that it can `return false` when *b² < 4ac*. – Weather Vane Aug 29 '23 at 19:45

2 Answers2

6

This creates a type called Roots:

typedef struct { double m, n; } Roots;

(I renamed the type to Roots to allow a variable named roots.)

As always, you can create a variable of that type using Type var;.

Roots roots;

Then, it's just a question of assigning to the members of the underlying struct.

roots.m = ((-b)+sqrt((b*b)-4*a*c))/(2*a);
roots.n = ((-b)-sqrt((b*b)-4*a*c))/(2*a);

You can even combine the variable declaration and the initialization.

Roots roots = {
   ((-b)+sqrt((b*b)-4*a*c))/(2*a),
   ((-b)-sqrt((b*b)-4*a*c))/(2*a),
};

Finally, returning the variable can be done using

return roots;

You'll need to declare the function with the correct return type. (There's no pointer involved here.)

Roots quadratic_formula(int a, int b, int c) {
   double t1 = -b;
   double t2 = sqrt( b*b - 4*a*c );
   double t3 = 2*a;

   Roots roots = {
      ( t1 + t2 ) / t3,
      ( t1 - t2 ) / t3,
   };

   return roots;
}

One could even use an anonymous variable.

Roots quadratic_formula(int a, int b, int c) {
   double t1 = -b;
   double t2 = sqrt( b*b - 4*a*c );
   double t3 = 2*a;

   return (Roots){
      ( t1 + t2 ) / t3,
      ( t1 - t2 ) / t3,
   };
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

Firstly, your data type inputs (int) are probably not what you want. Attempting to compute sqrts or divisions using integers is not going to end well. Try using double instead?

typedef struct { double m, n; } roots;

roots quadratic_formula(double a, double b, double c) {

    double root1,root2;
  
    root1 = ((-b)+sqrt((b*b)-4*a*c))/(2*a);
    root2 = ((-b)-sqrt((b*b)-4*a*c))/(2*a);
    
    return {root1, root2};
}

Now for the bigger problems.

(b*b)-4*a*c) can be negative, in which case the quadratic does not have a solution.

typedef struct { double m, n; int has_solution; } roots;

roots quadratic_formula(double a, double b, double c) {

    double root1,root2;

    double discriminant = (b*b)-4.0*a*c);
    if(discriminant < 0) {
        return (roots) { 0, 0, 0 };
    }

    root1 = ((-b)+sqrt(discriminant)/(2*a);
    root2 = ((-b)-sqrt(discriminant)/(2*a);
    
    return {root1, root2, 1};
}

If a is equal to zero, the equation is not a quadratic, and will need to be solved as a linear equation. If b is also zero, the equation has no solution.

typedef struct { double m, n; int has_solution; } roots;

roots quadratic_formula(double a, double b, double c) {

    // if linear
    if(a == 0) {
        if(b == 0) return {0,0,0}; // no solution
        double root = -c / b;
        return {root, root, 1};
    }

    double root1,root2;

    double discriminant = (b*b)-4.0*a*c);

    // quadratic is always above the y = 0 line
    if(discriminant < 0) {
        return (roots) { 0, 0, 0 };
    }

    root1 = ((-b)+sqrt(discriminant)/(2*a);
    root2 = ((-b)-sqrt(discriminant)/(2*a);
    
    return {root1, root2, 1};
}
robthebloke
  • 9,331
  • 9
  • 12
  • Actually the problem i was solving already provided the condition that inputs will be non-zero ints and there will never be imaginary roots. – Igneous Chunk Aug 31 '23 at 06:03