-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmaxSum.cpp
More file actions
41 lines (38 loc) · 747 Bytes
/
maxSum.cpp
File metadata and controls
41 lines (38 loc) · 747 Bytes
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
#include <iostream>
using namespace std;
int maximumSum(int array[],int size) {
int evenSum = 0;
int oddSum = 0;
int maxSum = 0;
for (int i = 0; i < size; i++) {
if (i % 2 == 0) {
evenSum = evenSum + array[i];
}
else {
oddSum = oddSum + array[i];
}
}
/*
if (evenSum > oddSum) {
maxSum = evenSum;
}
else {
maxSum = oddSum;
}
*/
return evenSum > oddSum ? evenSum : oddSum;
}
int main(int argc, char const *argv[]) {
int size;
int array[20];
int answer;
cout << "Enter the size of array: ";
cin >> size;
cout << "Enter the elements: ";
for (int i = 0; i < size; ++i) {
cin >> array[i];
}
answer = maximumSum(array,size);
cout << "Answer is " << answer;
return 0;
}