[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

return value

Posted on 2006-10-21
3
Medium Priority
?
202 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

834 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