-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem52.c
More file actions
99 lines (80 loc) Β· 2.43 KB
/
problem52.c
File metadata and controls
99 lines (80 loc) Β· 2.43 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
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int value;
int frequency;
} Element;
// Function to sort by frequency (descending), then by value (ascending)
int compare(const void *a, const void *b) {
Element *x = (Element *)a;
Element *y = (Element *)b;
if (x->frequency != y->frequency)
return y->frequency - x->frequency; // Higher frequency first
else
return x->value - y->value; // Lower number first if equal frequency
}
// Function to count how many times each number appears
int buildFrequencyMap(int *arr, int n, Element **freqArr) {
int capacity = n;
*freqArr = (Element *)malloc(capacity * sizeof(Element));
if (!*freqArr) {
printf("Oops! Couldn't allocate memory.\n");
exit(1);
}
int count = 0;
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < count; j++) {
if ((*freqArr)[j].value == arr[i]) {
(*freqArr)[j].frequency++;
found = 1;
break;
}
}
if (!found) {
(*freqArr)[count].value = arr[i];
(*freqArr)[count].frequency = 1;
count++;
}
}
return count;
}
int main() {
int n;
// Step 1: Take number of crates
printf(" How many crates are there? ");
scanf("%d", &n);
if (n <= 0) {
printf("β οΈ You need to enter at least one crate.\n");
return 1;
}
// Step 2: Allocate memory to store crate labels
int *arr = (int *)malloc(n * sizeof(int));
if (!arr) {
printf("π₯ Memory allocation failed. Please try again.\n");
return 1;
}
// Step 3: Take all crate labels from user
printf("π’ Please enter the labels of the crates:\n");
for (int i = 0; i < n; i++) {
printf("Label %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Step 4: Create frequency map
Element *freqArr;
int uniqueCount = buildFrequencyMap(arr, n, &freqArr);
// Step 5: Sort crate labels by frequency and value
qsort(freqArr, uniqueCount, sizeof(Element), compare);
// Step 6: Display the result
printf("\nβ
Sorted crate labels by frequency:\n");
for (int i = 0; i < uniqueCount; i++) {
for (int j = 0; j < freqArr[i].frequency; j++) {
printf("%d ", freqArr[i].value);
}
}
printf("\n");
// Step 7: Clean up
free(arr);
free(freqArr);
return 0;
}