am I blind

This is a discussion on am I blind within the C Programming forums, part of the General Programming Boards category; Code: int Count(float A[], int Size, float X) { if (Size <= 0) { return 0; } else return (A[0] ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    Question am I blind

    Code:
    int Count(float A[], int Size, float X)
    {
        if (Size <= 0)
          {     
           return 0;
            }
        else
           return (A[0] == X) + count(A + 1, Size - 1, X); 
    }
    My book has this as an example but I am lost as to what it does and it doesnt say what it does or break it down. I am trying desperately to learn recursive functions
    does it set each element in A to X
    or does it return the number of elements in A

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You already know the number of elements in A; it's given as a parameter, Size. remember that arrays don't carry their size with them in C -- you need to pass them separately, or perhaps encode the size into the array somehow (for example C-style strings, which are really arrays of characters, delimit the end of the string with an entry, the number zero). So realize that A isn't a container of any kind. It identifies a location in the vast sea of memory, a point after which a number of interesting and useful floating point values are placed (that number of values being Size, as far as this function's concerned).

    Now, considering that it's named Count, it probably counts something. Considering that it gets passed a floating point number X, it probably counts the number of elements equal to X. Or maybe it counts the number of elements not equal to X! Or maybe it counts the number of elements greater than X. To figure this out of course we must look closely at the code.

    If Size is zero, then our array has size zero. And we return zero. Okay. The idea that it's counting _something_ has not been ruled out.

    If the size is not zero, we return, uh, the value (A[0] == X) + count(A + 1, Size - 1, X). Now realize that the == function always returns 0 or 1 -- returning 1 if the values compared are equal, 0 if not. It adds this value to the value count(A + 1, Size - 1, X).

    Okay. Well, what does count(A + 1, Size - 1, X) do? It looks like it counts something in an array, starting at the memory location A + 1, of length Size - 1, involving the value X. (Well, it doesn't "do" anything, since it doesn't modify any external values or perform input or output -- it just returns a number. So the question should really be worded, "Well, what does count(A + 1, Size - 1, X) equal?")

    So, it implements the following algorithm:

    To count the number of values equal to X, of the 'Size' values starting at position A in memory, first ask yourself if Size is zero (or less).
    If Size is zero, then
    ... there are zero values in the region that are equal to X. (Duh.)
    If Size is not zero, then
    ... take the number of values equal to X, of the 'Size - 1' values starting at position 'A + 1' in memory, and add to that: 1, if the value in memory at position A[0] is equal to X, or 0, if that is not the case.

    So the recursive implementation follows closely what is really a _definition_ of how many values in the array are equal to X. You'll find that recursive functions, if they don't modify anything, tend to be rewordings of a mathematical specification of the function's behavior.
    Last edited by Rashakil Fol; 05-18-2007 at 12:27 AM.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    Thank you for the thorough explanation i was getting wrapped around the == when I thought it was supposed to be = and well you have outdone yourself!

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    C is case-sensitive, Count() is different from count()
    Last edited by zacs7; 05-18-2007 at 01:56 AM.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    ya think?
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes.
    Some computer languages are case sensitive (Java, C++, and C), while others are case insensitive (ie not case sensitive), for example BASIC (including ...
    [edit] Make a stupid statement, get a link to google. Or something like that. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed, going blind.
    By datait in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 08:01 AM
  2. Bachelor for blind and deaf
    By afreedboy in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 12-18-2003, 07:42 AM
  3. Blind date...Oh, my God!
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 09-18-2001, 11:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21