Sum lowest 2 integers of 3 in Obj-C

Discussion in 'iPhone/iPad Programming' started by SCHOEN, Jul 25, 2011.

  1. SCHOEN, Jul 25, 2011
    Last edited by a moderator: Jul 25, 2011

    macrumors newbie

    Joined:
    Jul 23, 2011
    #1
    The objective is to sum the lowest 2 integers of 3 selected (-max?).

    The code below is testing the sum of 3 integers portion and then I will attempt to -(max) to print the result required in my "result.text" field.

    If there is a better way for me to be writing this your assistance is very much welcomed and appreciated.

    Code:
    -(IBAction)SubmitB;     
    
    {
    int val = [month1.text intValue];
        val = val+[month2.text intValue];
        val = val+[month3.text intValue];
        
        NSString *String = [[NSString alloc] initWithFormat:@"%i", val];
        result.text = String;
        
        [String release];


    What I have to in my attempt to achieve sum of lowest 2 integers is as follows:

    Code:
    result.text = [month1.text + month2.text + month3.text] - max(month1.text,max(month2.text,month3.text));
    
    I am not having success. Any assistance and expertise?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What type does .text return? Can you use the + operator on that type?
     
  3. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    Also, related to robbieduncan's question, isn't val already the sum of three numbers you seek?

    B
     
  4. SCHOEN, Jul 25, 2011
    Last edited by a moderator: Jul 25, 2011

    thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #4
    The integers are numeric.
    Does that answer what you're asking? Float? I have it now to sum 3 integers and now have to figure out how to subtract the greater of the 3.

    Any suggestions?

    @balamw - are you referencing the NSString? Suggesting it be removed?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    What makes you think .text returns an integer? Not, say, a NSString? In fact class are month1, month2, month3 instances of?
     
  6. thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #6
    Would that have something to do with my warning message:
    "Local declaration of 'String' hides instance variable"

    I can't resolve this out and it's destroying my brain.
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    That has nothing to do with the questions I asked. Please answer the questions:

    1) What type does month1.text return?
    2) Can you perform arithmetic on this type?
    3) If not then why would
    Code:
    [month1.text + month2.text + month3.text] - max(month1.text,max(month2.text,month3.text));
    
    work?
     
  8. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    Take a step back.

    What are you trying to do with this code:

    Code:
    int val = [month1.text intValue];
        val = val+[month2.text intValue];
        val = val+[month3.text intValue];
    Does that in fact do what you expect it to do? If so, why?

    I dont' recall having participated in any of your threads. Can you tell me what your background is and what source material you are using to learn Objective-C?

    B
     
  9. thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #9
    1. What does .text return? month1.text (and month2,month3) each returns a value.

    2. Yes, arithmetic can be performed.
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    A value? Obviously. But that's not what i asked. What type is the value? int? float? NSString? some random NSObject subclass?
     
  11. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    What class are month1, month2 and month3? (I think this is what robbieduncan was asking earlier). (EDIT: you're too fast robbieduncan. ;))

    EDIT: The fact that your posted code uses "[[month1.text] intValue]" seems to be a clear indication that it's probably not an int.

    B
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    I asked what class they are instances of. Not quite the same thing, but most likely month1, month2 and month 3 are not classes :p
     
  13. thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #13
    Certainly.

    Attempting to establish variation values of 'val'. Obviously there are concerns I have.
    Background in SAP.
     
  14. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    Point taken. However the exact quote is
    which I presume has a bit of a typo in it :p

    B
     
  15. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #15
    I've had enough. You're not answering the simple, direct questions. So I can't help you.
     
  16. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #16
    I'll play a bit longer.

    What do you mean by "Attempting to establish variation values of 'val'." looks like a straight sum of three numbers to me.

    rewrite those three lines as:

    Code:
    int val = [month1.text intValue] + [month2.text intValue] + [month3.text intValue];
    How is that different from:

    Code:
    [month1.text + month2.text + month3.text]
    B
     
  17. SCHOEN, Jul 25, 2011
    Last edited by a moderator: Jul 25, 2011

    thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #17
    I thank you for asking me to take the step back. Helps to see the picture clearer. I've been taking so many steps back I'm out of focus.

    The revised code is used to call the value.

    Not to mention clearer.

    Now I'm going to work on the -max. I appreciate your patience and sense-of-humor.

    B.

    EDIT: Funny....at first glance should be the same - but your suggestion does not work.
     
  18. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #18
    I don't have any SAP experience so I can't connect it to what you know, but I find especially with OOP that you get the best results by breaking the problem down as far as you can.

    robbieduncan is right for calling me out on not being specific enough.

    Knowing what classes objects are instances of is crucial to knowing what you can and can't do with them, so be sure that you know that information yourself and also post the code that creates the objects in question where possible.

    EDIT: What doesn't work? Please be specific. did you get an error message on compilation, on run?

    B
     
  19. thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #19
    Thank you B.

    I'll be sure to follow your advice and suggestions.

    My error was on 'Run'. Stopped at breaking point but appears to be due to the NSString statement.

    B.
     
  20. SCHOEN, Jul 25, 2011
    Last edited by a moderator: Jul 26, 2011

    thread starter macrumors newbie

    Joined:
    Jul 23, 2011
    #20
    Figured it out.
    Code:
    -(IBAction)calculate;     
    
    {
        int x;
        int y;
        int z;
        int val;
        
        x = [month1.text intValue];
        y = [month2.text intValue];
        z = [month3.text intValue];
        
        val = (x+y+z) - MAX(x, MAX(y, z));
        NSString *String = [[NSString alloc] initWithFormat:@"%i", val];
        result.text = String;
    }  
        
        
       
    
    -(IBAction) clear {
        month1.text = @"";
        month2.text = @"";
        month3.text = @"";
        result.text = @"";
        
    }
    
    Thanks..
     
  21. macrumors regular

    Metal Dice

    Joined:
    Jun 3, 2009
    Location:
    Denmark
    #21
    I guess you could also do something like this.

    Code:
    int a;
    int b;
    int c;
    int val;
    
    a = someValue;
    b = someValue;
    c = someValue;
    
    if(a > b && a > c)
    {
    val = b + c;
    }
    
    if(b > a && b > c)
    {
    val = a + c;
    }
    
    if(c > a && c > b)
    {
    val = a + b;
    }
    
    I think it may seem a bit more cryptic, but this way you could skip the whole
    part. Maybe it would be more effecient. But what do I know, I'm only a beginner. :)
     
  22. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #22
    This would be my preferred method as it's shorter and, to me at least, simpler. Whether it's more efficient or not will depend on how MAX is implemented. If it's a function call that cannot be inlined then it would be more expensive. If it's a function call that can be inlined (which any reasonable version of max would be) then it'll basically just be two if statements.
     
  23. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #23
    It does seem silly to add the largest value in only to take it back out.

    A simple sort and adding only the lowest two values would be another clear way of structuring this.

    B
     

Share This Page