Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. return value

return value

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    messages
    wrote on last edited by
    #1

    This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?

    int _tmain(int argc, _TCHAR* argv[])
    {
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    int sum;

      sum=BinSearch(arr,12,0,9);
    	  cout< A\[mid\])
    		return Binary\_Search(A,item,mid+1,height);
    	else
    		return Binary\_Search(A,item,low,mid-1);
    

    }

    J A 3 Replies Last reply
    0
    • M messages

      This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?

      int _tmain(int argc, _TCHAR* argv[])
      {
      int arr[10] = {1,2,3,4,5,6,7,8,9,10};
      int sum;

        sum=BinSearch(arr,12,0,9);
      	  cout< A\[mid\])
      		return Binary\_Search(A,item,mid+1,height);
      	else
      		return Binary\_Search(A,item,low,mid-1);
      

      }

      J Offline
      J Offline
      Joe794
      wrote on last edited by
      #2

      1st line should be: if(low == height)return -1; // Return "Not Found"

      1 Reply Last reply
      0
      • M messages

        This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?

        int _tmain(int argc, _TCHAR* argv[])
        {
        int arr[10] = {1,2,3,4,5,6,7,8,9,10};
        int sum;

          sum=BinSearch(arr,12,0,9);
        	  cout< A\[mid\])
        		return Binary\_Search(A,item,mid+1,height);
        	else
        		return Binary\_Search(A,item,low,mid-1);
        

        }

        J Offline
        J Offline
        Joe794
        wrote on last edited by
        #3

        Add as 1st line in BinSearch: if(low > height)return -1; // Return 'not found' code

        M 1 Reply Last reply
        0
        • J Joe794

          Add as 1st line in BinSearch: if(low > height)return -1; // Return 'not found' code

          M Offline
          M Offline
          messages
          wrote on last edited by
          #4

          Thank you Joe794.

          1 Reply Last reply
          0
          • M messages

            This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?

            int _tmain(int argc, _TCHAR* argv[])
            {
            int arr[10] = {1,2,3,4,5,6,7,8,9,10};
            int sum;

              sum=BinSearch(arr,12,0,9);
            	  cout< A\[mid\])
            		return Binary\_Search(A,item,mid+1,height);
            	else
            		return Binary\_Search(A,item,low,mid-1);
            

            }

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            The array passed into the function could potentially have any value in it you can't select a simple return value to represent your error condition. I'd consider throwing an exception rather than using a return value:

            if( A[low] > item || A[high] < item] ) throw binary_search_out_of_bounds();

            Another option is to have the function return a std::pair<bool, int> where the first item in the pair tells the caller if they have an error condition or not and the second is the value they're after. Personally I find that a bit ugly and it complicates your code more as you have to do more in the calling function so I'd go with an exception. Cheers, Ash

            M 1 Reply Last reply
            0
            • A Aescleal

              The array passed into the function could potentially have any value in it you can't select a simple return value to represent your error condition. I'd consider throwing an exception rather than using a return value:

              if( A[low] > item || A[high] < item] ) throw binary_search_out_of_bounds();

              Another option is to have the function return a std::pair<bool, int> where the first item in the pair tells the caller if they have an error condition or not and the second is the value they're after. Personally I find that a bit ugly and it complicates your code more as you have to do more in the calling function so I'd go with an exception. Cheers, Ash

              M Offline
              M Offline
              messages
              wrote on last edited by
              #6

              Thank you Aescleal.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups