Buble sort with pointers
How can i use pointers to pass the array to sort function?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include<iostream.h>
void sort (int arr[]);
void main ()
{
int arr[5];
cout<<"Enter Numbers To Store In Array"<<endl;
for(int i=0; i<5; i++)
{
cin>>arr[i];
}
cout<<"Beofre Sorting"<<endl;
for(i=0; i<5; i++)
{
cout<<arr[i]<<",";
}
//sorting array
sort(arr);
cout<<"After Sorting"<<endl;
for(i=0; i<5; i++)
{
cout<<arr[i]<<",";
}
}
void sort (int arr[])
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(arr[j]>arr[j+1])
{
arr[j]=arr[j]+arr[j+1];
arr[j+1]=arr[j]-arr[j+1];
arr[j]=arr[j]-arr[j+1];
}
}
}
}
|
You... already using them.
Line 19: array type decays to pointer here.
Line 30: (int arr[])
is equvalent to (int* arr)
Topic archived. No new replies allowed.