-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolns.c
More file actions
61 lines (59 loc) · 1.05 KB
/
solns.c
File metadata and controls
61 lines (59 loc) · 1.05 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
/* Enter your solutions in this file */
#include <stdio.h>
int max(int a[], int n) {
int MAX = a[0];
for(int i = 1; i < n; i++) {
if(MAX < a[i]) MAX = a[i];
}
return MAX;
}
int min(int a[], int n) {
int MIN = a[0];
for(int i = 1; i < n; i++) {
if(MIN > a[i]) MIN = a[i];
}
return MIN;
}
float average(int a[], int n) {
float avg = a[0];
for(int i = 1; i < n; i++) {
avg += a[i];
}
return avg/n;
}
int mode(int a[], int n) {
int MIN = min(a, n);
int MAX = max(a, n);
int l = MAX-MIN+1;
int count[l];
for(int i = 0; i < l; i++) {
count[i] = 0;
}
for(int i = 0; i < n; i++) {
count[a[i]-MIN]++;
}
int pos = 0, temp = count[0];
for (int i = 1; i < l; i++) {
if(count[i] > temp) {
temp = count[i];
pos = i;
}
}
return pos+MIN;
}
int factors(int n, int a[]) {
int k = 0;
while (n % 2 == 0) {
a[k] = 2; k++;
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
a[k] = i; k++;
n = n/i;
}
}
if (n > 2)
a[k] = n;
return k+1;
}