-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_floats.c
67 lines (53 loc) · 1.29 KB
/
dynamic_floats.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
void sort(float*, int);
int main()
{
int max, i;
float *arr;
printf("Enter the number of elements in the float array: ");
scanf("%d", &max);
arr = (float *) malloc(max * sizeof(float));
printf("Okay... Array created with %d elements, now enter those :) \n", max);
for(i = 0; i < max; i++)
{
printf("Enter element number %d: ", i);
scanf("%f", &arr[i]);
printf("\n");
}
printf("Initially - \n");
for(i = 0; i<max; i++)
{
if(i != (max-1))
printf("%.4f, ", arr[i]);
else
printf("%.4f", arr[i]);
}
printf("\n\n");
// SORTING //
sort(arr, max);
printf("Finally - \n");
for(i = 0; i<max; i++)
{
if(i != (max-1))
printf("%.4f, ", arr[i]);
else
printf("%.4f", arr[i]);
}
return 0;
}
void sort(float* arr, int max)
{
int i, j, temp;
for(i = 0; i<max-1; i++)
{
for(j = i+1; j<max; j++)
{
if(*(arr+i) > *(arr+j))
{
temp = *(arr+i); //arr[i] is same as *(arr+i),using this as we took address of zeroth element
*(arr+i) = *(arr+j);
*(arr+j) = temp;
}
}
}
}