element ?

This is a discussion on element ? within the C Programming forums, part of the General Programming Boards category; I have a question about the elements of A in the following: Code: int Tally(float A[], int Size, float X) ...

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

    Question element ?

    I have a question about the elements of A in the following:
    Code:
    int Tally(float A[], int Size, float X)
    {
        if (Size <= 0){     /* to see if it is 0 or neg numb and returns a*/
           return a;}
        else
           return (A[0] == X) + Tally(A + 1, Size - 1, X); /*first element is checked against X and tally a is incremented by on size decremented*/
    }

    Doesnt this code return the number of elements in A equal to x

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    No you'd need something like:

    return (A[0] != X) ? 0 : 1 + Tally( A + 1, Size - 1, X );

    in this way, Tally always completes the recursion.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    Does it return the number of elements in a then?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by matthughes View Post
    Does it return the number of elements in a then?
    It doesn't do anything, since there is no such thing as "a"

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    27
    Quote Originally Posted by brewbuck View Post
    It doesn't do anything, since there is no such thing as "a"
    instead of a it was supposed to be
    Code:
     return 0;

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    27
    i got it people it returns the number of elements equal to x!!!

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by citizen View Post
    No you'd need something like:

    return (A[0] != X) ? 0 : 1 + Tally( A + 1, Size - 1, X );

    in this way, Tally always completes the recursion.
    Uh... what?
    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.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Rashakil Fol View Post
    Uh... what?
    Code:
    A[0] != X
    Is an expression it evaluates to TRUE (1) or FALSE (0).

    ie, it stops recursing once A[0] is not equal to X, otherwise it continues to recurse.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. sorting
    By penny_731729 in forum C Programming
    Replies: 3
    Last Post: 04-28-2003, 10:56 AM

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