harwind

harwind

Selection Sort Program In C

I received this error for a binary search programme in C, despite the fact that it requested for inputs and produced the right output. The error message I received in the output window is shown below.

binarysearch.c: In function 'sort': binarysearch.c:22:5: warning:
  implicit declaration of function 'bin'
  [-Wimplicit-function-declaration]
       bin(a,size);//invokes bin(int,int) to find element
       ^~~ binarysearch.c: At top level: binarysearch.c:24:6: warning: conflicting types for 'bin'  void bin(int a[],int size)//finds element
  using binary sort technique and return its index
       ^~~ binarysearch.c:22:5: note: previous implicit declaration of 'bin' was here
       bin(a,size);//invokes bin(int,int) to find element
       ^~~
  Enter size of array

How can I fix the problem?
What does the error indicate?
Why did it continue to ask for inputs after an error occurred?

I created two functions. As mentioned in this documentation, the first function, void sort(int a[],int size), used selection sort to sort the array and printed it before invoking the second function, void bin(int a[],int size), which used binary search to find an element and printed its position(index+1). The main function was then written.

The programme is as follows:

#include<stdio.h>
void sort(int a[],int size)//sorts array in ascending order
{
    int min;
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
           if(a[j]>a[j+1])//checks if elements at position 'i' is greater than element at position (i+1) and exchanges values if true
           {
              min=a[j];
              a[j]=a[j+1];
              a[j+1]=min;
           }
        }
    }
    printf("Sorted array:");
    for(int i=0;i<size;i++)//prints sorted array
    {
        printf("%d  ",a[i]);
    }
    bin(a,size);//invokes bin(int,int) to find element
}
void bin(int a[],int size)//finds element using binary sort technique and return its index
{
    int key;//stores element to be found
    printf("\nEnter element to be found:");
    scanf("%d",&key);
    int sp=0;//stores starting point which is initially 0
    int ep=size-1;//stores ending point which is initially one less than size of array
    int mid;//stores average of sp and ep
    int count=0;//counter
    printf("Occurrence is:");
    while(sp<=ep)
    {
        mid=(sp+ep)/2;
        if(a[mid]>key)//to run loop for elements that are before mid
        {
            ep=mid-1;
        }
        else if(a[mid]<key)//to run loop for elements that are after mid 
        {
            sp=mid+1;
        }
        else if(a[mid]==key)//when element is found
        {
            printf("%d",mid+1);
            count+=1;
            break;//to stop the loop
        }
    }
    if(count==0)//in case element is not found
    printf("ELEMENT NOT FOUND");
}
    int main()
    {
        int n;//stores size of array
        printf("Enter size of array:");
        scanf("%d",&n);
        int arr[n];//creates array of size n
        printf("Enter elements:");
        for(int i=0;i<n;i++)
        {
            scanf("%d",&arr[i]);
        }
        sort(arr,n);//invoke sort(int,int) function which later invokes bin(int,int) function
        return 0;
    }
/c

Popular Backend topics Top

andrea
Can Phoenix LiveView be used in multi-page applications, unlike React/Vue/Blazor which seems to be targeted for SPA?
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
GermaVinsmoke
Reading Programming Elixir 1.6 book, I’ve completed part 1 of the book. Now I’m thinking of reading Elixir in Action. What do you all sug...
New
sampu
I have a use case where a client is invoking a Rest endpoint via a load balancer, which in turn invokes a third party endpoint which is r...
New
Fl4m3Ph03n1x
Background I am trying to encode a structure into json format using the Jason library. However, this is not working as expected. Code L...
New
GermaVinsmoke
Does anyone know beginner friendly Elixir/Phoenix Open source projects? For learning purpose :slightly_smiling_face:
New
harwind
I received this error for a binary search programme in C, despite the fact that it requested for inputs and produced the right output. Th...
/c
New
pillaiindu
Currently reading the book “Programming Phoenix LiveView”. At the end of the Chapter 1, I’m trying to solve the guess game. If the user ...
New
Fl4m3Ph03n1x
Background I have a release file inside a tarball. However I want the final release to have some additional files and to move things aro...
New
Fl4m3Ph03n1x
Background I have an umbrella project, where I run mix test from the root. In one of the apps, I am mocking the File module using the Mo...
New

Other popular topics Top

siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New