-
Notifications
You must be signed in to change notification settings - Fork 0
/
005_Min_Max_Array_Function
65 lines (51 loc) · 1.38 KB
/
005_Min_Max_Array_Function
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
/* Min_Max_Array_Function Write a C PRogram to read: 1,-1,3,4,5,6,8,10,12 (9 numbers)into an array.
Then write two functions that finds Min and the other one finds Max of the array and returns Max and Min to main program.
You then print the values of Max and Min in main.*/
#include<stdio.h>
double getMax (double arr[], int size){
double max = arr[0];
int i;
for (i=0;i<size;i++){
if(arr[i]>max){
max= arr[i];
}
}
return max;
}
double getMin (double arr[], int size){
double min = arr[0];
int i;
for (i=0;i<size;i++){
if (arr[i]<min){
min=arr[i];
}
}
return min;
}
double sum(double arr[], int size){
double result =0;
int i;
for (i=0;i<size;i++){
result+= arr[i];
}
return result;
}
int main()
{
double arr[9];
int i,n;
printf("\n\n Function : Get MAX and MIN element of an array\n");
printf("\n-----------------------------------------------\n");
printf(" Input the number of elements to be stored in the array:");
scanf("%d",&n);
printf(" Input %d elements in the array:\n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",(i+1));
scanf("%lf",&arr[i]);
}
printf("\nAverage: %lf\n",sum(arr,n)/n);
printf("Max: %lf\n",getMax(arr,n));
printf("Min: %lf\n",getMin(arr,n));
return 0;
}