View previous topic :: View next topic |
Author |
Message |
talkerzero Grandmaster Cheater Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Tue Apr 14, 2009 1:30 pm Post subject: Quadratic Solver |
|
|
This will solve the any quadratic equation using the quadratic formula given the values of A, B, and C.
Code: | #include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int discriminant = 0;
int bottom = 0;
int top[2] = {0, 0};
int solution[2] = {0, 0};
for(;;)
{
printf("Enter the values of a, b, and c: ");
scanf("%i %i %i", a, b, c);
discriminant = (b ^ 2) - (4 * a * c);
top[0] = (b - (2 * b)) + (discriminant ^ (1/2));
top[1] = (b - (2 * b)) - (discriminant ^ (1/2));
solution[0] = top[0] / 2 * a;
solution[1] = top[1] / 2 * a;
if(discriminant < 0)
printf("\tThe solution is undefined.\n\n");
else if(discriminant == 0)
printf("\tThe solution is %i.\n\n", solution[0]);
else if(discriminant > 0)
printf("\tThe solutions are %i and %i.\n\n", solution[0], solution[1]);;
}
return EXIT_SUCCESS;
} |
Any advice?
|
|
Back to top |
|
|
Jani Grandmaster Cheater Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Apr 15, 2009 2:14 pm Post subject: Re: Quadratic Solver |
|
|
talker0 wrote: | Code: | discriminant = (b ^ 2) - (4 * a * c);
top[0] = (b - (2 * b)) + (discriminant ^ (1/2));
top[1] = (b - (2 * b)) - (discriminant ^ (1/2)); | Any advice? | You're xor'ing there. Also remember the parentheses: talker0 wrote: | Code: | solution[0] = top[0] / 2 * a;
solution[1] = top[1] / 2 * a; |
|
talker0 wrote: | Code: | if(discriminant < 0)
printf("\tThe solution is undefined.\n\n"); |
| It isn't... :)
|
|
Back to top |
|
|
VolatileAce Cheater Reputation: 0
Joined: 19 Mar 2009 Posts: 30 Location: 0001001100110111
|
Posted: Wed Apr 15, 2009 3:44 pm Post subject: |
|
|
Code: | /**
* @author VolatileAce
*/
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
void main(){
double a, b, c;
cout << "Enter a, b and c seperated by space inbetween: ";
cin >> a >> b >> c;
if(!a){
cout << "The solution is undefined. (Reason: a = 0)" << endl;
exit(0);
}
c = b*b-4*a*c;
b = -b;
a *= 2;
if(c < 0){
cout << "The solution is not a real number. (Reason: b*b-4*a*c is negative)" << endl;
exit(0);
}
c = sqrt(c);
b = (b - c) / a;
c = c / a + b;
if(c == b)
cout << "The solution is " << b << "." << endl;
else
cout << "The solutions are " << c << " and " << b << "." << endl;
} |
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Apr 15, 2009 4:13 pm Post subject: |
|
|
void main(), really?
|
|
Back to top |
|
|
hacksign23 Master Cheater Reputation: 0
Joined: 26 Nov 2006 Posts: 404
|
Posted: Thu Apr 16, 2009 12:19 am Post subject: |
|
|
hmm isn't this just like -b +/- sq((b*b) - 4ac) = bla,/2c,,yes?
_________________
|
|
Back to top |
|
|
oib111 I post too much Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Apr 16, 2009 8:21 am Post subject: |
|
|
No. It's:
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
|
92Garfield I'm a spammer Reputation: 57
Joined: 20 Dec 2007 Posts: 5871 Location: Banana Republic Germany
|
Posted: Sun May 10, 2009 2:27 am Post subject: |
|
|
lol
ax² + bx + c = 0
That part is clear right?
and this is formular
p = b/a
q = c/a
why dont you just use cin/cout, i cant get this running
Jani wrote: | It isn't... |
if basic quantity are the real numbers then the solution set is empty
with complex numbers its just
sqrt(discrminant*-1)*i
just so noone can say I diddnt define i
i² = -1
i = sqrt(-1)
_________________
|
|
Back to top |
|
|
|