Program throwing warning implicit declaration of function error

This is a discussion on Program throwing warning implicit declaration of function error within the C Programming forums, part of the General Programming Boards category; I am having trouble finding the cause of this error. It is a program that involves a binary search. Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Program throwing warning implicit declaration of function error

    I am having trouble finding the cause of this error. It is a program that involves a binary search.

    Code:
    #include <math.h>
    #include <stdio.h>
    
    
    /* selection sort of an array */
    #include <stdio.h>
    #define NSIZE 1000
    int get_min_range(int list[], int first, int last);
    void select_sort(int list[], int n);
    
    int
    main()
    {
       int array[NSIZE], size, num, i;
       FILE *fp;
    
     fopen("data.txt", "r");
       size = 0;
       while(fscanf(fp, "%d", &num) == 1)
           array[size++] = num;
    
       printf("\narray before sorting  = ");
       for (i = 0; i < size; ++i)
           printf("  %d",array[i]);
    
       select_sort(array, size);  // selection sort
    
       printf("\narray after sorting = ");
       for (i = 0; i < size; ++i) {
           printf("  %d",array[i]); }
    
           int input;
    
    
           printf("\nPlease enter a value (-1 = done)> ");
           scanf("%d", &input);
    
           while (input != -1) {
           int x = search(array, input, size);
                   printf("%d is located at  position in the array after %d comparisons \n", input, x);
                   printf("Please enter a value (-1 = done)> ");
                   scanf("%d", &input);
    
           }
    
    
    
       fclose(fp);
       return 0;
    }
    /* sort array list[] by selection sort */
    void
    select_sort(int list[], int n)
    {
       int cur_index, temp, index_of_min;
    
       for (cur_index = 0; cur_index < n-1; ++cur_index) {
           /* Find position of smallest element in unsorted subarray */
           index_of_min = get_min_range(list, cur_index, n-1);
           /* Exchange elements at fill and index_of_min */
           if (cur_index != index_of_min) {
               temp = list[index_of_min];
               list[index_of_min] = list[cur_index];
               list[cur_index] = temp;
           }
       }
    }
    /* Find the position of the smallest element in the subarray */
    int
    get_min_range(int list[], int first, int last)
    {
         int i, index_small, temp;
    
         temp = list[first];
         index_small = first;
         for  (i = first;  i <= last;  ++i)
             if (list[i] < temp){
                   temp = list[i];
                   index_small = i;
             }
    
         return (index_small);
    }
    
    
    int
    search(const int arr[],  //input - array to search
    int    target,           //input - value searched for
    int    n)                //input - number of elements to search
    {
        int i,
             found = 0,     //whether or not target has been found
             where;         //index where target found or NOT_FOUND
    
    i = 0;
    int max = n-1;
    int min = 0;
    int middle;
    int count = 0;
    while (!found && min < max) {
           count = count + 1;
           middle = min + (max-min)/2;
          if (arr[middle] == target)
              found = 1;
          else if (target > arr[middle])
                   min = middle +1;
           else
                   max = middle - 1;
             ++i;
    }
    
    if (found)
        where = count;
    else
        where = -1;
    
    return (where);
    
    }
    Help please, I cannot figure out where it goes wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Only 2 of your 3 functions have prototypes at the start of the file.

    search isn't one of them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Code:
     #include <math.h>
    #include <stdio.h>
    
    
    /* selection sort of an array */
    #include <stdio.h>
    #define NSIZE 1000
    int get_min_range(int list[], int first, int last);
    void select_sort(int list[], int n);
    
    int search(const int arr[],  //input - array to search
    int    target, int n)   ;       //input - value searched for
    
    int main()
    {
       int array[NSIZE], size, num, i;
       FILE *fp;
    
     fopen("data.txt", "r");
       size = 0;
       while(fscanf(fp, "%d", &num) == 1)
           array[size++] = num;
    
       printf("\narray before sorting  = ");
       for (i = 0; i < size; ++i)
           printf("  %d",array[i]);
    
       select_sort(array, size);  // selection sort
    
       printf("\narray after sorting = ");
       for (i = 0; i < size; ++i) {
           printf("  %d",array[i]); }
    
           int input;
    
    
           printf("\nPlease enter a value (-1 = done)> ");
           scanf("%d", &input);
    
           while (input != -1) {
           int x = search(array, input, size);
                   printf("%d is located at  position in the array after %d comparisons \n", input, x);
                   printf("Please enter a value (-1 = done)> ");
                   scanf("%d", &input);
    
           }
    
    
    
       fclose(fp);
       return 0;
    }
    /* sort array list[] by selection sort */
    void
    select_sort(int list[], int n)
    {
       int cur_index, temp, index_of_min;
    
       for (cur_index = 0; cur_index < n-1; ++cur_index) {
           /* Find position of smallest element in unsorted subarray */
           index_of_min = get_min_range(list, cur_index, n-1);
           /* Exchange elements at fill and index_of_min */
           if (cur_index != index_of_min) {
               temp = list[index_of_min];
               list[index_of_min] = list[cur_index];
               list[cur_index] = temp;
           }
       }
    }
    /* Find the position of the smallest element in the subarray */
    int
    get_min_range(int list[], int first, int last)
    {
         int i, index_small, temp;
    
         temp = list[first];
         index_small = first;
         for  (i = first;  i <= last;  ++i)
             if (list[i] < temp){
                   temp = list[i];
                   index_small = i;
             }
    
         return (index_small);
    }
    
    
    int
    search(const int arr[],  //input - array to search
    int    target,           //input - value searched for
    int    n)                //input - number of elements to search
    {
        int i,
             found = 0,     //whether or not target has been found
             where;         //index where target found or NOT_FOUND
    
    i = 0;
    int max = n-1;
    int min = 0;
    int middle;
    int count = 0;
    while (!found && min < max) {
           count = count + 1;
           middle = min + (max-min)/2;
          if (arr[middle] == target)
              found = 1;
          else if (target > arr[middle])
                   min = middle +1;
           else
                   max = middle - 1;
             ++i;
    }
    
    if (found)
        where = count;
    else
        where = -1;
    
    return (where);
    
    }
    now its saying warning fp is used uninitialized in this function on line 21. Does this mean that my file is missing from my directory?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, it means you aren't using fp for anything:
    Code:
       FILE *fp;
    
     fopen("data.txt", "r"); /* this should start with: fp = */

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21