PDA

View Full Version : Sum lowest 2 integers of 3 in Obj-C

SCHOEN
Jul 25, 2011, 04:49 PM
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.

-(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:

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?

robbieduncan
Jul 25, 2011, 04:52 PM
What type does .text return? Can you use the + operator on that type?

balamw
Jul 25, 2011, 04:55 PM
Also, related to robbieduncan's question, isn't val already the sum of three numbers you seek?

B

SCHOEN
Jul 25, 2011, 04:59 PM
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?

robbieduncan
Jul 25, 2011, 05:00 PM
The integers are numeric.

What makes you think .text returns an integer? Not, say, a NSString? In fact class are month1, month2, month3 instances of?

SCHOEN
Jul 25, 2011, 05:01 PM
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.

robbieduncan
Jul 25, 2011, 05:04 PM
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.

1) What type does month1.text return?
2) Can you perform arithmetic on this type?
3) If not then why would

[month1.text + month2.text + month3.text] - max(month1.text,max(month2.text,month3.text));

work?

balamw
Jul 25, 2011, 05:07 PM
Take a step back.

What are you trying to do with this 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

SCHOEN
Jul 25, 2011, 05:12 PM
1. What does .text return? month1.text (and month2,month3) each returns a value.

2. Yes, arithmetic can be performed.

robbieduncan
Jul 25, 2011, 05:13 PM
A value? Obviously. But that's not what i asked. What type is the value? int? float? NSString? some random NSObject subclass?

balamw
Jul 25, 2011, 05:14 PM
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

robbieduncan
Jul 25, 2011, 05:15 PM
What class are month1, month2 and month3? (I think this is what robbieduncan was asking earlier).

B

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

SCHOEN
Jul 25, 2011, 05:17 PM
Certainly.

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

balamw
Jul 25, 2011, 05:18 PM
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

Point taken. However the exact quote is In fact class are month1, month2, month3 instances of? which I presume has a bit of a typo in it :p

B

robbieduncan
Jul 25, 2011, 05:18 PM

balamw
Jul 25, 2011, 05:27 PM
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:

int val = [month1.text intValue] + [month2.text intValue] + [month3.text intValue];

How is that different from:

[month1.text + month2.text + month3.text]

B

SCHOEN
Jul 25, 2011, 05:31 PM
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.

balamw
Jul 25, 2011, 05:38 PM
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

SCHOEN
Jul 25, 2011, 06:03 PM
Thank you B.

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

B.

SCHOEN
Jul 25, 2011, 10:24 PM
Figured it out.

-(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..

Metal Dice
Jul 28, 2011, 04:07 PM
I guess you could also do something like this.

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. :)

robbieduncan
Jul 29, 2011, 06:16 AM
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.

balamw
Jul 29, 2011, 09:22 AM
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