-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_max.cpp
More file actions
62 lines (45 loc) · 1.14 KB
/
min_max.cpp
File metadata and controls
62 lines (45 loc) · 1.14 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
#include<iostream>
using namespace std;
int getMin(int numbers[], int n){
int min = numbers[0];
for (int i = 1 ; i < n ; i++ ){
if(min >= numbers[i])
min = numbers[i];
}
return min;
}
int getMax(int numbers[], int n){
int max = numbers[0];
for (int i = 1 ; i < n ; i++ ){
if(max <= numbers[i])
max = numbers[i];
}
return max;
}
void GetMin_Max(int numbers[], int n , int *max , int *min){
for (int i = 1 ; i < n ; i++ ){
if(*max <= numbers[i])
*max = numbers[i];
else if(*min >= numbers[i])
*min = numbers[i];
}
}
int main(){
int n;
cout << "Size of array : ";
cin >> n;
int numbers[n];
cout << "Array : " << endl;
for (int i = 0 ; i < n ; i++){
cin >> numbers[i];
}
cout << "Min is : " << getMin(numbers,n) << endl;
cout << "Max is : " << getMax(numbers,n) << endl;
//Alternnative approach
int min = numbers[0];
int max = numbers[0];
GetMin_Max(numbers,n,&max,&min) ;
cout << "Min is : " << min << endl;
cout << "Max is : " << max << endl;
return 0;
}