Home>

In C language, I am creating a program that reads data and searches for numbers. The following are given specifications.
✓ Implemented "binary search method" and "sequential search method" as search algorithms
✓ Compare the calculation time of each search algorithm
✓ Enter from the search key or keyboard
✓ If the search result is not found, a message to that effect is displayed.

In "Binary search method" and "Sequential search method", when the search key is not found, I would like to display "not found.", But "found. 0" is displayed.
This is because the return value is set to 0 when it remains to the end in the BinarySearch function and FullSearch function, but I do not know how to change this to obtain the desired display. How can I set the return value if there is no match (search key not found)?

The corresponding code is shown below. Just in case, I've shown the whole code, but I've commented out the parts that don't seem to affect this question.
Su.

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
/ * int * ReadDataFile (const char * FileName, int * N) {
  int i;
  FILE * file;
  int * Data;
  file = fopen (FileName, "r");
  if (file == NULL) {// If the file could not be opened correctly
    fprintf (stderr, "cannnot open file:% s \ n", FileName);
    exit (1);// Forced termination
  }
  for (i = 0 ;;i ++) {
    double dummy;
    if (fscanf (file, "% lf",&dummy)! = 1) {
      break;// Break if reading fails
    }
  }
  * N = i;
  printf ("data size is% d \ n", i);
  Data = (int *) malloc (sizeof (int) * i);// Dynamic allocation of memory
  if (Data == NULL) {// In case of memory allocation failure
    fprintf (stderr, "cannnot allocate memory \ n");
    exit (1);// Forced termination
  }
  rewind (file);// Prepare to reread the file from scratch
  for (i = 0;i<* N;i ++) {
    fscanf (file, "% d",&Data [i]);
  }
  fclose (file);// Close the file
  return Data;
}
int compare (const void * a, const void * b) {
  return * (int *) a-* (int *) b;
}
double gettimeofday_sec () {
  struct timeval tv;
  gettimeofday (&tv, NULL);
  return tv.tv_sec + tv.tv_usec * 1e-6;
} * /
int BinarySearch (int * Data, int N, int key) {
  int l, r, m;// left, right, middle
  l = 0;
  r = N-1;
  while (l<= r) {
        m = (l + r)/2;/* calc of middle key * /
        if (Data [m] == key) {
            break;
        } else if (Data [m]<key) {
            l = m + 1;/* adjustment of left key * /
        } else {
            r = m --1;/* adjustment of right key * /
        }
    }
  if (Data [l] == key) {// If interrupted
    return l;
  } else {// If it remains to the end
    return 0;
  }
}
int FullSearch (int * Data, int N, int key) {
  int i;
  for (i = 0;i<N;i ++) {
    if (Data [i] == key) {
      break;// break if matched
    }
  }
  if (i<N) {// when the for statement is interrupted
    return i;
  } else {// if the for statement remains until the end
    return 0;
  }
}

int main (void) {
  int N, i1, i2;
  int * Data;
  int key;
  double t1, t2;
  // Read data from file
  Data = ReadDataFile ("student.txt",&N);
  // From now on, Data will be regarded as an array with N elements.
  // Enter the search key
  printf ("key:");
  scanf ("% d",&key);
  // Sort execution
  qsort (Data, N, sizeof (Data [0]), compare);
  printf ("Quick sort is done. \ N");
  // Execution of binary search method
  t1 = gettimeofday_sec ();
  i1 = BinarySearch (Data, N, key);
  t2 = gettimeofday_sec ();
  // Output of search results
  if (i1! = -1) {// if found
    printf ("(Binary) found.% D \ n", i1);
  } else {// If not found
    printf ("(Binary) not found.");
  }
  printf ("BinarySearch:% .6f (sec) \ n", t2-t1);
  // Execution of sequential search method
  t1 = gettimeofday_sec ();
  i2 = FullSearch (Data, N, key);
  t2 = gettimeofday_sec ();
  // Output of search results
  if (i2! = -1) {// if found
    printf ("(Full) found.% D \ n", i2);
  } else {// If not found
    printf ("(Full) not found.");
  }
  printf ("FullSearch:% .6f (sec) \ n", t2-t1);
  // Need to release used memory
  free (Data);
  return 0;
}
What I tried

Although the return value is an int type, I tried to return NULL;at the source of the failure, but it still resulted in an error.