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

New
pillaiindu
Cross posting from HashNode. A friend of mine is creating Uber-like app for a small company with 200 to 1000 cars. The app will operate ...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
jaimeiniesta
I maintain a project that lists hundreds of thousands of web pages, and I’d like to show a screenshot for each web page. There are alread...
New
Fl4m3Ph03n1x
Background I am now trying Gradual type checking, as a consequence I am giving a shot to Gradient. As I see it, this is an alternative to...
New
Fl4m3Ph03n1x
Background I am trying out polymorphic typing with dialyzer. As an example I am using the famous Option type (aka, Maybe Monad) that is n...
New
GermaVinsmoke
Does anyone know beginner friendly Elixir/Phoenix Open source projects? For learning purpose :slightly_smiling_face:
New
Fl4m3Ph03n1x
Background I am a fan of dialyzer and friends (looking at Gradient) and I try to have sepcs in my code as much as I can. To this end, I a...
New
Fl4m3Ph03n1x
Background I have a module that uses TypedStruct to create structs. This is the code: defmodule Shared.Data.Authorization do @moduledo...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
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
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
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
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