PDA

View Full Version : Iphone Help (String Method help Needed) Xcode

Dylan552
Jan 22, 2011, 08:56 PM
if(calledNumbers.contains(recent))
{
number = 1 + Bingo.nextInt(75);
}

so this is in java and it basically says if the random number has already been added then gernrate a new one. Also recent is the last number called placed in a string and callednumbers is a string of all the numbers. EX: "75 55 33 "So i was looking at the RangeofString in objective c and wnat to know how to do that in objective c . PLease help!

Thanks!!

wlh99
Jan 22, 2011, 10:16 PM
I wouldn't use any type of string to store a list of numbers that I have randomly picked already. I would use an array of numbers. A simple c array of int's would probably be easiest.

Also, rather than picking a random number, then checking if it has already been called, why not initialize the array with numbers 1-75, shuffle the array, then just call the numbers in order? Then you can eliminate the logic altogether.

Dylan552
Jan 22, 2011, 10:38 PM
I wouldn't use any type of string to store a list of numbers that I have randomly picked already. I would use an array of numbers. A simple c array of int's would probably be easiest.

Also, rather than picking a random number, then checking if it has already been called, why not initialize the array with numbers 1-75, shuffle the array, then just call the numbers in order? Then you can eliminate the logic altogether.

i was going to use an array but didnt know how to do this could you explain thx

wlh99
Jan 23, 2011, 12:18 AM
i was going to use an array but didnt know how to do this could you explain thx

Unfortunately I don't have time to write a tutorial. Google is your friend, just search for "c array"

int numbers[74];
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}

Would create an array of 75 elements, and fill it with the numbers 1 - 75.

RonC
Jan 23, 2011, 08:30 PM
Unfortunately I don't have time to write a tutorial. Google is your friend, just search for "c array"

int numbers[74];
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}

Would create an array of 75 elements, and fill it with the numbers 1 - 75.

Not exactly, it would create a vector with 74 elements, fill it with numbers 1-74, and then the value 75 would be written to the memory address immediately after the end of the 74th element.

That will likely cause weird unpredictable stuff to happen. Not in a simple program like this one, but buried somewhere in even a modestly more complex program will make you pull your hair out. (Consider that a lesson from my past; I'm very sensitive to those things now.)

Ron C.

dejo
Jan 23, 2011, 08:43 PM
Not exactly, it would create a vector with 74 elements, fill it with numbers 1-74, and then the value 75 would be written to the memory address immediately after the end of the 74th element.

Code looks fine to me and should work as predicted. Why do you say otherwise?

chown33
Jan 24, 2011, 12:30 AM
Code looks fine to me and should work as predicted. Why do you say otherwise?

I think you missed something.

This declaration:
int numbers[74];

is an array of 74 ints. The range of valid subscripts is 0 thru 73, inclusive.

This loop:
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}

will overflow the array when i exceeds 73. There is one integer that exceeds 73 (the max valid array index) and is less than 75 (the loop ending condition): 74. So when i is 74, the assignment in the loop body will write to a location past the end of the array.

dejo
Jan 24, 2011, 09:22 AM
I think you missed something.
Oops, you're right. I did miss something. Thanks for the clarification! :)

Dylan552
Jan 24, 2011, 11:49 AM
I think you missed something.

This declaration:
int numbers[74];

is an array of 74 ints. The range of valid subscripts is 0 thru 73, inclusive.

This loop:
for (int i=0;i<75;i++) {
numbers[i]=i+1;
}

will overflow the array when i exceeds 73. There is one integer that exceeds 73 (the max valid array index) and is less than 75 (the loop ending condition): 74. So when i is 74, the assignment in the loop body will write to a location past the end of the array.

so how whould i chec to c if a number has already been called?

RonC
Jan 25, 2011, 09:07 AM
so how whould i chec to c if a number has already been called?

You're on the right path. I'll assume that "calledNumbers" is some kind of container. That container has the "contains()" method defined on it.

The equivalent iOS container is likely CFMutableSet (a subclass of CFSet). Look it up (here (https://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFMutableSetRef/Reference/reference.html%23//apple_ref/doc/uid/20001503)) and see if it works for you. You can probably get away with using int's because it stores (and likely compares) pointers (as "void *").

I don't like that pointer comparison thing, since two objects that happen to be equal will not compare as equal when you compare pointers to these apparently equal values, but as long as you know going in it can be ok.

if you have a CFMutableSet bob and int newRandomValue, you can use CFSetContainsValue(bob, (void *)newRandomValue) to test if the element exists in the set, and you can add it into the set by using CFSetAddValue(bob,(void*)newRandomValue).

No guarantees on this; give it a spin and see what happens.

Ron