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:
- oTop and oBottom: two UITextFields, stands for originalTop and originalBottom.
- 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.