-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex8-2.c
36 lines (28 loc) · 819 Bytes
/
ex8-2.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
#include <stdio.h>
#include <stdlib.h>
int is_bigger(const void* p1, const void* p2){
const int* ptr1 = p1;
const int* ptr2 = p2;
return *ptr1 > *ptr2 ? 1 : -1;
}
int main(int argc, char *argv[]){
printf("How many numbers would you like me to store?\n");
size_t number;
fscanf(stdin, "%lu", &number);
int* ptr = calloc(number, sizeof(int));
printf("Now give %lu numbers:\n", number);
for(int i = 0; i < number; i++){
fscanf(stdin, "%d", (ptr+i));
}
printf("Here are your original numbers:\n");
for(int i = 0; i < number; i++){
printf("address: %p | number: %d\n", (ptr+i), ptr[i]);
}
printf("Here are your sorted numbers:\n");
qsort(ptr , number, sizeof(int), is_bigger);
for(int i = 0; i < number; i++){
printf("address: %p | number: %d\n", (ptr+i), ptr[i]);
}
free(ptr);
return 0;
}