Change value of multidimensional array in objective c? - objective-c

I am having the strangest problem and looking for help. I have an array of arrays of integers. I am trying to change these values and I cannot find out how. Here is what I tried:
[[multidim-array objectAtIndex:0] replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:1]];
what this does is get the first array from the main array and change the integer at the first index to one. Or at least, that's what I think it should be doing. and its what the other threads here said it should do. But I am getting strange behavior.
The above code, for some reason, changes the first value of EVERY array to 1. It's as if I run the code 10 times on every array in the multidim-array array.
Anyone know why this could be happening? If my phrasing was unclear, what I am trying to do is the objective-c equivelant of multidim-array[0][0] = 1;
EDIT: Someone told me this should work so here is info about how I am handling the array. The array starts out filled with 0s. There is a lot of code affecting the array but it shouldn't matter because I have breakpoints trailing through so I can see what each line of code does to the array. Before I run that line, the numbers are all 0s. I try and change one value, and they a whole bunch of them become changed. I am still looking into it if no one knows what's happening :/

I'm guessing that you've assigned the same inner array to every element of the outer array.
BTW, you can actually write this:
multidim_array[0][0] = #1;

Related

What is the fast way to find all anagrams inside an array in objective c?

I have an array that contains multiple words in there. The problem is asking me to find all the anagrams in there and pick out which one has the most anagrams. This is what I have so far:
Create a duplicate array of the old array.
Create an index array for the old array, to keep track of where every word is located. This index value won't be changed throughout the program
For the new array, I will be sort each individual word in the array.
For example: Before sorting, array has: [cat dog tac god act] then after that, it will have [act dgo act dgo act]
Sort the word in the array, then the array now will have: [act act act dgo dgo]. Now we can see that all anagrams has come together, but the order has changed in the the new array.
Use in the index array to keep track of the order of the words in the old array.
This approach seems to be good, but it depends on the number of the words that I have in the original array. If my original array is big enough, it could slow down the program due to copying process. Moreover, there is no way that I could keep track how many anagrams for each word. What could be the best approach to this kind of problem, consider that the array is big and the program has to find all the anagrams in a fast way ? Any help would be greatly appreciated. Thank you.
I think I would do something like this...
Create an NSCountedSet called anagramCounts. The objects in here will be a sorted array of letters.
Iterate your word array.
Turn each word into an array of letters.
Sort this array alphabetically.
Add the sorted array to the anagramCounts set.
After doing this the anagramCounts set will have all of the possible anagrams only once each and each will be stored next to a count of how many there are.
You can then get the object enumerator from this set and find the one with the highest count...
Or even... Add this to the previous list.
On the beginning
Create an NSInteger called highestAnagramCount and set it to 0.
Create an NSArray called mostCommonAnagram and leave it as nil.
...
On the end
Get [anagramCounts countForObject:sortedArray]; and if it is higher than highestAnagramCount then save this new value out and save the array.
At the end highestAnagramCount will tell you how many anagrams there are and mostCommonAnagram will contain the sorted array of letters.

Filling NSMutableArray for later use in obj-c

How do you fill a NSMutableArray with a set capacity for later use?
Basically I want to set up a NSMutableArray to act as a map for my game objects, so I have this line...
gameObjects = [[NSMutableArray alloc] initWithCapacity:mapWidth*mapHeight];
Which I had hoped would create and fill my MutableArray so I can get then access it with this kind of index...
int ii = (cellY*mapWidth)+cellX;
NSDictionary *currentObject = [gameObjects objectAtIndex:ii];
But I just learned initWithCapacity doesn't fill the array, so should I create blank objects to fill it with, or is there a Null that I can fill it with? Also would I do that with 2 for loops or is there an instruction something like "initWith:myObject" ?
I want to be able to check at a certain index within the array to see if there's an object there or not, so I need to be able to acces that index point, and I can only do that if there's something there or I get an out of bounds error.
I'll be using this NSMutableArray pretty much as a grid of objects, it's a 1 dimensional array organised as a 2 dimensional array, so I need to be able to fill it with mapWidth*mapHeight of something, and then calculate the index and do a check on that index within the array.
I've looked on here and googled but couldn't find anything like what I'm asking.
Thanks for any advice.
I think what you are looking for is [NSNull null]. It is exactly what you want- a placeholder value.
You can find more information on the topic in this question.
initWithCapacity is just a performance optimization -- it has no effect on the array behavior, it just keeps the code "under the covers" from having to repeatedly enlarge the internal array as you add more entries.
So if you want a "pre-allocated" array, you'd need to fill it with NSNull objects or some such. You can then use isKindOfClass to tell if the object is the right type, or simply == compare the entry to [NSNull null]. (Since there's only ever one NSNull object it always has the same address).
(Or you could use a C-style array of pointers with nil values for empty slots.)
But you might be better off using an NSMutableDictionary instead -- no need to pre-fill, and if the element isn't there you get a nil pointer back. For keys use a NSNumber object that corresponds to what would have been your array index.
initWithCapacity only hints to NSMutableArray that it should support this many objects. It won't actually have any objects in it until you add them. Besides, every entry in the array is a pointer to an object, not a struct like you'd normally have in a standard c array.
You need to change how you're thinking about the problem. If you don't add an object to the array, it's not in there. So either you pre-fill the array with "empty" objects as you've said, which is weird. Or you can add the objects as you need them.

