473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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(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
14 3184
zuko32
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
thank you very much zuko32! I will start with it right away and hope it works !

tanks again
Oct 30 '15 #3
#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
zuko32
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
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
zuko32
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
exactly!! its just (p,q) that i have to enter in order to get roots!
Oct 30 '15 #8
zuko32
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
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
zuko32
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
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
zuko32
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
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
zuko32
13
Great! So good luck and if ypu need something, just contact me.
Oct 30 '15 #15

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

Similar topics

3
by: parisgoblet | last post by:
hi, i'm a freshman with c++,i got a question from my assignment,does someone could give me some ideas for that. Given the following prototype of a function that solves the roots of a quadratic...
1
by: tedla | last post by:
i want a fragment of code that accepts the coefficient of quadratic equation from user and calculate the eqt'n.
6
by: chronoxx251 | last post by:
Hi, I'm doing a school project where I need to make a program to calulate the quadratic equation. Now, I already got that part taken care of with the following code: #include "stdafx.h" #include...
6
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a...
5
by: mulhadhevi | last post by:
Can anyone tell me how to write a program to find the roots of the quadratic equation using switch case.
2
by: ioannoual | last post by:
Hi...I 'am new in C and I want a program that solves a quadratic equation!! I try something by my self to write some code about that but I want you to help me writing a new one that will work!! ...
1
by: bbench123 | last post by:
Make a program that will ask for values of a quadratic equation (ax2+bx+c). the program must determine the roots of the equation using the quadratic equation determinants to distinguish if the roots...
1
by: candacefaye1 | last post by:
1. write a C++ program to decide if the coefficients of a quadratic equation have real roots. The three choices will be to write the message “zero divide” when A is zero, write the message “no real...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.