在气泡排序程序中未声明错误功能

having error function not declared in bubble-sort program

本文关键字:未声明 错误 功能 程序 气泡 排序      更新时间:2023-10-16

我在编译气泡排序程序时遇到一些问题,它给了我 错误:未在此范围内声明"气泡排序"(A,5(;

#include<iostream>
using namespace std;
int main()
{
int a[]={12,34,8,45,11};
int i;
bubblesort(a,5);
for(i=0;i<=4;i++)
cout<<a[i];
}
void bubblesort(int a[],int n)
{
int round,i,temp;
for(round=1;round<=n-1;round++)
for(i=0;i<=n-1-round;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}

在 c++ 中,词法顺序很重要,即如果你使用一个名称,那么在使用之前必须至少声明该名称。(当然,也可以在使用之前对其进行定义(。

所以你需要:

void bubblesort(int a[],int n); // declare
int main()
{
// ...
bubblesort(a,5);   // use
}
void bubblesort(int a[],int n)  // define
{
// ...
}

void bubblesort(int a[],int n)  // define
{
// ...
}
int main()
{
// ...
bubblesort(a,5);   // use
}