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

First Post!

bot

bot

Corresponding tweet for this thread:

Share link for this tweet.

Popular Backend topics Top

New
conradwt
Hi, I’m building an application that will have support for both the web and mobile. At this time, I’m using PhxGenAuth for authenticatio...
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
Fl4m3Ph03n1x
Background I am moving towards defined data structures in my application, and I find that TypedStruct is quite useful. Questions Howeve...
New
sona11
I’m having a difficulty. I want to modify an attribute’s data type from String to Array. { “id”: “trn:tarb:tradingpartner:uuid:00000...
New
sona11
I studied very basic PHP (I believe). After that, I feel like I’ve gotten a handle on the language. My dream is to work as a web develope...
New
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 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 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

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
AstonJ
Or looking forward to? :nerd_face:
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
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
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in Questions

View all threads ❯