pls help me w/ this c problem
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: pls help me w/ this c problem

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    534

    pls help me w/ this c problem

    Im supposed to write a recursive function to count how many time does a number occur in an array. Function is in a format: count(number, array[], length);

    Look at the attached txt file

    I am getting an error in this line: our_sum = count(1, our_array[], 8);
    error: syntax error before ] token

    What am I doing wrong???

  2. #2
    Senior Member Deeboe's Avatar
    Join Date
    Nov 2005
    Posts
    185
    Oh man... I need to go back to coding school.

    I might be WAY off here, but is it expecting a number between the brackets in our_array[]? It seems like you are passing our_array[] into array[9] and there is not a match there. Maybe remove the 9 from the one, or put a varible in each?

    Sorry, that is my best guess. I will go home and cry into my programming books.

    -Deeboe
    If you know the enemy and know yourself, you need not fear the result of a hundred battles. If you know yourself but not the enemy, for every victory gained you will also suffer a defeat. If you know neither the enemy nor yourself, you will succumb in every battle.
    - Sun Tzu, The Art of War

    http://tazforum.**********.com/

  3. #3
    Banned
    Join Date
    Aug 2004
    Posts
    534
    if i define array[9] or array[] in the count function ... same error

    when i pass our_array[] to a count function i know for a fact you don't have to specify elements

  4. #4
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Wound up changing how it was done b/c I was having problems resolving the recursion in my head the way you did it, but here goes, tested it and it worked (changed array to make sure it worked in a different case). I'd probably chose to do it differently, but chose to mirror what you had as closely as I could:

    Code:
    #include <stdio.h>
    
    int count(int number, int array[], int length){
            if(length < 0) { return 0; }
            else
            {
                    if(array[length-1] == number) {
                            return 1+ count(number, array, length-1);
                    }
                    else
                    {
                            return count(number, array, length-1);
                    }
            }
    }
    
    int main(int argc, int **argv){
    
        int our_sum = 0;
        int our_array[] =  {0, 3, 1, 2, 3, 1, 5, 1, 7, 8};
    
        our_sum = count(3, our_array, 9);
    
        printf("1 is in the array %d times\n", our_sum);
        getchar();
        return(0);
    }
    In your original, pull the [9] off the function declaration and just make it [] and when you call it in the main, you don't need the []. When I use your code as is with the above change it compiles but segmenation faults (not uncommon for bugged recursion). Also personal pet peeve: int main() --> int main(int argc, int ** argv).

    Anyway, like I said before, couldn't follow how y ou were using the recursion and this is probably a poor application for it, considering there are more classical uses for recursion.
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  5. #5
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    I deleted my original post, since Nebulus posted the solution in the meantime.
    A little correction, however
    Code:
    if(length < 0) { return 0; }
    should be modified to
    Code:
    if(length ==  0) { return 0; }
    otherwise "array[length-1]" can access "array[-1]".

    Another remark: "int array[]"
    Nebulus has chosen this way to define the function arguments.
    Another way is to use "int *array". The advantage of these two
    compared with "int array[9]" is that arbitrary array-sizes are
    allowed.

    when i pass our_array[] to a count function i know for a fact you don't have to specify elements
    You actually should pass, as Nebulus did, "our_array", rather than "our_array[]".
    Or, if you wish, "&(our_array[0])"


    Cheers.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  6. #6
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Lol, yup your right about the length. I was screwing aroun d with several things (array subscripts, using --length, etc) and forgot to change that back. Oh well, pretty good considering I haven't seriously coded in C in ohh...hmm...7 years ?

    EDIT: Two important concepts that weren't really mentioned with recursion:

    1) Always, always, always make sure you have a terminating condition in the recursion, lest it be infinite.
    -- In your code, you never checked length, you just kept calling count. So it would happily go along trying to access array[-1] array[-2], etc until it caused a core dump/segmentation fault. This is why the statement if(length <= 0) is necessary, it provides a condition for the recursion to end (and in this case returns zero since there is no array element -1, there would never be a match).

    2) In this case, you want to provide a way of transferring values from recursive call to recursive call. Since you kept declaring a local variable sum, it would keep overwriting whatever you had. You can see I chose to address this with the return. So lets say you had: array= {0, 3, 2, 3} and you called sum = count(3, array, 4) and chose to print out the value of length and the value returned everytime...

    It'd look like:

    4, return 1+count(3, array, 3)
    3, return count(3, array, 2)
    2, return 1+count(3, array, 1)
    1, return count(3, array, 0)
    return 0

    You'd then go from the bottom, upwards...so replace : 1, return count(3, array, 0) with the return from the line below, or 1, 0. Then on the next line, return 1+count(3, array, 1) would become 2, 1, and so on and so forth, until the first line looked like 4, 2, and therefore sum would be 2.

    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  7. #7
    Banned
    Join Date
    Aug 2004
    Posts
    534
    I know what you are saying about "resolving recursion in my head"

    The reason i'm doing these problems is because for some reason it is very hard for me to "wrap my head around" a lot of recursion algorithms. I am just trying to find as many problems as I can find until it'll become "no-brainer" for me.

    Thanx guys

    ... neb... system says: gotta spread AP's before giving to you

  8. #8
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Was editing while you posted, wanted to give the thread a bump since I attempted to explain the recursion...
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  9. #9
    Banned
    Join Date
    Aug 2004
    Posts
    534
    You are right by starting w/ the topmost element in calling the function " (3, our_array, 9)"

    but the function starts counting from "array[length-1]" which mean that if the number you are looking for is in the last element you'll miss it


    take out "-1"

  10. #10
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Originally posted here by unhappy
    You are right by starting w/ the topmost element in calling the function " (3, our_array, 9)"

    but the function starts counting from "array[length-1]" which mean that if the number you are looking for is in the last element you'll miss it

    how can we fix that? be right back w/ ideas
    Negative. In C, arrays are indexed 0 to length-1. for example:

    Code:
    int *array = {1, 2, 3, 4};
    int lenth = 4;
    int i=0;
    
    for(i=0; i &lt; (length-1); i++)
    {
    printf("array[%d] = %d\n", i, array[i]);
    }
    Will print:
    array[0] = 1
    array[1] = 2
    array[2] = 3
    array[3] = 4

    Since you are passing the length/size of the array in the call, to hit the max index of the array you have to use length-1.
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •