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



No comments:

Post a Comment

Tweet