Thread: What is the effect of this?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    What is the effect of this?

    Here is the following recursive program

    [code]
    #include<stdio.h>

    void Mystery(int A[], int size)
    {
    if (size > 0) {A[0] = 0; Mystery (A+1, size -1);}
    }

    int main(void)
    {
    int A
    Mystery ();
    return 0;
    }
    [/code

    a. Its sets 1st element of array A to 0.
    b. sets last ...
    c. sets all...
    d. generates an infinite recursion loop
    Now before anyone comes up with any smart comments. I already wrote a program to see and it should be obvious that it did not work because i chose ans. D. Now you can either help or not, the choice is up to you?
    Thanks to anyone who does.
    Last edited by correlcj; 07-31-2002 at 05:23 PM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main(void)
    {
    int A
    1) You forgot a ;

    2) A is not an array.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    int A
    post the rest of this line
    Edit - damn got beat

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    1) You forgot a ;

    2) A is not an array.

    Quzah.



    What do you mean Quzah? Educate me? I still do not understand.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    1) You forgot a ;
    Statements need to be ended with a semicolon

    2) A is not an array.
    You declared A as int A but you passed in A[9] in your mystery function. You're accessing memory that you're not supposed to.

    void Mystery(int A[], int size)
    You passed in a value into your function (A[9]). Then you treat it as an array which isn't valid.
    It seems as if you want to declare something like int A[SIZE];
    Then call your function like this Mystery (&A[9], 4);

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    It seems like your intentions were more along the lines of:

    Code:
    #include<stdio.h> 
    #define SIZE 10
    
    void Mystery(int A[], int size) 
    { 
       if (size > 0) {A[0] = 0; Mystery (A+1, size -1);} 
    } 
    
    int main(void) 
    { 
       int i;
       int A[SIZE];
       for(i = 0; i < SIZE; i++)
          A[i] = -1;
       Mystery (A, 4);
       for(i = 0; i < SIZE; i++)
          printf("A[%d] = %d\n", i, A[i]);
       return 0; 
    }
    This function recursively sets your elements to 0. In this case, I passed in the address of A[0] with size of 4. Therefore the first four elements A[0]...A[3] will be set to 0.

    Hope that helps
    Last edited by Cshot; 07-31-2002 at 05:32 PM.

  7. #7
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Hope that helps

    it did alot.
    thanks Cshot!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Type Writer Effect help please.
    By Fujitaka in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 10:11 AM
  2. Disable 3D effect
    By underline_bruce in forum Windows Programming
    Replies: 1
    Last Post: 08-11-2007, 04:10 PM
  3. operator has no effect ??
    By studentc in forum C Programming
    Replies: 3
    Last Post: 05-28-2004, 07:28 AM
  4. effect of following code
    By cruzinny in forum C Programming
    Replies: 2
    Last Post: 10-22-2002, 04:44 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM