-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxmin_functions.cpp
45 lines (39 loc) · 1.15 KB
/
maxmin_functions.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
#include <iostream>
using namespace std;
int FindMax(int a[],int n) //function to find largest element
{
int i, max;
max = a[0];//assume that first element is max
for(i=1;i<n;i++)
{
if(a[i]>max) //if currentelement is greater than max
max =a[i]; //assign that number as max now
}
return max; //returns the largest number to main function
}
int FindMin(int a[],int n) //function to find smallest element
{
int i, min;
min = a[0];// assuming first element as minimum
for(i=1;i<n;i++)
{
if(a[i]<min)// If current element is smaller than min
min =a[i];//assigning the smaller number to min
}
return min; //returns the smallest number to main function
}
int main()
{
int i, array[50], size, max, min;
cout<<"Input number of elements in array\n";
cin>>size;
cout<<"Enter "<< size << " integers\n";
for(i=0;i<size;i++)
cin>>array[i];
max = FindMax(array,size); //calls the max function
min = FindMin(array,size); //calls the min function
// Print maximum and minimum element
cout<<"Maximum element in the array is:" << max << "\n";
cout<<"Minimum element in the array is:" << min << "\n";
return 0;
}