警告:函数的隐式声明 'bin';警告:'bin' 的类型冲突
warning: implicit declaration of function 'bin'; warning: conflicting types for 'bin'
我在 c 中的二进制搜索程序中遇到了这个错误,尽管它要求我提供输入并打印正确的 output.Below 是我在输出中遇到的错误 window。
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 do I resolve the error?
- What does the error mean?
- Why did it still ask for inputs when an error occurred?
我做了2个函数。 void sort(int a[],int size)
的第一个函数使用选择排序对数组进行排序并打印它并调用第二个函数,该函数下面的函数或 void bin(int a[],int size)
的第二个函数使用二进制搜索来查找元素并打印其位置(索引+1)。然后我写了main函数。
节目是:
#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;
}
在使用前先声明例程。
要么将bin
的定义移到sort
的定义之前,要么将此非定义声明放在sort
之前:
void bin(int a[], int size);
我在 c 中的二进制搜索程序中遇到了这个错误,尽管它要求我提供输入并打印正确的 output.Below 是我在输出中遇到的错误 window。
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 do I resolve the error?
- What does the error mean?
- Why did it still ask for inputs when an error occurred?
我做了2个函数。 void sort(int a[],int size)
的第一个函数使用选择排序对数组进行排序并打印它并调用第二个函数,该函数下面的函数或 void bin(int a[],int size)
的第二个函数使用二进制搜索来查找元素并打印其位置(索引+1)。然后我写了main函数。
节目是:
#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;
}
在使用前先声明例程。
要么将bin
的定义移到sort
的定义之前,要么将此非定义声明放在sort
之前:
void bin(int a[], int size);