NSMutableArray storing integers

I know this is probably basic... I’m trying to store an answer to a multiple choice question (i.e. integer 1-4) stored in “selection”. The question number is “gCount”. This prints nil for the answerSubmit array. What part is wrong? Thanks...
[answerSubmit insertObject:[NSNumber numberWithInt:selection] atIndex:gCount];
NSLog(#"Answer submit: %#\n",[answerSubmit objectAtIndex:gCount]);
If you have those two statements, exactly as shown, back-to-back in your program, and if the printed result was "Answer submit: (nil)", then "answerSubmit" is nil going in.

How to append to the end of a C Array

I've been programming for a while in objective-c, but I've unfortunately never delved very deeply into C and memory pointers, although I do have a rudimentary understanding of them. I'm working with an array of CLLocationCoordinate2D structures, and I'm trying to figure out how to append to the array. First of all, I get the
NSString *aString; //a bunch of coordinates
CLLocationCoordinate2d *coordinates;
int length;
doSomethingCool(aString, &coordinates, &length);
after I do something cool, I want to preserve it in a class variable. If I simply do something like
points = newPoints
points contains the appropriate contents. However, if I try to do something like this:
points = malloc(sizeof(CLLocationCoordinate2D) * length);
points[0] = *newPoints;
points ends up with contents different from newPoints.
Ultimately my goal is to be able to append to points based on length, but I'm not going to be able to do that if I can't get the above code to work. What am I doing wrong?
Your code simply copies the first value of newPoints into the first value of points (*newPoints is equivalent to newPoints[0]).
One situation is to make a new array, copy all values, switch the arrays, and free() the old one. For example:
int* newvals = malloc(sizeof(int) * newcount);
memcpy(newvals, vals, sizeof(int) * oldcount);
free(vals);
vals = newvals;
You can also use realloc - its behavior is similar to the above (though it can fail!), but at times may be more efficient.
Note that you simply can't change the underlying pointer's size in a safe and portable fashion. You will need to update your instance ("class") variable with the new pointer.
The idea would be to copy all of the array into a temporary array, resize the original, and then copy them back. However, managing this could get hairy. You'd be better off using an std::vector and just appending it.
EDIT: I just realized you're using C, not C++. Disregard the second half of this.

Arrays in Objective-C

If you want to use an array of characters/badguys is it better to have one array of badguy objects and each object have properties like Badguy1: color=blue, health=80. Then you loop through your array of characters and pull that information.... OR is it better to have multiple small arrays like character array, color array, health array and the index of all 3 arrays align to be the same character?
I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array.
I ask just because it seems like it would make more sense to use a single array and pull the parts that you need.
I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array. I ask just because it seems like it would make more sense to use a single array and pull the parts that you need.
You are correct in that it's generally smarter and a better design decision to keep related things together. If you'd like to store them this way, you can simply do:
// Get the character at index 3 in the "characters" array
// and print how many hit points it has left.
GameCharacter *ch = [characters objectAtIndex:3];
NSLog(#"This character has: %# hit points", ch.hitPointsRemaining);
Actually, now that I think about it. I guess I should just have an array of pointers and then get my values from the objects and not try to store all of that in the array.