Init 9
This commit is contained in:
parent
f2eb3a6ddc
commit
85597c99a5
30
lab_2/9_const-array.cpp
Normal file
30
lab_2/9_const-array.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
/* return the second-largest element of an array
|
||||||
|
* of N integer values (must contain at least two values)
|
||||||
|
*/
|
||||||
|
int second_of(const int N, const int* const x) {
|
||||||
|
assert(N >= 2);
|
||||||
|
int largest = x[0];
|
||||||
|
|
||||||
|
// smallest possible int value
|
||||||
|
int second_largest = std::numeric_limits<int>::min();
|
||||||
|
|
||||||
|
for(int i = 1; i < N; i++)
|
||||||
|
if(x[i] > largest) {
|
||||||
|
second_largest = largest;
|
||||||
|
largest = x[i];
|
||||||
|
}
|
||||||
|
else if(x[i] > second_largest)
|
||||||
|
second_largest = x[i];
|
||||||
|
return second_largest;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
constexpr int fixed_array_size = 5;
|
||||||
|
constexpr int x[fixed_array_size] = {4, 0, 6, 5, 2};
|
||||||
|
const int t = second_of(fixed_array_size, x);
|
||||||
|
std::cout << t << "\n";
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user