Creating a Simple Quadratic Equation Solver

Discussion in 'iOS Programming' started by Mr.Faisal, Jun 28, 2011.

  1. Mr.Faisal, Jun 28, 2011
    Last edited: Jun 28, 2011

    Mr.Faisal macrumors newbie

    Joined:
    Jun 28, 2011
    #1
    Hello everyone.

    I am new to Xcode. However, I used to code for visual basic before and I wonder if that will help me to get along with iPhone development.

    I am trying to create an app that is related to mathematics, but I'm stuck and I need some help in coding.

    So here is the main view of the application

    [​IMG]

    I want the user to insert values for A, B, and C. Then, when the button 'Solve' is tapped, the two values for X1 and X2 will be calculated.

    So here is my .h file where I specified the textfields and labels

    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface SecondViewController : UIViewController {
        
        IBOutlet UILabel *label1;
        IBOutlet UILabel *label2;
        IBOutlet UITextField *valueA;
        IBOutlet UITextField *valueB;
        IBOutlet UITextField *valueC;
    }
    
    -(IBAction)solve;
    
    @end
    And my problem now is in the .m file where I have to insert the code that will solve for the values of X. In visual basic, this was pretty easy, but here I am a bit confused.

    I want the button 'Solve' to do the following:

    Code:
    label1 = ( -(valueB) + ( (valueB)^2 - (4*valueA*valueC) )^0.5 ) / (2*valueA)
    label2 = ( -(valueB) - ( (valueB)^2 - (4*valueA*valueC) )^0.5 ) / (2*valueA)
    
    Oh, and (if possible) I want a pop-up message that will ask the user to fill in all the textfields in case one of them is left empty. I think I know the code for the pop-up message, but I don't know where to place it.

    Code:
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"You must fill in all the textfields" delegate:self cancelButtonTitle:@"Ok" , nil];
    
    Anyone can help me with this?
    Thanks :)
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    It might help if you tell us what resources you are using to learn Objective-C and Xcode.

    B
     
  3. gibbz macrumors 68030

    Joined:
    May 31, 2007
    #3
    Your equations should be:

    Code:
    label1 = ( -(valueB) + ( (valueB)^2 - (4*valueA*valueC) )^0.5 ) / (2*valueA)
    label2 = ( -(valueB) - ( (valueB)^2 - (4*valueA*valueC) )^0.5 ) / (2*valueA)
    
    As to the rest, I would echo balamw.
     
  4. Mr.Faisal thread starter macrumors newbie

    Joined:
    Jun 28, 2011
    #4
    There is no specific source actually.
    I mainly use YouTube and Google.
    If you have a website that you think is helpful I'll be glad to have a look on it.

    Oh, you are absolutely correct!
    I forgot about the negative sign.
    Thanks man :)
     
  5. jnoxx, Jun 28, 2011
    Last edited by a moderator: Jun 28, 2011

    jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    U have an IBAction on your Solve button.
    Press there
    Code:
    if ([valueA.text isEqualToString=@""] || [valueC.text isEqualToString=@""]) {
     Alert box here.
    }
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    IMHO you should identify a root source of information to use as a reference, even if it is just the developer docs at Apple. Preferably, pick up a book. Conway/Hillegass iPhone book might be a good one for you.

    If we know what you have access to at your fingertips it is much easier to help you find it instead of handing you a solution.

    B
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    label1 and label2 are UILabel objects; they probably won't like being assigned numeric values. Also, you need to revisit how you are extracting the numeric values from your valueA, valueB and valueC UITextFields. Plus, there's semicolons missing from this code.

    If this is not the actual code, we'd prefer if you copy-and-paste the real code so we are debugging what you're actually using and not some adjusted representation.
     
  8. Mr.Faisal thread starter macrumors newbie

    Joined:
    Jun 28, 2011
    #8

    That is what I'm trying to do actually.
    Code:
    if ([valueA.text isEqualToString=@""] || [valueB.text isEqualToString=@""] || [valueC.text isEqualToString=@""]) {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Text fields can't be left empty" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        }
        
    else {
    [COLOR="Red"]        'Solve' button command is to be inserted here
    [/COLOR]}
    

    Yeah I think I'll pick up one of these books and start reading them!


    This is not the code. This is just an equation that I wanted you guys and understand, and thus, change it into a functional code.
    So sorry, I don't have a code for this, this is what I'm asking for actually.

    Thank you all for the replies :)
     
  9. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #9
    Asking for people on the forum to write code for you usually doesn't go over very well. If you have code you need help with, we will either help you with it or point you to where you can find the answers and learn at the same time. There are many books on beginning C, Objective-C and iPhone programming available.
     
  10. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #10
    You've picked a great "first app" problem to solve. You completely understand what it needs to do, now you just have to translate it to iOS. It also exhibits a great MVC structure. The Model, in this case, is the quadratic equation. The Controller manages the View and propagates information between the view and the model as appropriate.

    My first recommendation is the same as others: find some structured training and follow it. I'm personally fond of the Stanford iTunesU course - I've recommended it several times here, and have used it myself to get going. Using the Stanford class, you'll solve this problem in about 2 hours of watching video and writing code, and it will give you a very solid basis for moving forward.

    Other less-than-directly-useful comments:
    • Somehow you have to limit the input characters on the UITextFields to numeric kinds of characters - '0'-'9','+','-', and '.'. Look at the API documentation to find out how to do that (hint: look at UITextField and its implementation of UITextInputTraits)
    • Somehow you have to convert these number characters to number values. Look at the API docs again (hint: look at NSString for something that would return a number value)
    • What happens if a = 0?
    • What happens if b^2<4ac?
    • Let's say you happen to get updated values in to label1 and label2. What causes the screen to update? (hint: look at UIView or the View Lifecycle in the documentation)
     
  11. DavidB52 macrumors newbie

    Joined:
    Apr 22, 2011
    #11
    This is a good project to take on as a first step toward learning how to program an app. "RonC" made several good suggestions, in particular, checking if "a" is 0, and checking if the discriminant is <= 0. If it is zero, then the two roots are the same. If it is negative, the roots are complex.

    I think if you hope to make this an industrial-strength numerical app for broad distribution, you would also have to make it account for a large range in the input numbers (i.e. - take into account overflow or underflow numbers). The field of numerical programming has many pitfalls to keep in mind.

    A Google search for a quadratic equation solver produced over 300,000 results. Here is one of the top results:

    Quadratic Equation Solver

    Perhaps you can get some ideas from it.

    Hope this helps.
     

Share This Page