-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTugasKelompok_3.c
130 lines (108 loc) · 3.07 KB
/
TugasKelompok_3.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
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
123
124
125
126
127
128
129
130
#include <stdio.h>
#define SIZE 10
void swap(int *a, int *b);
void show(int arr[], int length);
int linearSearch(int arr[], int N, int x);
int binarySearch(int arr[], int l, int r, int target);
void selectionSort(int arr[], int length);
float find_mean(int arr[], int N);
float find_median(int arr[], int N);
int find_mode(int arr[], int N);
int main(){
int data,i;
int array[10] = {1, 3, 6, 8, 6, 4, 3, 1, 3, 3};
int linearResult, binaryResult;
// puts("Masukkan 10 angka!");
// for(i = 0; i < SIZE; i++){
// printf("Index ke-%d: ", i+1);
// scanf("%d", &array[i]);
// }
printf("\nArray: \n");
show(array, SIZE);
selectionSort(array, SIZE);
printf("\nArray yang telah terurut menggunakan selection sort:\n");
show(array, SIZE);
printf("\nMasukkan data yang dicari: ");
scanf("%d",&data);
linearResult = linearSearch(array, 10, data);
if(linearResult == -1) printf("Elemen tidak terdapat dalam array dengan linear search\n");
else printf("Elemen berada pada indeks ke-%d dengan linear search\n", linearResult);
binaryResult = binarySearch(array, 0, 10, data);
if(binaryResult == -1) printf("Elemen tidak terdapat dalam array dengan binary search\n");
else printf("Elemen berada pada indeks ke-%d dengan binary search\n", binaryResult);
float mean = find_mean(array, SIZE);
printf("Mean: %.2f\n", mean);
float median = find_median(array, SIZE);
printf("Median: %.2f\n", median);
int mode = find_mode(array, SIZE);
printf("Modus: %d\n", mode);
return 0;
}
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void show(int arr[], int length){
int i;
for(i=0;i<length;i++){
printf("%d, ",arr[i]);
}
}
int linearSearch(int arr[], int N, int x){
int i;
for (i = 0; i < N; i++){
if (arr[i] == x) return i;
}
return -1;
}
int binarySearch(int arr[], int l, int r, int target){
int mid;
while(r >= l){
mid = l + (r - l) / 2;
if(target == arr[mid]) return mid;
else if (target < arr[mid]) r = mid-1;
else l = mid+1;
}
return -1;
}
void selectionSort(int arr[], int length){
int i, j, min_index;
for(i = 0; i < length - 1; i++){
min_index = i;
for(j = i+1; j < length; j++){
if(arr[j] < arr[min_index]){
min_index = j;
}
}
swap(&arr[min_index], &arr[i]);
}
}
float find_mean(int arr[], int N) {
int i, sum = 0;
for (i = 0; i < N; i++)
sum += arr[i];
return (float) sum / N;
}
float find_median(int arr[], int N) {
if (N % 2 != 0)
return (float) arr[N / 2];
else
return (float) (arr[(N - 1) / 2] + arr[N / 2]) / 2.0;
}
int find_mode(int arr[], int N) {
int max_count = 0, mode = -1;
int i, j, count;
for (i = 0; i < N; i++) {
count = 0;
for (j = 0; j < N; j++) {
if (arr[j] == arr[i])
count++;
}
if (count > max_count) {
max_count = count;
mode = arr[i];
}
}
return mode;
}