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

Where Next?

Popular Backend topics Top

chasekaylee
Hi there everyone! Recently, I have fallen in love with programming with Elixir and have been having so much fun with it. I have been do...
New
New
mrmurphy
I’ve run into a situation where I’ve got a list of posts inside of a container that uses phx-update=“prepend”, and the posts on the socke...
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
JimmyCarterSon
Hello, I am working on a new application with Elixir, Dish_out. I want to see Data I follow this tutorial with Elixir Casts. However, I ...
New
osbre
Hello everyone I’m trying to implement a “magic link” or “one-time login link” functionality I wonder what a secure way to implement it...
New
sona11
In Java, if I try to do.equals() on a null string, a null pointer error is issued. I’m wondering whether I can perform the following if I...
New
Sumityadav
Hello all, I am new to learning Java Programming and want to learn java from scratch. I was writing a Java program to get the first and l...
New
Fl4m3Ph03n1x
Background I have a phoenix application in Windows 11. Unfortunately for me, I cannot compile the application because of a dependency err...
New
Fl4m3Ph03n1x
Background As I often do, I read books to learn and improve myself. I also enjoy teaching and helping others when I can, so this is somet...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
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
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New