Error with index 1 beyond bounds - objective-c

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.

Related

Objective C - What causes copying of data to float array to result in continuous memory increase?

When I call this method
- (int)runDetection:(NSArray *)data length:(int)length
{
long count = [data count];
float *dataArray = (float *)malloc(sizeof(float *) * count);
for (int i = 0; i < count; ++i)
{
dataArray[i] = [[data objectAtIndex:i] floatValue];
}
free(dataArray);
return -2;
}
But after commenting this piece of code, the app works fine. No continuous memory increase.
for(int i = 0; i < count; ++i)
{
dataArray[i] = [[data objectAtIndex:i] floatValue];
}
The memory is freed after use so why is this causing memory increase?

Algorithm to find all possible solutions from an array of array

What is the best algorithm to find all possible words from an array of array of character.
Here an example :
From this array : [[A],[B,C,D],[E,F],[G,H]]
I need in return an array of the 12 ordered possibilities [[A,B,E,G],[A,C,E,G], ... , [A,D,F,H]]
Do you know how to implement this algorithm ? If you know it and you provide an example in any language (C,JAVA,Javascript, ...), feel free to share because it's been a day I try to find it ...
Here how I tries to implement it ("array" is an array of array of char):
+ (NSArray*) possibleReading:(NSMutableArray*)array {
int nbPossibilities = 1;
for(int i = 0; i < [array count]; i++) {
nbPossibilities *=[[array objectAtIndex:i] count];
}
NSMutableArray *possArr = [[NSMutableArray alloc] initWithCapacity:nbPossibilities];
for (int i=0; i < nbPossibilities; i++) {
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
[possArr addObject:innerArray];
}
for (int i=0; i< [array count]; i++) {
//
for(int nbPoss = 0; nbPoss < nbPossibilities; nbPoss++) {
NSMutableArray * arr = [possArr objectAtIndex:nbPoss];
NSNumber * num = [NSNumber numberWithInt:nbPoss % [[array objectAtIndex:i] count]];
NSString * literal = [[array objectAtIndex:i] objectAtIndex:[num intValue]];
[arr insertObject:literal atIndex:i];
}
}
return possArr;
}
It would be easiest to do this using a recursive method.
Java code
import java.util.Arrays;
public class CartesianProductCalculator {
private char[][] result;
private char[][] sets;
private char[] currentSet;
private int index;
public char[][] calculateProduct(char[][] sets) {
index = 0;
// calculate size of result
int resultSize = 1;
this.sets = sets;
for (char[] set : sets) {
resultSize *= set.length;
}
result = new char[resultSize][];
currentSet = new char[sets.length];
calculateProduct(sets.length-1);
return result;
}
// fills result from right to left
public void calculateProduct(int setIndex) {
if (setIndex >= 0) {
for (char c : sets[setIndex]) {
currentSet[setIndex] = c;
calculateProduct(setIndex-1);
}
} else {
result[index++] = Arrays.copyOf(currentSet, currentSet.length);
}
}
public static void main(String[] args) {
char[][] input = {{'A'},{'B','C','D'},{'E','F'},{'G','H'}};
CartesianProductCalculator productCalculator = new CartesianProductCalculator();
System.out.println(Arrays.deepToString(productCalculator.calculateProduct(input)));
}
}
Objectiv-C
+ (NSArray *) cartesianProductOfArrays(NSArray *arrays) {
int arraysCount = arrays.count;
unsigned long resultSize = 1;
for (NSArray *array in arrays)
resultSize *= array.count;
NSMutableArray *product = [NSMutableArray arrayWithCapacity:resultSize];
for (unsigned long i = 0; i < resultSize; ++i) {
NSMutableArray *cross = [NSMutableArray arrayWithCapacity:arraysCount];
[product addObject:cross];
unsigned long n = i;
for (NSArray *array in arrays) {
[cross addObject:[array objectAtIndex:n % array.count]];
n /= array.count;
}
}
return product;
}
C
#include <stdio.h>
#include <string.h>
void print(int size, char *array[size], int indexs[size]){
char result[size+1];
int i;
for(i = 0; i < size; ++i)
result[i] = array[i][indexs[i]];
result[size] = 0;
puts(result);
}
int countUp(int size, int indexs[size], int lens[size]){
int i = size -1;
while(i >= 0){
indexs[i] += 1;// count up
if(indexs[i] == lens[i])
indexs[i--] = 0;
else
break;
}
return i >= 0;
}
void find_all(int size, char *array[size]){
int lens[size];
int indexs[size];
int i;
for(i = 0; i < size; ++i){//initialize
lens[i] = strlen(array[i]);
indexs[i] = 0;
}
do{
print(size, array, indexs);
}while(countUp(size, indexs, lens));
}
int main(void){
char *array[] = { "A", "BCD", "EF", "GH" };
int size = sizeof(array)/sizeof(*array);
find_all(size, array);
return 0;
}
If you can remove duplicate entries in inner array objects before executing method then you won't get duplicate words in result array.
- (NSArray*) possibleReading:(NSMutableArray*)array {
int nbPossibilities = 1;
for(int i = 0; i < [array count]; i++)
{
NSArray *cleanedArray = [[NSSet setWithArray:[array objectAtIndex:i]] allObjects];
[array replaceObjectAtIndex:i withObject:cleanedArray];
nbPossibilities *=[[array objectAtIndex:i] count];
}
NSMutableArray *possArr = [[NSMutableArray alloc] initWithCapacity:nbPossibilities];
for (int i=0; i < nbPossibilities; i++) {
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
[possArr addObject:innerArray];
}
for (int i=0; i< [array count]; i++) {
//
for(int nbPoss = 0; nbPoss < nbPossibilities; nbPoss++) {
NSMutableArray * arr = [possArr objectAtIndex:nbPoss];
NSNumber * num = [NSNumber numberWithInt:nbPoss % [[array objectAtIndex:i] count]];
NSString * literal = [[array objectAtIndex:i] objectAtIndex:[num intValue]];
[arr insertObject:literal atIndex:i];
}
}
return possArr;
}

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];
}

Displaying factors of a number

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]];
}

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?