Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

return value

Posted on 2006-10-21
3
Medium Priority
?
198 Views
Last Modified: 2010-04-15
could someone explain to me what is different in return 0 and return -1

   /* binsearch:  find x in v[0] <= v[1] <= ... <= v[n-1] */
   int binsearch(int x, int v[], int n)
   {
       int low, high, mid;

       low = 0;
       high = n - 1;
       while (low <= high) {
           mid = (low+high)/2;
           if (x < v[mid])
               high = mid + 1;
           else if (x  > v[mid])
               low = mid + 1;
           else    /* found match */
               return mid;
       }
       return -1;   /* no match */ ----------------->?
   }


0
Comment
Question by:rmtogether
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17780982
return 0 means the value is found on the first position in the array, the array starting at index 0, as explicitely noted in the comment   /* binsearch:  find x in v[0] <= v[1] <= ... <= v[n-1] */

return -1 means the value is not found in the array at all, again as noted in the comment:
/* no match */ ----------------->?
0
 

Author Comment

by:rmtogether
ID: 17781155

does return -1 means return v[-1]? does it must use -1 or can be any negative number?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 17781252

>does return -1 means return v[-1]?
no. the function is written to return the index in the array v that contains the value v.
return -1 means that the value is not found, and to indicate that the "index" -1 is returned.

>does it must use -1 or can be any negative number?
it could be any negative number, but the code that calls the function has to know about that special return value to compare to, unless it checks if the returned value is in the bounds (0  - n-1) of the array size.

note that -1 is used in most cases when developing functions that return index positions of values to be found.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question