-
Notifications
You must be signed in to change notification settings - Fork 21
/
ex14d.c
57 lines (46 loc) · 1.11 KB
/
ex14d.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
/*
* 14. Rewrite the functions developed in the last four exercises to use global
* variables instead of arguments. For example, the preceding exercise should
* now sort a globally defined array.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#include <stdbool.h>
/* globals */
bool gSortOrder;
/* functions */
void sort(int [], int);
void sort(int a[], int n)
{
int i, j, temp, cond;
for (i = 0; i < n - 1; ++i)
for (j = i + 1; j < n; ++j) {
cond = (gSortOrder) ? a[i] < a[j] : a[i] > a[j];
if (cond) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int main(void)
{
int i;
int array[16] = { 34, -5, 6, 0, 12, 100, 56, 22,
44, -3, -9, 12, 17, 22, 6, 11 };
printf("\nBefore sorting:\n");
for (i = 0; i < 16; ++i)
printf(" %i", array[i]);
gSortOrder = false; /* ascending order */
printf("\n\nSorted in ascending order:\n");
for (i = 0; i < 16; ++i)
printf(" %i", array[i]);
gSortOrder = true; /* descending order */
sort(array, 16);
printf("\n\nSorted in descending order:\n");
for (i = 0; i < 16; ++i)
printf(" %i", array[i]);
printf("\n\n");
return 0;
}