Solved

return value

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set environment variables in C 2 86
Console based application in Linux 1 92
smtp c source code 7 85
How to install SVN Command Line Client? 5 78
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

790 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