Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SCHOEN

macrumors newbie
Original poster
Jul 23, 2011
8
0
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?
 
Last edited by a moderator:
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?
 
Last edited by a moderator:
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.
 
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.

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?
 
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
 
1. What does .text return? month1.text (and month2,month3) each returns a value.

2. Yes, arithmetic can be performed.
 
A value? Obviously. But that's not what i asked. What type is the value? int? float? NSString? some random NSObject subclass?
 
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
 
Certainly.

Attempting to establish variation values of 'val'. Obviously there are concerns I have.
Background in SAP.
 
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
 
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.
 
Last edited by a moderator:
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
 
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.
 
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..
 
Last edited by a moderator:
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
- MAX(x, MAX(y, z));
part. Maybe it would be more effecient. But what do I know, I'm only a beginner. :)
 
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. :)

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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.