Qsort Float

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include<stdio.

h>
#include<stdlib.h>
#include<process.h>
#define SIZE 5
void PrintArr(float a[], int size);
int cmpasc(const void * a, const void *b );
int cmpdesc(const void * a, const void *b);
int main(void)
{
float arr[SIZE] = {1.11, 1.21, 1.10, 1.09, 1.07};
PrintArr(arr, SIZE);
qsort(arr, SIZE, sizeof(float), cmpasc);
printf("\n after asc sort :: ");
PrintArr(arr, SIZE);
qsort(arr, SIZE, sizeof(float), cmpdesc);
printf("\n after desc sort :: ");
PrintArr(arr, SIZE);
return 0;
}
int cmpasc(const void * a, const void *b)
{
float *ptr1 = (float*)a;
float *ptr2 = (float*)b;
if ((*ptr1 - *ptr2) > 0)
return 1;
else if ((*ptr1 - *ptr2) < 0)
return -1;
else
return 0;
}
int cmpdesc(const void * a, const void *b)
{
float *ptr1 = (float*)a;
float *ptr2 = (float*)b;
if ((*ptr2 - *ptr1) > 0)
return 1;
else if ((*ptr2 - *ptr1) < 0)
return -1;
else
return 0;
}
void PrintArr(float a[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("\n %f[%u]", a[i], &a[i]);
}
}

You might also like