I wrote this code, but it is only giving me an address of something:
#include<iostream>
using namespace std;
void swap(int* a,int* b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void sort(int* p[],int n)
{
for(int i=0; i<n;i++)
{
if(*p[i]>*p[i+1])
swap(p[i],p[i+1]);
}
}
int main()
{
int arr[]={8,6,5,4,3,7,1};
int* p[7];
for(int i=0;i<7;i++)
{
p[i]=&arr[i];
}
sort(p,7);
/*i tried to change arr[1] to p[1] or *p[1] but same output*/
cout<<arr[1]<<arr[2];
return 0;
}
I think I'm lacking in concept somewhere. The complete question is this:
Write the following function that indirectly sorts the floats pointed to by the first n pointers in the array p by rearranging the pointers: void sort(float* p[],int n)
Please help.
int[]rather than afloat*[](without resorting tostd::sort, which you would do in real code but which would defeat the purpose of the exercise)?