-
Notifications
You must be signed in to change notification settings - Fork 256
/
11 Get Set max min sum avg.c
78 lines (73 loc) · 1.49 KB
/
11 Get Set max min sum avg.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
71
72
73
74
75
76
77
78
#include<stdio.h>
struct Array
{
int A[20];
int length;
int size;
};
void Display (struct Array arr)
{
int i;
printf("Enter elements ");
for (i=0;i<arr.length;i++)
printf("%d ",arr.A[i]);
}
int Get(struct Array arr, int index )
{
if (index>=0 && index<arr.length)
return arr.A[index];
else
return -1;
}
int Set (struct Array *arr, int index, int x)
{
if (index>=0 && index<arr->length)
arr->A[index]=x;
return 0;
}
int Max (struct Array arr)
{
int max=arr.A[0];
int i;
for (i=1;i<arr.length;i++)
{
if (arr.A[i]>max)
max=arr.A[i];
}
return max;
}
int Min (struct Array arr)
{
int min=arr.A[0];
int i;
for (i=1;i<arr.length;i++)
{
if (arr.A[i]<min)
min=arr.A[i];
}
return min;
}
int Sum (struct Array arr)
{
int sum=0;
int i;
for (i=0;i<arr.length;i++)
sum+=arr.A[i];
return sum;
}
float Avg(struct Array arr)
{
return (float)Sum(arr)/arr.length;
}
int main ()
{
struct Array arr1={{1,3,5,6,77,8,9,22},20,7};
printf("The element at particular position is %d\n",Get(arr1,2)); // From This we will get the element at index 2
Set(&arr1,2,15); // This will set the index 0 element with 15
printf("Maximum in the array is : %d\n",Max(arr1)); // this will give the max value
printf("Minimum in the array is : %d\n",Min(arr1)); // This will return the min value
printf("The sum of array is : %d\n",Sum(arr1));
printf("The average value of the array is :%d\n",Avg(arr1));
Display(arr1);
return 0;
}