-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw9-1.cpp
64 lines (56 loc) · 1.18 KB
/
hw9-1.cpp
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int findMax(int a[], int c) {
int ans=a[0];
for(int i=0; i<c; i++) {
if (a[i]>ans) ans=a[i];
}
return ans;
}
int findMin(int a[], int c) {
int ans=a[0];
for(int i=0; i<c; i++) {
if (a[i]<ans) ans=a[i];
}
return ans;
}
double findAverage(int a[], int c) {
double ans;
int total=0;
for(int i=0; i<c; i++)
total+=a[i];
ans=(double)total/c;
return ans;
}
int findMinGap(int a[], int c) {
int gap=0;
int min=abs(a[0]-a[1]);
for (int i=0; i<c-2; i++) {
gap=abs(a[i]-a[i+1]);
if (gap<min) min=gap;
}
return min;
}
int findGapSum(int a[], int c) {
int sum=0;
for (int i=0; i<c-2; i++) {
sum+=abs(a[i]-a[i+1]);
}
return sum;
}
int main() {
int array[20];
srand (time(NULL));
for (int i=0; i<20; i++) {
array[i]=rand()%41+60;
}
cout << findMax(array, 20) << endl;
cout << findMin(array, 20) << endl;
cout << findAverage(array, 20) << endl;
cout << findMinGap(array, 20) << endl;
cout << findGapSum(array, 20) << endl;
return 0;
}