By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,557 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,557 IT Pros & Developers. It's quick & easy.

quadratic equation

P: 18
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(int 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(int p, int q, double a) // def

{


return ((-a*p) +- sqrt(a*a*p*p - q));
}
Oct 30 '15 #1
Share this Question
Share on Google+
14 Replies


P: 13
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!
Oct 30 '15 #2

P: 18
thank you very much zuko32! I will start with it right away and hope it works !

tanks again
Oct 30 '15 #3

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

int squareRoot_of(int 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(int 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?
Oct 30 '15 #4

P: 13
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!! :)
Oct 30 '15 #5

P: 18
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 !
Oct 30 '15 #6

P: 13
Ooo, I think I understand. So, a = 1 by default, and you just have to give b,c or otherwise, p,q ?
Oct 30 '15 #7

P: 18
exactly!! its just (p,q) that i have to enter in order to get roots!
Oct 30 '15 #8

P: 13
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(void) {
  5.  
  6.     // Ok, so the first thing I do is to define the three values that I need and these are: a, b, c
  7.     // I just define in the program, but you could just ask the user for input. Let's not complicate things.
  8.     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.
  9.             // For the sake of simplicity, you can just use ints.
  10.     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.
  11.     float c = 2.0; // This is the third value, otherwise the q.
  12.     float x1, x2; // Where we are going to store the roots.
  13.  
  14.     // So, now that we have the values, as I wrote you, we want to find the discriminent(D) and take the cases I described.
  15.     float D = pow(b, 2) + (-4) * a * c;
  16.  
  17.     // If D > 0, then I have two real roots
  18.     if(D > 0) {
  19.         x1 = ((-1) * b + sqrt(D)) / (2 * a);
  20.         x2 = ((-1) * b - sqrt(D)) / (2 * a);
  21.         printf("There are two real roots and these are: %f, %f\n", x1, x2);
  22.     } else if(D == 0) { // i.e. I have only one root.
  23.         x1 = ((-1) * b) / (2 * a);
  24.         printf("There is a single root, which is: %f\n", x1);
  25.     } else // There are no real roots
  26.         printf("No real roots\n");
  27.  
  28.     return 0;
  29. }
  30.  
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
Oct 30 '15 #9

P: 18
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 !!
Oct 30 '15 #10

P: 13
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. void root(float sols[], float, float, float); // return nothing, take as arguments an array of floats and three float numbers
  5.  
  6. int main(void) {
  7.  
  8.     float roots[3];     // This array has three elements. The first one is the first solution(if there is one). 
  9.                 // The second is the second solution(if there is one). Finally, the third is how many roots
  10.                 // did we find, so we can output the array properly(you will understand how this works below);    
  11.  
  12.     // Pass the array to the 'root', so that root can store the values(if there are any)
  13.     root(roots, 1.0, 7.0, 2.0);
  14.     /* Two calls to test the other two possibilities(one root and no roots)
  15.     root(roots, 1.0, 2.0, 1.0); // one root
  16.     root(roots, 1.0, 2.0, 7.0); // no roots
  17.     */
  18.  
  19.     // Now, depending on what I have in the third item of the array, I will output the array
  20.     if(roots[2] == 2) // the first two elements are the roots
  21.         printf("There are two real roots and these are: %f, %f\n", roots[0], roots[1]);
  22.     else if(roots[2] == 1)
  23.         printf("There is one real root: %f\n", roots[0]);
  24.     else // no solutions
  25.         printf("No real roots\n");
  26.  
  27.  
  28.     return 0;
  29. }
  30.  
  31. void root(float roots[], float a, float b, float c) {
  32.  
  33.     // We are basically do the same procedure, but we just store the values in the array
  34.  
  35.     float D = pow(b, 2) + (-4) * a * c;
  36.  
  37.     // If D > 0, then I have two real roots
  38.     if(D > 0) {
  39.         roots[0] = ((-1) * b + sqrt(D)) / (2 * a);
  40.         roots[1] = ((-1) * b - sqrt(D)) / (2 * a);
  41.         roots[2] = 2; // we found 2 roots 
  42.     } else if(D == 0) { // i.e. I have only one root.
  43.         roots[0] = ((-1) * b) / (2 * a);
  44.         roots[2] = 1; // one root. I have to store it again in the third item(i.e. index 2)
  45.     } else // There are no real roots
  46.         roots[2] = 0; // No real roots
  47. }
  48.  
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!
Oct 30 '15 #11

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

P: 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(especially in bigger problems), but this is something you build with experience.
Oct 30 '15 #13

P: 18
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!
Oct 30 '15 #14

P: 13
Great! So good luck and if ypu need something, just contact me.
Oct 30 '15 #15

Post your reply

Sign in to post your reply or Sign up for a free account.