forked from thantrieu/LearnC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bai4.2.c
70 lines (63 loc) · 1.27 KB
/
bai4.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
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
#include <stdio.h>
void nhap(int *arr, int *n);
void minMax(int *arr, int n, int *min, int *max);
void minMax2(int *arr, int n, int *min2, int *max2);
void ketQua(int min2, int max2);
int main() {
int n = 0;
int arr[100];
int min2, max2;
nhap(arr, &n);
minMax2(arr, n, &min2, &max2);
return 0;
}
void nhap(int *arr, int *n) {
while((*n) <= 0) {
printf("Nhap n > 0: ");
scanf("%d", n);
}
int i;
for(i = 0; i < (*n); i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
}
void minMax(int *arr, int n, int *min, int *max) {
int i;
(*min) = (*max) = arr[0];
for(i = 1; i < n; i++) {
if((*min) > arr[i]) {
(*min) = arr[i];
}
if((*max) < arr[i]) {
(*max) = arr[i];
}
}
}
void minMax2(int *arr, int n, int *min2, int *max2) {
int min, max;
minMax(arr, n, &min, &max);
*min2 = max;
*max2 = min;
int i;
for(i = 1; i < n; i++) {
if((*min2) > arr[i] && arr[i] != min) {
(*min2) = arr[i];
}
if((*max2) < arr[i] && arr[i] != max) {
(*max2) = arr[i];
}
}
if(min != (*min2) && max != (*max2) && *min2 < *max2) {
ketQua(*min2, *max2);
} else {
printf("\nKhong co gia tri thoa man");
}
}
void ketQua(int min2, int max2) {
if(min2 == max2) {
printf("\nKhong co gia tri thoa man");
} else {
printf("\nMIN2 = %d\nMAX2 = %d", min2, max2);
}
}