Last active
November 4, 2025 11:11
-
-
Save juanfal/303dd42d4919d12e1a181373c3053c31 to your computer and use it in GitHub Desktop.
sum of two greatests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 30.sumtwogreatests.cpp | |
| // juanfc 2025-11-04 | |
| // https://gist.github.com/juanfal/303dd42d4919d12e1a181373c3053c31 | |
| #include <iostream> | |
| #include <array> | |
| using namespace std; | |
| const int N = 5; | |
| typedef array<int,N> TVec; | |
| int main() | |
| { | |
| int sumTwoGreatests(TVec); | |
| cout << sumTwoGreatests((TVec){{1,2,3,4,5}}) << endl; | |
| cout << sumTwoGreatests((TVec){{1,5,3,4,5}}) << endl; | |
| return 0; | |
| } | |
| int sumTwoGreatests(TVec a) | |
| { | |
| int m1, m2; | |
| m1 = m2 = a[0]; | |
| for (int i = 1; i < N; ++i) { | |
| if (a[i] > m1) { | |
| m2 = m1; | |
| m1 = a[i]; | |
| } else if (a[i] > m2) { | |
| m2 = a[i]; | |
| } | |
| } | |
| return m1 + m2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment