-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-min-avg.cpp
84 lines (71 loc) · 1.45 KB
/
max-min-avg.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Finding maximum,minimum and average of an array
SAMPLE INPUT: 4
5
6
7
8
SAMPLE OUTPUT: 8
5
6
*/
//Time complexity= O(n)
//Space complexity= O(n)
//DESCRIPTION
//Problem related to find the maximum, minimum and average from the given array.
#include<iostream>
using namespace std;
//function to find max element in an array
int max(int *arr, int k, int size)
{
int i;
for(i=1; i<size; i++)
{
if(arr[i]>k)
{
k=arr[i];
}
}
return k;
}
//function to find min element in an array
int min(int *arr, int k, int size)
{
int i;
for(i=1; i<size; i++)
{
if(arr[i]<k)
{
k=arr[i];
}
}
return k;
}
//function to find average of an array
int avg(int *arr, int k, int size)
{
int sum=0,avg;
for(int i=0; i<size; i++)
{
sum=sum+arr[i]; //finding sum of every element in an array
}
avg=sum/size;
return avg;
}
int main()
{
int size;
int k=0;
cout<<"Enter the size of an array\n";
cin>>size;
int *arr=new int[size]; //dynamic memory allocation
cout<<"Enter elements in an array\n";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}
k=arr[0];
cout<<"Maximum element is:\n"<<max(arr,k,size)<<"\n";
cout<<"Minimum element is:\n"<<min(arr,k,size)<<"\n";
cout<<"Average will be:\n"<<avg(arr,k,size)<<"\n";
return 0;
}