Showing posts with label ARRAYS. Show all posts
Showing posts with label ARRAYS. Show all posts

Thursday, August 4, 2011

Objective-C: Merging(Mixing) two arrays in one

Good Morning, Sports,


Continuing with the last post.
This is the code that merges two arrays (if you guys ever learned COBOL, you'll get quite familiar).
It's simple and straight forward.


-(void)arrayMixer{
    
    int A[18], B[18], ARRAY[36];
    
    for (int i=1; i<=2; i++){
        for (int k=0; k<=17; k++){
            if (i == 1){
                //CMT01
                A[k] = [self returnArrayNumber:i atIndex:k];
            } else {
                B[k] = [self returnArrayNumber:i atIndex:k];
            }
        }
    }
    
    int l=0, m=0;
    for (int i=0; i<=35; i++){
        if (i%2 == 0){
            ARRAY[i] = A[m];
            NSLog(@"A[%d]=%d | ARRAY[%d]=%d", m, A[m], i, ARRAY[i]);
            m++;
        } else {
            ARRAY[i] = B[l];
            NSLog(@"B[%d]=%d | ARRAY[%d]=%d", l, B[l], i, ARRAY[i]);
            l++;
        }
    }    
}


CMT01: This piece of code is a function that retrieves me the value of the array #i in the index #k


Well as you can see, the first two "fors" i use just to get back my values, then the second one is the hardcode stuff.
It fills up ARRAY[36] with arrays A[] and B[], A[] when the rest of the division of the counter is 0 and B[] when it's not 0.


To create the two arrays (A[] and B[]) you can use the post before this one.
That's it.
Peace



Wednesday, August 3, 2011

Objective-C: Array creating with no repeated numbers in

Dudes & Ladies,


Here's a piece of code that just came at me for a game we're bringing up.
What does it do? It creates a array with any number of possible positions and stocks aleatory non-repeated numbers in each index.
Cool, huh?


CODE:

-(void)createArrayDude{

    int AN=1, indx = 36, array[indx], arcvalue; 
    
    for (int k=0; k<=(indx-1); k++){
        jumpLabel:
        arcvalue = arc4random()%(indx+1);
        if (arcvalue == 0){
            goto jumpLabel;
        }
        for (int m=0; m<=k; m++){
            if (arcvalue == array[m]){
                goto jumpLabel;
            }
        }
        array[k] = arcvalue;
       //CMT01
       [self saveArrayNumber:AN stocking:array[k] atIndex:k];
        NSLog(@"ARRAY[%d]=%d", k, array[k]);
    }
}


CMT01: The line under saves the array right in the memory(kind of visible because of the name, eh?)

In the indx you can use any number.
Just be happy.
Tweet