-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex18
More file actions
122 lines (106 loc) · 3.27 KB
/
ex18
File metadata and controls
122 lines (106 loc) · 3.27 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//pointers to functions using a bubble sort function
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
/* our old friend die from ex17. */
void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
exit(1);
}
//a typedef creates a fake type, in this
//case for a function pointer is used as type int or char in bubble_sort and
//test_sorting later on
typedef int (*compare_cb)(int a, int b);
/* A classic bubble sort function using
compare_cb to sort
*
//count and numbers def in main at end of this doc
//cmp compares and reports first character of difference in 2 files
*create variables on stack. array 'target' goes on heap
*/
int *bubble_sort(int *numbers, int count, compare_cb cmp)
{
int temp = 0;
int i = 0;
int j = 0;
int *target = malloc(count * sizeof(int));
if(!target) die("Memory error.");
memcpy(target, numbers, count * sizeof(int));
for(i = 0; i < count; i++) { //outer loop of sort
for(j = 0; j < count - 1; j++) { //inner loop of sort
if(cmp(target[j], target[j+1]) > 0) {
//cmp pointer to compare_cb, can pas variables to it like normal function
//standard comparison sort routine
temp = target[j+1];
target[j+1] = target[j];
target[j] = temp;
}
}
}
return target;
}
//functions operating on elements a and b based on compare_cb fn type
int sorted_order(int a, int b) {
return a - b;
}
int reverse_order(int a, int b) {
return b - a;
}
int strange_order(int a, int b) {
if(a == 0 || b == 0) {
return 0;
} else {
return a % b; // modulus operator returns remainder of a/b
}
}
/* Used to test correct sorting by bubble_sort
* doing the sort and printing
*cmp completely replaces compare_cb at '*sorted' declaration
*note sorted is freed at end, targets not freed in bubble_sort def
*/
void test_sorting(int *numbers, int count, compare_cb cmp)
{
int i = 0;
int *sorted = bubble_sort(numbers, count, cmp);
if(!sorted) die("Failed to sort as requested.");
for(i = 0; i < count; i++) {
printf("%d ", sorted[i]);
}
printf("\n");
unsigned char *data = (unsigned char *)cmp;
for(i = 0; i < 25; i++) {
printf("%02x:", data[i]);
}
printf("\n");
free(sorted);
}
/*main sets array of int from input @ cmdline
*inputs takes array cmdline char, conv to int w/ atoi goes to new array
*'numbers'
*test_sorting operates on this arrar of inputs
*/
int main(int argc, char *argv[])
{
if(argc < 2) die("USAGE: ex18 4 3 1 5 6");
int count = argc - 1;
int i = 0;
char **inputs = argv + 1;
int *numbers = malloc(count * sizeof(int));
if (!numbers) die("Memory error.");
for(i = 0; i < count; i++) {
numbers[i] = atoi(inputs[i]);
}
printf("\n");
printf("Your list has been sorted:\n");
test_sorting(numbers, count, sorted_order);
test_sorting(numbers, count, reverse_order);
test_sorting(numbers, count, strange_order);
free(numbers);//note data free() from test_sorting def as well as this free()
return 0;
}