Objective-C adding values of 2 arrays - objective-c

I have NSArray1 = (1, 5, 2)
and NSArray2 = (1, 3, 5)
i want to array1 + array2 = (should return) = (2, 8, 7)
(in fact is it even possible to do this with NSArray)?
Heres a similar question
Adding two arrays together
(but this adds the values of the second array onto the end of the first array)
NSArray *a = [NSArray arrayWithObjects: #"1" ,#"2",#"3",nil];
NSArray *b = [NSArray arrayWithObjects: #"1" ,#"2",#"3",nil];
NSMutableArray *c = [[NSMutableArray alloc]init];
c = [a addObjectsFromArray:b];
// just a test code . . . .

If it's a C array, then just do
int newArray[3];
for (int i=0;i<3;i++)
newArray[i] = array1[i]+array2[i];
But if it's a NSArray with NSNumbers (You can't have primitives in NSArray), then just do
NSMutableArray *newArray = [NSMutableArray array];
for (int i=0;i<[array1 count];i++)
[newArray addObject:[NSNumber numberWithInt:[[array1 objectAtIndex:i] intValue]+[[array2 objectAtIndex:i] intValue]]];
//If you're using Mountain Lion, then you can use the following
//[newArray addObject:#([array1[i] intValue]+[array2[i] intValue])];
Edit:
If you have more than 1 array, then
int numArrays = 3;
NSArray *arrayOfNum = //An array of arrays that contains all the numbers
NSMutableArray *newArray = [NSMutableArray array]
for (int i=0;i<[array1 count];i++)
{
int total = 0;
for (int x=0;x<numArrays;x++)
total+=[arrayOfNum[x] intValue];
[newArray addObject:#(total)];
}

Related

how to get the index on numbers generated by int arc4random()

How do you get the index of the number appearing from the arc4rand method?. If 4,1,2,3 appear how do you index them . Example 4 would be 0 in the array.
int rand=((arc4random()%4)+1);
For getting random number, i did like this:
Suppose, i have a mutable array
NSMutableArray * myArray = [[NSMutableArray alloc] init];
and i kept 5 data in this myArray.
Now I have to generate random number index from myArray.
int randomIndex = (arc4random() % ([myArray count]));
Here randomIndex is random index of array
try this one. you need to put all that values into array.
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:4];
[array insertObject:#"4" atIndex:0];
[array insertObject:#"1" atIndex:1];
[array insertObject:#"2" atIndex:2];
[array insertObject:#"3" atIndex:3];
using array to get index in ios to add all values in array like this
//convert int to nsstring and add array
NSArray *array=[NSArray arrayWithObjects:NSStringFromInt(4),NSStringFromInt(1),NSStringFromInt(2),NSStringFromInt(3), nil];
//reverse process to get int value
int value=[[array objectAtIndex:index]integerValue];
I'm just guessing, you might look for this...
NSMutableArray *_array = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
[_array addObject:#((arc4random()%4)+1)];
}

how to compare 2 NSMutableArray and get different number of value

I want compare 2 NSMutableArray values and check which indexPath is different and store this numbers of index in one array.
for example I have these NSMutableArray :
NSMutableArray *a = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
NSMutableArray *b = [[NSMutableArray alloc]initWithObjects:#"11",#"2",#"5",#"3",#"53",#"6", nil];
and I want to compare these two NSMutableArray index to index (compare value to value) and tell me that which index of array in these NSMutableArrays is different. for example in two NSMutableArray on top this index is different (0,2,3,4) and I want store this values in certain Array.
NSMutableArray* differentIndex;
differentIndex = [[NSMutableArray alloc] init];
for(int i = 0 ; i < [a count]; i++) {
if(i < [b count]){
if(![b[i] isEqual:a[i]]){
[differentIndex addObject:[NSNumber numberWithInt:i]];
}
} else {
[differentIndex addObject:[NSNumber numberWithInt:i]];
}
}
Now your differentIndex contains all the different index in the arrays. :)
NSMutableArray *a = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
NSMutableArray *b = [[NSMutableArray alloc]initWithObjects:#"11",#"2",#"5",#"3",#"53",#"6", nil];
NSMutableArray *new = [[NSMutableArray alloc]init];
for(int i =0;i<[b count]<=[a count]?[b count]:[a count];i++)
{
NSString *_keyA = [a objectAtIndex:i];
NSString *_keyB = [b objectAtIndex:i];
if ([_keyA isEqualToString:_keyB])
continue;
[new addObject:[NSString stringWithFormat:#"%d",i]];
}

Populating array with integers

Let's say I want to populate NSarray with 50 integers. We know that NSarray accept only objects. So I have to do 50 times
NSNumber *num1 = [NSNumber numberWithInit:10];
NSNumber *num2 = [NSNumber numberWithInit:212];
......
NSNumber *num50 = [NSNumber numberWithInit:12];
Is there more elegant way to achieve that, beacause looks stupid 50 lines of code only for create number objects ?
try this...
NSMutableArray *array=[[NSMutableArray alloc]initWithCapacity:50 ];
for (int i=0; i<0; i++) {
NSNumber *number=[[NSNumber alloc] initWithInt:i];
[array addObject:number];
[number release];
}
//do anything with arrray and release the array later.
is this OK or you are seeking anything else.?
How about using NSMutableArray?
NSMutableArray* arr = [[NSMutableArray alloc] init];
int i = 0;
for(i=0; i<50; i++) {
NSNumber* num = [NSNumber numberWithInt:i]; // use i or random numbers
[arr addObject:num];
}
Your numbers do not seem to follow any particular pattern, so you might be better doing this by creating a C array first:
int myValues[] = { 10, 212, ..., 12 };
NSUInteger count = sizeof(myValues)/sizeof(int); // number of integers in myValues
// abstract the following into a function/method/category if doing more than once
NSMutableArray *objcValues = [NSMutableArray arrayWithCapacity:count];
for(NSUInteger ix = 0; ix < count; ix++)
[objcValues addObject:[NSNumber numberWithInt:myValues[ix]];

finding a number in array

I have an Array {-1,0,1,2,3,4...}
I am trying to find whether an element exist in these number or not, code is not working
NSInteger ind = [favArray indexOfObject:[NSNumber numberWithInt:3]];
in ind i am always getting 2147483647
I am filling my array like this
//Loading favArray from favs.plist
NSString* favPlistPath = [[NSBundle mainBundle] pathForResource:#"favs" ofType:#"plist"];
NSMutableDictionary* favPlistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:favPlistPath];
NSString *favString = [favPlistDict objectForKey:#"list"];
NSArray *favList = [favString componentsSeparatedByString:#","];
//int n = [[favList objectAtIndex:0] intValue];
favArray = [[NSMutableArray alloc] initWithCapacity:100];
if([favList count]>1)
{
for(int i=1; i<[favList count]; i++)
{
NSNumber *f = [favList objectAtIndex:i];
[favArray insertObject:f atIndex:(i-1)];
}
}
That's the value of NSNotFound, which means that favArray contains no object that isEqual: to [NSNumber numberWithInt:3]. Check your array.
After second edit:
Your favList array is filled with NSString objects. You should convert the string objects to NSNumber objects before inserting them in favArray:
NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];

Create multiple numbered variables based on a int

How would I create a number of NSDictionary variables using an array's count?
This is basically what I came up with, but I'm not sure how to make this work with Objective-C syntax. doesntContainAnother is an NSArray. I want the names of the dictionaries to use the current value of loopInt.
int *loopInt = 0;
while (doesntContainAnother.count <= loopInt) {
NSMutableDictionary *[NSString stringWithFormat:#"loopDictionary%i", loopInt] = [[[NSMutableDictionary alloc] init] autorelease];
[NSString stringWithFormat:#"loopDictionary%i", loopInt] = [NSDictionary dictionaryWithObject:[array1 objectAtIndex:loopInt]
forKey:[array2 objectAtIndex:loopInt]];
loopInt = loopInt + 1;
}
Create a mutable array and loop until you reach the original array's count, creating a dictionary and adding it to the mutable array on each iteration.
Your code should look like this.
NSMutableArray *dictionaries = [[NSMutableArray alloc] init];
for (int i = 0; i < doesntContainAnother.count; i++) {
[dictionaries addObject:[NSMutableDictionary dictionaryWithObject:[array1 objectAtIndex:i] forKey:[array2 objectAtIndex:i]]];
}
The approach of creating variables with numbers at the end of their names is an antipattern and not even possible in Objective-C. It's equivalent to an array, but clunkier.
You need to create a mutable array, and then put the objects into the array. You can't create a variable with the same name as the contents of a string, as you have done. For example:
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[doesntContainAnother count]];
int i = 0; // Note: type is int, not int*
for (i = 0; i < [doesntCountainAnother count]; i++) {
[arr addObject:[NSMutableDictionary dictionary]];
}
// Later...
NSMutableDictionary *d1 = [arr objectAtIndex:3];
Or if you want to pull them out of the list by name:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:[doesntCountainAnother count]];
int i = 0;
for (i = 0; i < [doesntContainAnother count]; i++) {
[dict setObject:[NSMutableDictionary dictionary] forKey:[NSString stringWithFormat:#"loopDictionary%d", i]];
}
// Later...
NSMutableDictionary *d1 = [dict objectForKey:#"loopDictionary3"];
But the first way is likely the easiest.