Need help with script

Discussion in 'iOS Programming' started by drf1229, Jun 22, 2009.

  1. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #1
    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:mad:"%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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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.
     
  3. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #3
    You're also going to get nothing if the roots are imaginary.
     
  4. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  5. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  6. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  7. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #7
    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?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Put [ CODE ]...[ /CODE ] tags around your snippet, removing the spaces. These tags are easily accessible via the # icon in the toolbar.

    You want to look into formatting the output. Since stringWithFormat works very much like printf, you should be able to do "%.1f"
     
  9. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #9
    Thank you so much! Is there any way to tell when the answer is an integer and when its a long decimal?
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Sure. When the decimal part of the number is 0, it's an integer. :)
     
  11. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Yeah, I gave you some pseudo-code there. :)
     

Share This Page