|
| 1 | +#include<stdio.h> |
| 2 | +struct Array |
| 3 | +{ |
| 4 | + int A[20]; |
| 5 | + int length; |
| 6 | + int size; |
| 7 | +}; |
| 8 | +void Display (struct Array arr) |
| 9 | +{ |
| 10 | + int i; |
| 11 | + printf("Enter elements "); |
| 12 | + for (i=0;i<arr.length;i++) |
| 13 | + printf("%d ",arr.A[i]); |
| 14 | +} |
| 15 | +int Get(struct Array arr, int index ) |
| 16 | +{ |
| 17 | + if (index>=0 && index<arr.length) |
| 18 | + return arr.A[index]; |
| 19 | + else |
| 20 | + return -1; |
| 21 | + } |
| 22 | +int Set (struct Array *arr, int index, int x) |
| 23 | +{ |
| 24 | + if (index>=0 && index<arr->length) |
| 25 | + arr->A[index]=x; |
| 26 | + return 0; |
| 27 | +} |
| 28 | + |
| 29 | +int Max (struct Array arr) |
| 30 | +{ |
| 31 | + int max=arr.A[0]; |
| 32 | + int i; |
| 33 | + for (i=1;i<arr.length;i++) |
| 34 | + { |
| 35 | + if (arr.A[i]>max) |
| 36 | + max=arr.A[i]; |
| 37 | + } |
| 38 | + return max; |
| 39 | +} |
| 40 | +int Min (struct Array arr) |
| 41 | +{ |
| 42 | + int min=arr.A[0]; |
| 43 | + int i; |
| 44 | + for (i=1;i<arr.length;i++) |
| 45 | + { |
| 46 | + if (arr.A[i]<min) |
| 47 | + min=arr.A[i]; |
| 48 | + } |
| 49 | + return min; |
| 50 | +} |
| 51 | +int Sum (struct Array arr) |
| 52 | +{ |
| 53 | + int sum=0; |
| 54 | + int i; |
| 55 | + for (i=0;i<arr.length;i++) |
| 56 | + sum+=arr.A[i]; |
| 57 | + return sum; |
| 58 | +} |
| 59 | +float Avg(struct Array arr) |
| 60 | +{ |
| 61 | + return (float)Sum(arr)/arr.length; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +int main () |
| 66 | + |
| 67 | +{ |
| 68 | + struct Array arr1={{1,3,5,6,77,8,9,22},20,7}; |
| 69 | + printf("The element at particular position is %d\n",Get(arr1,2)); // From This we will get the element at index 2 |
| 70 | + Set(&arr1,2,15); // This will set the index 0 element with 15 |
| 71 | + printf("Maximum in the array is : %d\n",Max(arr1)); // this will give the max value |
| 72 | + printf("Minimum in the array is : %d\n",Min(arr1)); // This will return the min value |
| 73 | + printf("The sum of array is : %d\n",Sum(arr1)); |
| 74 | + printf("The average value of the array is :%d\n",Avg(arr1)); |
| 75 | + Display(arr1); |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
0 commit comments