Ok, so I am a bit new with the iPhone SDK and this is getting frustrating. I am trying to build an application where the user types in the values of a,b, and c for a quadratic equation and it gives them x. so far, this is what I have
HTML Code:
- (IBAction) solve {
//a is the string, afield is the text field, ia is double
a = afield.text;
ia = [a doubleValue];
b = bfield.text;
ib = [b doubleValue];
c = cfield.text;
ic = [c doubleValue];
ix1 = (-ib + sqrt((ib*ib)-4*(ia*ic)))/2*ia;
x1 = [NSString stringWithFormat:@"%i", ix1];
x1field.text = x1;
ix2 = (-ib - sqrt((ib*ib)-4*(ia*ic)))/2*ia;
x2 = [NSString stringWithFormat:@"%i", ix2];
x2field.text = x2;
x2field.text = [NSString stringWithFormat:@"%d", ix2];
root = ((ib*ib)-4*(ia*ic))/2*ia;
if (root == 0) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Oh No!" message:@"There is no real answer to this equasion." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
I don't get any errors, but the x1field and x2field values are always 0. I'm pretty sure the answer is something simple. Help would be greatly appreciated
Last edited by mikelowe; 12-13-2009 at 01:34 AM.
Reason: needed to fix something
You're using "%i" and "%d" in your string formatter, both of which are expecting integers, but ix1 and ix2 look like they're doubles. Try changing them to "%f".
You're using "%i" and "%d" in your string formatter, both of which are expecting integers, but ix1 and ix2 look like they're doubles. Try changing them to "%f".
I really doubt this works perfectly, because you have missing the parenthesis at the end of the assignment:
ix1 = (-ib + sqrt((ib*ib)-4*(ia*ic)))/2*ia;
The "2*ia" should be like this: "(2*ia)", because if you omit the parenthesis, it firstly divide the result from the left expression by 2, and then it multiply it all with 'ia'.
__________________
indie iPhone developer
Check out my website: http://www.glimsoft.com
and my iPhone app 'Quadratic Master' on the AppStore: AppStore link