-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp-data-structures-array-get-set-max.cpp
93 lines (69 loc) · 1.84 KB
/
cpp-data-structures-array-get-set-max.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
85
86
87
88
89
90
91
92
93
#include <iostream>
using namespace std;
// (->) arrow operator is to access members of a structure through a pointer
struct Array
{
int A[10];
int size;
int length;
};
void Display(struct Array arr) // function to display the array's elements
{
int i;
cout << "Elements are" ;
for (i = 0; i < arr.length; i++)
cout << " " << arr.A[i];
cout << endl;
}
int Get(struct Array arr, int index) { // get an element at a given index
if (index >= 0 && index < arr.length)
return arr.A[index];
return -1;
}
int Set(struct Array *arr, int index, int x) { // set an element at a given index
if (index >= 0 && index < arr->length)
return arr->A[index] = x;
}
int Max(struct Array arr) { // find the maximum element value in the array
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) { // find the minimum element value in the array
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) { // return the sum of all elements in the array
int total = 0;
int i;
for (i = 0; i < arr.length; i++)
{
total = total + arr.A[i];
}
return total;
}
float Avg(struct Array arr) { // return the average of all elements in the array
return (float)Sum(arr)/arr.length;
}
int main() {
struct Array arr = { {2,3,4,5,6},10,5 };
Display(arr);
cout << "The element at index 1 is " << Get(arr, 1) << endl;
cout<< "The new element at index 2 is now " << Set(&arr, 2, 4) << endl;
cout<< "The min element value is " << Min(arr) << endl;
cout<< "The max element value is " << Max(arr) << endl;
cout<< "The total of all elements is " << Sum(arr) << endl;
cout<< "The average of all elements is " << Avg(arr) << endl;
return 0;
}