Displaying factors of a number - objective-c

I want to display every factor of a number typed in a textfield to achieve this I tried using an array. But I am always getting the error 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' . Any suggestions to eliminate this error?
NSMutableArray *array;
array = [NSMutableArray arrayWithCapacity:100];
for (factorsNumber=1; factorsNumber<=number; factorsNumber++) {
if (number%factorsNumber == 0) {
[array addObject:[NSString stringWithFormat:#"%d", factorsNumber]];
}
}
for (int i = 0; i <= [array count]; i++) {
factors.text = [NSString stringWithFormat:#"%d", i, [[array objectAtIndex:i] intValue]];
}

for (int i = 0; i <= [array count]; i++) {
should be
for (int i = 0; i <= [array count] - 1; i++) {
or
for (int i = 0; i < [array count]; i++) {
The valid indices of an n-element array are 0 to n-1.

In your for loop, remove the = so that it reads:
for (int i = 0; i < [array count]; i++)

you problem is here:
for (int i = 0; i < [array count]; i++) { // < instead of <=
factors.text = [NSString stringWithFormat:#"%d", i, [[array objectAtIndex:i] intValue]];
}

Related

Error with using local variable to access array index

For some reason this occurs every time i try to access a specific index from the array by specifying a variable int in the index in a forloop. When i do i get a Thread 1 Error, but if i use a variable that has not be declared in the forloop itself than it seem to work fine.
Code:
for(int i =0 ; i<= [array count]; i++) {
NSNumber *convert = [array objectAtIndex:i]; //results in error
NSLog(#"%i", [convert intValue]);
}
Problem is you are trying to access array beyond its capacity. Array starts with 0 index and goes upto array.count - 1. That said, please try with below code and you should be good:
for (int i = 0 ; i <= array.count - 1 ; i++) {
NSNumber *convert = [array objectAtIndex:i];
NSLog(#"%i", [convert intValue]);
}
Another variation could be:
for (int i = 0 ; i < array.count ; i++) {
NSNumber *convert = [array objectAtIndex:i];
NSLog(#"%i", [convert intValue]);
}

Error with index 1 beyond bounds

As following code I wanna send matrix1Col and matrix2Col to multiply method.
But it's error with index 1 beyond bounds.
From inside UIButton code
double kk[2][2] = {{1,2},{5,6}};
double t [2][1] = {1,2};`
if (!matrix1Col) {
matrix1Col = [NSMutableArray array];
}
for (unsigned int i=0; i<2; i++) {
matrix1Row = [NSMutableArray new];
for (unsigned int j=0 ; j<2; j++) {
[matrix1Row addObject:#(kk[i][j])];
}
[matrix1Col addObject:matrix1Row];
}
if (!matrix2Col) {
matrix2Col = [NSMutableArray array];
}
for (unsigned int i=0; i<2; i++) {
matrix2Row = [NSMutableArray new];
for (unsigned int j=0; j<1; j++) {
[matrix2Row addObject:#(t[i][j])];
}
[matrix2Col addObject:matrix2Row];
}
NSMutableArray *resultMultiply = [self multiply:matrix1Col :matrix2Col];
Another method in ViewController.m:
-(NSMutableArray*)multiply:(NSMutableArray*)matrix1 :(NSMutableArray*)matrix2{
int matrix1RowCount = [matrix1 count];
int matrix2RowCount = [matrix2 count];
int matrix2ColCount = [[matrix2 objectAtIndex:0] count];
NSMutableArray *multiplyMatrix = [NSMutableArray arrayWithCapacity:matrix1RowCount];
for (int i=0; i< matrix1RowCount; i++) {
NSMutableArray *matrixInRow = [NSMutableArray arrayWithCapacity:matrix2ColCount];
for (int j=0; j<matrix2RowCount; j++) {
double valueTotal = 0;
for (int k=0; k<matrix2ColCount; k++) {
double value1 = [[[matrix1 objectAtIndex:i] objectAtIndex:k] doubleValue];
double value2 = [[[matrix2 objectAtIndex:k] objectAtIndex:j] doubleValue];
valueTotal += value1*value2;
}
[matrixInRow addObject:[NSNumber numberWithDouble:valueTotal]];
}
[multiplyMatrix addObject:matrixInRow];
}
return multiplyMatrix;
}
This is error code:
2013-02-14 06:26:06.595 matrixMutableArray[21578:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
I can't find it where it's error.
Thanks you for your comment.
The error occurs on this row:
double value2 = [[[matrix2 objectAtIndex:k] objectAtIndex:j] doubleValue];
When the error occurs k=0 and j=1. The objectAtIndex:0 for matrix2 only has one object, hence the error.

Show object in NSMutableArray of matrix

Following this code:
In ViewController.m
double kk[2][2] = {{1,2},{5,6}};
if (!matrix1Col) {
matrix1Col = [NSMutableArray array];
}
for (int i=0; i<2; i++) {
[matrix1Row removeAllObjects];
if (!matrix1Row) {
matrix1Row = [NSMutableArray array];
}
for (int j=0 ; j<2; j++) {
[matrix1Row insertObject:[NSNumber numberWithDouble:kk[i][j]] atIndex:j];
}
[matrix1Col insertObject:matrix1Row atIndex:i];
}
self.label100.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:0] objectAtIndex:0] doubleValue]];
self.label110.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:1] objectAtIndex:0] doubleValue]];
self.label101.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:0] objectAtIndex:1] doubleValue]];
self.label111.text = [NSString stringWithFormat:#"%f",[[[matrix1Col objectAtIndex:1] objectAtIndex:1] doubleValue]];`
I wanna show object in NSMutableArray of matrix which receive value from matrix of double in label.
And my all label must show as following ->label100 show 1 ->label110 show 5 ->label101 show 2 and ->label111 show 6
but It shows ->label100 show 5 ->label110 show 5 ->label101 show 6 and ->label111 show 6
How can I do?
The problem is that you're always overwriting already inserted objects, as you insert the row many times in the same loop. That's how I would write it:
double kk[2][2] = {{1,2},{5,6}};
NSMutableArray* matrix= [NSMutableArray new];
for(unsigned int i=0; i<2; i++)
{
NSMutableArray* row= [NSMutableArray new];
for(unsigned int j=0; j<2; j++)
{
[row addObject: #(kk[i][j]) ];
}
[matrix addObject: row];
}

insertion sort algorithm in Objective-C to implement in iphone

I am trying to sort 15 random numbers using Objective-C in the code shown below. The code is not working as planned. I took the concept from the the insertion sort C code. The 15 random numbers are generating properly but the sort is not working.
C code:
int i, j, index;
for (i = 1; i < array_size; ++i)
{
index = a[i];
for (j = i; j > 0 && a[j-1] > index; j--)
a[j] = a[j-1];
a[j] = index;
}
Objective-C code:
-(IBAction)clicked_insertsort:(id)sender
{
NSMutableArray *array = [NSMutableArray array];
for (int x = 0; x < 15; x++)
{
[array addObject: [NSNumber numberWithInt: arc4random()%200]];
}
NSLog(#"%#",array);
{
int i, j;
id index;
for (i = 1; i < 15; ++i)
{
index = [array objectAtIndex:(NSUInteger)i]; // a[i];
for (j = i; j > 0 && [array objectAtIndex:(NSUInteger)j-1] > index; j--)
[array replaceObjectAtIndex: (j) withObject: [array objectAtIndex: (j-1)]];
[array objectAtIndex:(NSUInteger)j] == index ;
}
}
NSLog(#"%#",array);
}
You are comparing pointers, which is just sorting your array by the memory addresses of your objects, not their actual value.
index = [array objectAtIndex:(NSUInteger)i]; // a[i];
[array objectAtIndex:(NSUInteger)j-1] > index
You need to get the primitive integer value of the NSNumber:
[NSNumber numberWithInt:20] != 20; // This is wrong.
[[NSNumber numberWithInt:20] intValue] == 20; // This is correct.
Here's your code, with revisions:
-(IBAction)clicked_insertsort:(id)sender
{
NSMutableArray *array = [NSMutableArray array];
for (int x = 0; x < 15; x++)
{
[array addObject: [NSNumber numberWithInt: arc4random()%200]];
}
NSLog(#"%#",array);
{
int i, j;
id index;
for (i = 1; i < 15; ++i)
{
index = [[array objectAtIndex:(NSUInteger)i] intValue]; // a[i];
for (j = i; j > 0 && [[array objectAtIndex:(NSUInteger)j-1] intValue] > index; j--)
[array replaceObjectAtIndex: (j) withObject: [array objectAtIndex: (j-1)]];
[[array objectAtIndex:(NSUInteger)j] intValue] == index ;
}
}
NSLog(#"%#",array);
}
Actually the problem is that the algorithm itself does not make much sense.
This line:
[array objectAtIndex:(NSUInteger)j] == index ;
Should be:
[array replaceObjectAtIndex:j withObject:index]; //yes again
Try this way, with modern syntax:
-(IBAction)clicked_insertsort:(id)sender
{
NSMutableArray *array = [NSMutableArray array];
for (int x = 0; x < 15; x++)
{
[array addObject: #(arc4random()%200)];
}
NSLog(#"%#",array);
NSUInteger i, j;
for (i = 1; i < 15; ++i)
{
NSNumber *current = array[i];
for (j = i; j > 0 && [array[j-1] unsignedIntegerValue] > [current unsignedIntegerValue]; j--)
array[j] = array[j-1];
array[j] = current;
}
NSLog(#"%#",array);
}
Run the code and see the result.

Array-within-array items replacement

I have a list of arrays, each of elements of these is array to. I know, it's a little freaky, but it's necessary. So, I need to replace elements of deepest array. I've tried this:
for (int i = 0; i < [myArray count]; i++) {
for (int j = 0; j < [[myArray objectAtIndex:i] count]; j++) {
for (int k = 0; k < [[[myArray objectAtIndex:i] objectAtIndex:j] count]; k++) {
[[[myArray objectAtIndex:i] objectAtIndex:j] replaceObjectAtIndex:k withObject:#"Some_string"];
}
}
}
and got an error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance. But I can to log this element, e.g. NSLog(#"%#", [NSString stringWithFormat:#"%#",[[[myArray objectAtIndex:0] objectAtIndex:0]objectAtIndex:0]]);, it's ok.
What it can be? Thanks for your help.
Are your arrays instances of NSMutableArray instead of immutable NSArray?