Closed Thread
Results 1 to 5 of 5

Thread: Is my recursion correct?

  1. #1
    Gear2d is offline Newbie
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Is my recursion correct?

    Hello all, I need help with my pseucode recursion for a search program for my class. This type of search is a sequential type of search. You give the program an array of n elements and a value to search for, and it will return the location of the element (only one match needed). If it is not found then it will return a -1. Here is the non-recursive one below followed by the recursive one I made (if anyone can tell me my mistakes): Here it runs from the end of the array instead of the start.

    Non-Recursive:

    Algorithm SS(A,n,X,B)
    Input: A[n] integers and integer X (X is the one we want to find in the
    array)

    Output: Integer B such that B is largest integer such that A[B] = X or B =
    -1 if A contains no X

    For B=n-1 to 0 do
    If A[B] = X then
    Return I

    Return I


    Recursive:

    Algorithm recursiveSS(A,n,X,B)

    Input: A[n] integers and integer X (X is the one we want to find in the
    array)

    Output: Integer B such that B is largest integer such that A[B] = X or B =
    -1 if A contains no X

    B <-- n-1

    If B = -1 //Test and see if B == -1
    Return B //Return -1 if B == -1 is true

    If A[B] = X then //compare
    Return B //Return the index of where the first match occurs in the
    array as its looked at from n-1 to 0

    Return recursiveSS( A, n-1, X, B)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Is my recursion correct?

    Quote Originally Posted by Gear2d View Post
    Hello all, I need help with my pseucode recursion for a search program for my class. This type of search is a sequential type of search. You give the program an array of n elements and a value to search for, and it will return the location of the element (only one match needed). If it is not found then it will return a -1. Here is the non-recursive one below followed by the recursive one I made (if anyone can tell me my mistakes): Here it runs from the end of the array instead of the start.

    Non-Recursive:

    Algorithm SS(A,n,X,B)
    Input: A[n] integers and integer X (X is the one we want to find in the
    array)

    Output: Integer B such that B is largest integer such that A[B] = X or B =
    -1 if A contains no X

    For B=n-1 to 0 do
    If A[B] = X then
    Return I

    Return I


    Recursive:

    Algorithm recursiveSS(A,n,X,B)

    Input: A[n] integers and integer X (X is the one we want to find in the
    array)

    Output: Integer B such that B is largest integer such that A[B] = X or B =
    -1 if A contains no X

    B <-- n-1

    If B = -1 //Test and see if B == -1
    Return B //Return -1 if B == -1 is true

    If A[B] = X then //compare
    Return B //Return the index of where the first match occurs in the
    array as its looked at from n-1 to 0

    Return recursiveSS( A, n-1, X, B)
    It is indeed a recursive algorithm as the function calls itself, and the logic appears to be correct. If you know a programming language, this would be simple enough code to check the logic.

  4. #3
    butidonot is offline Newbie
    Join Date
    Sep 2008
    Posts
    14
    Rep Power
    0

    Re: Is my recursion correct?

    Here is the non-recursive one below followed by the recursive one I made (if anyone can tell me my mistakes): Here it runs from the end of the array instead of the start.
    MCSE

    ccna

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Is my recursion correct?

    I don't see any code, butidonot.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Is my recursion correct?

    Quote Originally Posted by butidonot View Post
    Here is the non-recursive one below followed by the recursive one I made (if anyone can tell me my mistakes): Here it runs from the end of the array instead of the start.
    MCSE

    ccna
    Is this your attempt to spam CodeCall by using legitimate posts? It sure seems like it.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. i need to correct my program
    By king_programmer in forum C and C++
    Replies: 4
    Last Post: 07-19-2010, 12:37 AM
  2. How Do I Get The Date Order To be correct?
    By byronwells in forum PHP Development
    Replies: 6
    Last Post: 02-03-2010, 01:09 AM
  3. Am I correct?
    By unleashed-my-freedom in forum Visual Basic Programming
    Replies: 1
    Last Post: 01-10-2010, 05:53 AM
  4. does anyone know how to correct this in greenfoot?
    By maximus123 in forum Java Help
    Replies: 0
    Last Post: 05-04-2009, 06:20 AM
  5. Any reason I cannot get correct math?
    By Subgenius in forum Visual Basic Programming
    Replies: 12
    Last Post: 11-15-2008, 05:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

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