quadratic equation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haidarrrr
    New Member
    • Oct 2015
    • 18

    quadratic equation

    Hi everyone !

    i'm a freshmen in c programming, and we got an assignment to create a program that spit out the roots of an quadratic equation!

    I've given it a try but it seems to be wrong somehow that's why i need your help! thanks


    the formula is x= (-1/2)*p +- sqrt(1/4)*p^2 -q) and its (p,q) is my in-data. if the equation does not have real root printf("itdoes not have real roots");

    my code

    obs its by calling a function !!
    //
    // by Haidar Wahid
    //
    //

    #pragma warning (disable:4996)
    #include <stdio.h>
    #include<stdlib .h>
    #include<math.h >

    int squareRoot_of(i nt p, int q, double a); // prototype

    int main() {
    int x, y;
    double z;
    z = 0.5;
    x = 55;
    y = 10;

    printf("%d\n%d\ n", squareRoot_of(x ,y,z));


    system("pause") ;
    }

    int squareRoot_of(i nt p, int q, double a) // def

    {


    return ((-a*p) +- sqrt(a*a*p*p - q));
    }
  • zuko32
    New Member
    • Oct 2015
    • 13

    #2
    Do you want to find real roots for this type of equation?
    a * x^2 + b * x + c = 0.

    If yes, I will try to give you an answer without revealing the solution.
    OK, you first should find the discriminent, say D, and this is
    D = b^2 - 4 * a * c.

    Now, you want to test cases for D. If D > 0, then that means that there are
    2 possible real roots. If D = 0, then there is only one real root.
    If D < 0, then you can just simply output that there are no real roots.
    Given that the formula for finding the roots, say x1, x2, if D > 0 is:

    x1 = (-b + sqrt(D)) / 2 * a
    x2 = (-b - sqrt(D)) / 2 * a

    and if D = 0 is(which is derived from the above formula):
    x = (-b) / 2 * a

    I think that you can make a good solution!

    Hope it helped!

    Comment

    • haidarrrr
      New Member
      • Oct 2015
      • 18

      #3
      thank you very much zuko32! I will start with it right away and hope it works !

      tanks again

      Comment

      • haidarrrr
        New Member
        • Oct 2015
        • 18

        #4
        #pragma warning (disable:4996)
        #include <stdio.h>
        #include<stdlib .h>
        #include<math.h >

        int squareRoot_of(i nt p, int q, double a, int d ,int x1, int x2); // prototype

        int main() {
        int x, y, c, v, g, h;
        double b;
        x = 7;
        y = 2;
        b = 0.5;
        g = 0;
        h = 0;
        v = ((b*b*x*x) - y);
        c = squareRoot_of( x,y,b,g,h,v);

        printf("%d%d\n" , c);


        system("pause") ;



        }

        int squareRoot_of(i nt p, int q, double a, int d, int x1, int x2) // def

        {

        d = ((a*a*p*p) - q);

        if (d >= 0) {

        printf("%d%d\n" ,p,q);
        }
        else
        {
        printf("there are no roots\n");
        }

        x1 = ((-a*p) + sqrt((a*a*p*p)) - q);

        x2 = (-a*p - sqrt((a*a*p*p) - q));

        return x1, x2;
        }


        what do u think zuko32?

        Comment

        • zuko32
          New Member
          • Oct 2015
          • 13

          #5
          Man, I am really glad I helped. However, to be honest, I cannot really understand your solution. :P
          Of course, I don't say it is right or wrong because I can't understand. ;)
          Your purpose is to solve an equation of this type? :
          a * x^2 + b * x + c = 0

          If yes, then you need three numbers to be input in the program and these are: a, b, c.
          In your initial post you state that you have only two numbers as input and these are (p,q).
          If you can, give some mathematical info of what you try to achieve.

          Please explain what you do or you want to do so I can offer more help if I can!! :)

          Comment

          • haidarrrr
            New Member
            • Oct 2015
            • 18

            #6
            Yes ofc mate! sorry for being unclear !

            the problem is that I'm trying to solve an equation of that kind
            (a * x^2 + b * x + c = 0) it's just that in Sweden we use (p,q) instead of (b,c)!

            the formula I've got is x= (-1/2)*p +- sqrt(1/4)*p^2 -q) witch I have been asked to develop a function for!so that I put numbers instead of a the Coefficients (p,q) so that the program can find out the roots if there is !

            I hope I'm a little more clear now !

            Comment

            • zuko32
              New Member
              • Oct 2015
              • 13

              #7
              Ooo, I think I understand. So, a = 1 by default, and you just have to give b,c or otherwise, p,q ?

              Comment

              • haidarrrr
                New Member
                • Oct 2015
                • 18

                #8
                exactly!! its just (p,q) that i have to enter in order to get roots!

                Comment

                • zuko32
                  New Member
                  • Oct 2015
                  • 13

                  #9
                  OK, so I will try to give you a better solution, because it is more general, I hope I will not complicate things, but since you are a programmer, you will probably get asked a lot more times for this solution. Also, I used the formula you gave me and it did not work(Personally , I see this formula for the first time). I ran your program and it gave me answers -72 and 60 which are not correct. Anyway, let's get started, I hope this is more helpful.
                  So, I will use the mathematical ideas I described in my first answer:

                  Code:
                  #include <stdio.h>
                  #include <math.h>
                  
                  int main(void) {
                  
                  	// Ok, so the first thing I do is to define the three values that I need and these are: a, b, c
                  	// I just define in the program, but you could just ask the user for input. Let's not complicate things.
                  	float a = 1.0; // This could change. I just use 1 by default. Also, I use float, because the input can be of course floating point.
                  			// For the sake of simplicity, you can just use ints.
                  	float b = 7.0; // If I am correct, this is the second value you give in your program that you presented. In other words, the p.
                  	float c = 2.0; // This is the third value, otherwise the q.
                  	float x1, x2; // Where we are going to store the roots.
                  	
                  	// So, now that we have the values, as I wrote you, we want to find the discriminent(D) and take the cases I described.
                  	float D = pow(b, 2) + (-4) * a * c;
                  
                  	// If D > 0, then I have two real roots
                  	if(D > 0) {
                  		x1 = ((-1) * b + sqrt(D)) / (2 * a);
                  		x2 = ((-1) * b - sqrt(D)) / (2 * a);
                  		printf("There are two real roots and these are: %f, %f\n", x1, x2);
                  	} else if(D == 0) { // i.e. I have only one root.
                  		x1 = ((-1) * b) / (2 * a);
                  		printf("There is a single root, which is: %f\n", x1);
                  	} else // There are no real roots
                  		printf("No real roots\n");
                  
                  	return 0;
                  }
                  I do not want to write spoilers but this is just something that you have to understand. This solution is a lot more universal.
                  Now that you have at least a solution, I will try to understand the formula you provided for maybe a solution closer to what you did.

                  Just read carefully the code. I put a lot of comments so you can understand every bit.
                  If you have any questions, feel free to ask.
                  Hope it helped!!

                  P.S. Sorry for being late, I had to finish some work also. :P

                  Comment

                  • haidarrrr
                    New Member
                    • Oct 2015
                    • 18

                    #10
                    Wow thank u man for your time I really appreciate it !!

                    what if i want to write the same code but by calling because that's was tricky part for me !!

                    Comment

                    • zuko32
                      New Member
                      • Oct 2015
                      • 13

                      #11
                      I suppose you mean calling a function to do what I did in main and storing somewhere the solutions you find instead of just printing them. First of all, it is wrong to do something like what you did here:
                      Code:
                      return x1, x2;
                      In C, you can't just directly return two values.

                      So, what are the possible solutions? Well, the most simple way I can think at this moment is to create an array in main(), pass it to a function and store the solutions there.
                      Something like that:
                      Code:
                      #include <stdio.h>
                      #include <math.h>
                      
                      void root(float sols[], float, float, float); // return nothing, take as arguments an array of floats and three float numbers
                      
                      int main(void) {
                      	
                      	float roots[3]; 	// This array has three elements. The first one is the first solution(if there is one). 
                      				// The second is the second solution(if there is one). Finally, the third is how many roots
                      				// did we find, so we can output the array properly(you will understand how this works below);	
                      	
                      	// Pass the array to the 'root', so that root can store the values(if there are any)
                      	root(roots, 1.0, 7.0, 2.0);
                      	/* Two calls to test the other two possibilities(one root and no roots)
                      	root(roots, 1.0, 2.0, 1.0); // one root
                      	root(roots, 1.0, 2.0, 7.0); // no roots
                      	*/
                      
                      	// Now, depending on what I have in the third item of the array, I will output the array
                      	if(roots[2] == 2) // the first two elements are the roots
                      		printf("There are two real roots and these are: %f, %f\n", roots[0], roots[1]);
                      	else if(roots[2] == 1)
                      		printf("There is one real root: %f\n", roots[0]);
                      	else // no solutions
                      		printf("No real roots\n");
                      
                      
                      	return 0;
                      }
                      
                      void root(float roots[], float a, float b, float c) {
                      
                      	// We are basically do the same procedure, but we just store the values in the array
                      	
                      	float D = pow(b, 2) + (-4) * a * c;
                      
                      	// If D > 0, then I have two real roots
                      	if(D > 0) {
                      		roots[0] = ((-1) * b + sqrt(D)) / (2 * a);
                      		roots[1] = ((-1) * b - sqrt(D)) / (2 * a);
                      		roots[2] = 2; // we found 2 roots 
                      	} else if(D == 0) { // i.e. I have only one root.
                      		roots[0] = ((-1) * b) / (2 * a);
                      		roots[2] = 1; // one root. I have to store it again in the third item(i.e. index 2)
                      	} else // There are no real roots
                      		roots[2] = 0; // No real roots
                      }
                      This is a clever way to obtain the number of roots you found and output them accordingly. You will see similar usage of arrays often.

                      Note, that most of the time, there are multiple ways of doing the same thing and this problem is not an exception. I just think that this is one of the simple solutions. Other require knowledge of the "advanced" concepts of C(like pointers and structs).

                      Anyway, I hope I helped.
                      For any questions, don't hesitate!

                      Comment

                      • haidarrrr
                        New Member
                        • Oct 2015
                        • 18

                        #12
                        oki ! I will give a try and start over ! would it be ok if i get back to you in case something go wrong?

                        Comment

                        • zuko32
                          New Member
                          • Oct 2015
                          • 13

                          #13
                          Yes, of course anytime! You can send me inbox message or even post here, if the question is relevant.

                          I would advice you, as you said, to use this information to build your own solution.
                          By no means should you just copy the code and not try on your own. In general, you should not copy code that you don't understand and forums are not for this purspose. The goal is to understand the idea of something and then working on it.

                          Finally, note that C has a lot of traps and it is usually difficult to come up with elegant solutions to a problem(especia lly in bigger problems), but this is something you build with experience.

                          Comment

                          • haidarrrr
                            New Member
                            • Oct 2015
                            • 18

                            #14
                            thank u for ur advice ! thats what i'm planing to do! i will try coding it as many times as it takes to understand it not just to finish the assignment!

                            Comment

                            • zuko32
                              New Member
                              • Oct 2015
                              • 13

                              #15
                              Great! So good luck and if ypu need something, just contact me.

                              Comment

                              Working...