Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:37:50+00:00 2026-05-13T13:37:50+00:00

I’ll get to this quick: I have an application for the iPhone OS 3.1.2

  • 0

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,

HiGuy

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:37:51+00:00Added an answer on May 13, 2026 at 1:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.