I'll get to this quick: I have an application for the iPhone OS 3.1.2 that will reduce fractions. I have 4 outlets:

  1. oTop and oBottom: two UITextFields, stands for originalTop and originalBottom.
  2. rTop and rBottom: two UILabels, stands for reducedTop and reducedBottom.

Here is the code I use:

-(IBAction)reduce {

int numerator = [[oTop text] intValue];
int denominator = [[oBottom text] intValue];
if (denominator > 0) {
    NSMutableArray *factors1 = [[NSMutableArray alloc] init];
    NSMutableArray *factors2 = [[NSMutableArray alloc] init];
    int factors1length;
    int factors2length;
    for (int i = 1; i < ceil(sqrt(numerator)); i ++) {
        [factors1 addObject:[NSString stringWithFormat:@"%@", i]];
        if (round(numerator / i) != numerator / i) {
            [factors1 removeLastObject];
        } else {
            factors1length ++;
    for (int i = factors1length; i <= 0; i --) {
        [factors1 addObject:[NSString stringWithFormat:@"%@", (numerator / [[factors1 objectAtIndex:i] intValue])]];
    } //End get numerator factors
    for (int i = 1; i < ceil(sqrt(denominator)); i ++) {
        [factors2 addObject:[NSString stringWithFormat:@"%@", i]];
        if (round(denominator / i) != denominator / i) {
            [factors2 removeLastObject];
        } else {
            factors2length ++;
    for (int i = factors2length; i <= 0; i --) {
        [factors2 addObject:[NSString stringWithFormat:@"%@", (denominator / [[factors2 objectAtIndex:i] intValue])]];
    } //End get denominator factors 


Sorry about the stray lines. Anyway, could someone tell me what is going on? When I launch, type a number in both text fields, greater than 0, and press the "reduce" button, the app crashes. Please help,


EDIT: Changed the 1st and 3rd for loops from (int i = 0 to (int i = 1.

+3  A: 

Looks like in the first for-i loop, i starts at 0 and it divides by i (which is zero).

EDIT: There was one problem before the divide-by-zero which caused the initial crash and several others afterwards as well.

  1. In the first loop, the addObject is done using @"%@" but this needs to be @"%d" because i is an integer not a string. See String Format Specifiers.

  2. factors1length and factors2length need to be initialized to zero otherwise they will start with random values which will throw off the rest of the code.

  3. The addObject in the second loop needs to use @"%f" instead of @"%@" because you are trying to use a floating point value there (numerator / xxx...). However, there are problems with the approach of storing numbers in your array as strings. First, you're storing some as ints and some as floats. Stick to one type. Second, it's better to store numbers as numbers rather than converting to string and back. You can store ints/floats in a NSMutableArray by converting them to an NSNumber object.

If you haven't already done so, please take cdespinosa's advice and step through the code one line at a time in the debugger and see what values the variables have and exactly what code is being executed.

I'd also recommend not trying to put too much logic into one line. For example, the addObject line in the second loop is doing too much making it hard to see the problem. Break it up by declaring local variables to store the values of expressions within the line.
So instead of this:

[factors1 addObject:[NSString stringWithFormat:@"%@", (numerator / [[factors1 objectAtIndex:i] intValue])]];

write it like this:

int iFactor = [[factors1 objectAtIndex:i] intValue];
float value = (numerator / iFactor);
NSString *newFactor = [NSString stringWithFormat:@"%@", value];
[factors1 addObject:newFactor];

This will make it easier to debug.

Thanks for the help but it still crashes. Any more problems? (besides the third for-i loop)
HiGuy Smith
Have you tried setting a breakpoint at the beginning of the method, stepping through the code, looking at the values of local variables as it executes, and noting the line on which it crashes?
Not sure if you've already solved this after following cdespinosa's good advice but I'll add more problems found in the code to my answer shortly.
Ok, I found the problem. It's in the second and fourth for-i loops. I will investigate more on those. Thanks!
HiGuy Smith

What's the error message you're seeing when it crashes?

+1  A: 

You might want to look into Euclid's algorithm for finding the greatest common divisor of two integers. It's much faster (and even easier to code) than factoring. There are sample implementations in the Wikipedia article.

Jim Lewis