Solved

return value

Posted on 2006-10-21
3
185 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 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now