Note that there are some explanatory texts on larger screens.

plurals
  1. POarray index out of bounds problems
    primarykey
    data
    text
    <p>So I'm making a biginteger program, and I'm having a problem with adding two arrays that aren't the same length. The problem I'm having is with the add method. If I'm iterating through an array is there any way to test if element is out of bounds. I've tried testing if the element in a is equal to nil, but I still get the exception. Any help would be great thanks. </p> <pre><code> #import &lt;Foundation/Foundation.h&gt; #import "MPInteger.h" @implementation MPInteger { } -(id) initWithString: (NSString *) x { self = [super init]; if (self) { intString = [NSMutableArray array]; for (int i = 0; i &lt; [x length]; i++) { NSString *ch = [x substringWithRange:NSMakeRange(i, 1)]; [intString addObject:ch]; } } return self; } -(NSString *) description { return self.description; } - (MPInteger *) add: (MPInteger *) x { NSMutableArray *a = self-&gt;intString; NSMutableArray *b = x-&gt;intString; NSMutableArray *c = [NSMutableArray array]; NSInteger arrayCount; if (a &lt; b) { arrayCount = [b count]; } else { arrayCount = [a count]; } int num = 10; int carry = 1; NSNumber *total; NSNumber *carrySum; for (int i = 0; i &lt; arrayCount; i++) { if (a[i] == nil) { total = @([b[i] intValue]); [c addObject:total]; } else if (b[i] == nil) { total = @([a[i] intValue]); [c addObject:total]; } else { total = @([a[i] intValue] + [b[i] intValue]); [c addObject:total]; } } for (NSInteger j = [c count]-1; j &gt;=0; j--) { if ([c[j] intValue] &gt;= num) { total = @([c[j] intValue] - num); carrySum = @([c[j-1] intValue] + carry); [c replaceObjectAtIndex:j withObject:total]; [c replaceObjectAtIndex:j-1 withObject: carrySum]; } } NSString *str = [c componentsJoinedByString:@""]; NSLog(@"%@", str); return x; } -(MPInteger *) multiply: (MPInteger *) x { NSMutableArray *a = self-&gt;intString; NSMutableArray *b = x-&gt;intString; NSMutableArray *c = [NSMutableArray array]; NSMutableArray *sum = [NSMutableArray array]; NSNumber *total; NSNumber *carrySum; int num = 10; NSNumber *endZero = 0; NSInteger bottomCount = [b count]-1; while (bottomCount != -1) { for (int i = 0; i &lt; [a count]; i++) { total = @([a[i] intValue] * [[b objectAtIndex:bottomCount] intValue]); if (bottomCount == [b count] -1) { [c addObject:total]; } else { [c replaceObjectAtIndex:i withObject:total]; } } for (NSInteger j = [c count]-1; j&gt;=0; j--) { NSString *carry = [NSString stringWithFormat:@"%d", [c[j] intValue]]; NSString *carry2 = [carry substringToIndex:1]; int carryFinal = [carry2 intValue]; NSString *carry3 = [carry2 stringByAppendingString:@"0"]; int carry4 = [carry3 intValue]; if ([c[j] intValue] &gt;= num) { total = @([c[j] intValue] - carry4); carrySum = @([c[j-1] intValue] + carryFinal); [c replaceObjectAtIndex:j withObject:total]; [c replaceObjectAtIndex:j-1 withObject: carrySum]; } else { if(j == 0) { if (bottomCount == [b count] -1) { bottomCount = bottomCount - 1; NSString *str = [c componentsJoinedByString:@""]; [sum addObject: str]; } else { [c addObject:@([endZero intValue])]; bottomCount = bottomCount - 1; NSString *str = [c componentsJoinedByString:@""]; [sum addObject: str]; } } } } } NSMutableArray *finalSum = [NSMutableArray array]; MPInteger *ele1; MPInteger *ele2; MPInteger *eleSum; NSNumber *endZ= @(0); [finalSum insertObject:endZ atIndex:0]; for (int k = 0; k &lt; [sum count]; k++) { NSString *str= [NSString stringWithFormat:@"%d", [sum[k] intValue]]; NSString *str2 = [NSString stringWithFormat:@"%d", [sum[k+1] intValue]]; ele1 = [[MPInteger alloc] initWithString:str]; ele2 = [[MPInteger alloc] initWithString:str2]; eleSum = [ele1 add: ele2]; NSLog(@"%@", eleSum); } NSLog(@"%@", sum); return self; } </code></pre> <p>Updated this</p> <pre><code> for (int i = 0; i &lt; arrayCount; i++) { if (a[i] == nil) { total = @([b[i] intValue]); [c addObject:total]; } else if (b[i] == nil) { total = @([a[i] intValue]); [c addObject:total]; } else { total = @([a[i] intValue] + [b[i] intValue]); [c addObject:total]; } } </code></pre> <p>has now become:</p> <pre><code>NSMutableArray *c = a.count &gt; b.count ? [a mutableCopy] : [b mutableCopy]; NSArray *shortestArray = a.count &gt; b.count ? b : a; [shortestArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSNumber *currentNumber, NSUInteger idx, BOOL *stop) { c[idx] = @(currentNumber.integerValue + [c[idx] integerValue]); NSLog(@"%@", c[idx]); }]; </code></pre> <p>What I think I need to do is every index that is in array a and not b or vise versa, is add beginning zeros, but I don't know how to do that.</p> <p>I printed out what it does after each iteration and it gives:</p> <pre><code>2013-09-02 12:31:42.630 Asgn1[42471:303] 5 2013-09-02 12:31:42.632 Asgn1[42471:303] 3 2013-09-02 12:31:42.632 Asgn1[42471:303] 1 And a final answer of: 2013-09-02 12:31:42.633 Asgn1[42471:303] 353 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload