PDA

View Full Version : Need help with script




drf1229
Jun 22, 2009, 10:50 AM
I am making an app that finds the roots of a quadratic equation. It looks like this:

ViewController.m-

//
// ViewController.m
// PercentFinder
//
// Created by Danny Flax on 6/21/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <stdio.h>

@implementation ViewController

-(IBAction)Solve(UIButton*)sender{


NSString*as=[A text];
int a=[as intValue];
NSString*bs=[B text];
int b=[bs intValue];
NSString*cs=[C text];
int c=[cs intValue];
int d=((-1*b)+sqrt((b*b)-4*(a*c)))/(2*a);
int e=((-1*b)-sqrt((b*b)-4*(a*c)))/(2*a);



[Answer setText:[NSString stringWithFormat:@"%d and %d", d,e]];






}


@end

For some odd reason, it returns 0 for the values of d and e no matter what I type. I'm just a beginner, so there probably is some obvious problem that I missed, and I would greatly appreciate it if somebody could help me.



dejo
Jun 22, 2009, 10:58 AM
First, I think you need to involve some floats in there somewhere. Otherwise, things like sqrt()s on ints are just going to give you unexpected results.

Second, I suggest enclosing your snippet in code tags.

jnic
Jun 22, 2009, 11:06 AM
You're also going to get nothing if the roots are imaginary.

drf1229
Jun 23, 2009, 08:18 AM
Which values should be floats?

drf1229
Jun 23, 2009, 08:18 AM
and how do I enclose snippets in tags?

drf1229
Jun 23, 2009, 08:19 AM
Sorry, i'm very new to this :(

drf1229
Jun 23, 2009, 08:40 AM
It worked! Thanks for your help. But I have one more problem. because the answer it a float, it shows up as -0.5000000000. How do I just make it -0.5?

dejo
Jun 23, 2009, 09:24 AM
and how do I enclose snippets in tags?
Put [ CODE ]...[ /CODE ] tags around your snippet, removing the spaces. These tags are easily accessible via the # icon in the toolbar.

But I have one more problem. because the answer it a float, it shows up as -0.5000000000. How do I just make it -0.5?
You want to look into formatting the output. Since stringWithFormat works very much like printf, you should be able to do "%.1f"

drf1229
Jun 23, 2009, 09:38 AM
Thank you so much! Is there any way to tell when the answer is an integer and when its a long decimal?

dejo
Jun 23, 2009, 09:41 AM
Thank you so much! Is there any way to tell when the answer is an integer and when its a long decimal?
Sure. When the decimal part of the number is 0, it's an integer. :)

drf1229
Jun 23, 2009, 10:03 AM
I meant in code lol

dejo
Jun 23, 2009, 10:04 AM
I meant in code lol
Yeah, I gave you some pseudo-code there. :)