Thread: in need of a little explaination..

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    in need of a little explaination..

    so i am given this code...

    insert
    Code:
    #include <stdio.h>
     
    void function1(int array[], int length);
     
    int main() {
        int values[] = {2, 5, 1, 0, 3, 4};
        function1(values, 6);
        return 0;
    }
     
    void function1(int array[], int length) {
        
        int temp[length], i;
       
        for (i=0; i<length; i++) 
            temp[i] = array[array[i]];
           
        for (i=0; i<length; i++)
            array[i] = temp[i];     
        
        for (i=0; i<length; i++)
            printf("%d ", array[i]);
    }
    and i have to answer this question...

    1) Let the array values be an integer array of size 6 storing the following values (in this order): 2, 5, 1, 0, 3, 4. What is the output of running the function call function1(values, 6)?

    A) 2 5 1 0 3 4
    B) 0 1 2 3 4 5
    C) 5 4 3 2 1 0
    D) 1 4 5 2 0 3
    E)NOTA

    The answer is D can anyone explain why? i just dont get it...

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The key is in line 16. Let's work through it step by step:

    When i=0, array[i] is equal to 2 because 2 is the 0th element of the array.
    So array[array[0]] = array[2] = 1.

    When i=1, array[i] is 5. So array[array[1]]=array[5]=4.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    soooo
    when i = 2, array[i] is 1. So array[array[2]]=array[1]=5?

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    thanks! it actually makes sense now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need explaination of the following function
    By dianazheng in forum C Programming
    Replies: 5
    Last Post: 06-09-2005, 10:34 AM
  2. Explaination of behavior in C
    By Mister C in forum C Programming
    Replies: 14
    Last Post: 08-15-2004, 05:57 PM
  3. Explaination of EOF
    By Stinky in forum C Programming
    Replies: 1
    Last Post: 11-05-2003, 01:18 PM
  4. relation explaination
    By asli_masti in forum C Programming
    Replies: 1
    Last Post: 08-28-2003, 06:55 AM
  5. Explaination
    By rogerlpe in forum C++ Programming
    Replies: 4
    Last Post: 03-11-2002, 07:28 AM