santosh kumar
santosh kumar

Reputation: 17

having error function not declared in bubble-sort program

i am having some problem whlie compiling bubble sort program , it gives me error: ‘bubblesort’ was not declared in this scope bubblesort(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;
  }
}

Upvotes: 0

Views: 419

Answers (1)

cigien
cigien

Reputation: 60228

In c++, the lexical order matters, i.e. if you use a name, then that name has to be at least declared before it is used. (It can also be defined before it is used, of course).

So you need:

void bubblesort(int a[],int n); // declare

int main()
{
  // ...
  bubblesort(a,5);   // use
}

void bubblesort(int a[],int n)  // define
{
 // ...
}

or

void bubblesort(int a[],int n)  // define
{
 // ...
}

int main()
{
  // ...
  bubblesort(a,5);   // use
}

Upvotes: 1

Related Questions