Ferdig med 9

This commit is contained in:
Trygve 2024-02-27 18:40:25 +01:00
parent 1fcf466144
commit ac988f1962
1 changed files with 11 additions and 6 deletions

View File

@ -5,14 +5,14 @@
/* 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) {
auto second_of(const int N, const int* const x) {
assert(N >= 2);
int largest = x[0];
auto largest = x[0];
// smallest possible int value
int second_largest = std::numeric_limits<int>::min();
auto second_largest = std::numeric_limits<int>::min();
for(int i = 1; i < N; i++)
for(auto i = 1; i < N; i++)
if(x[i] > largest) {
second_largest = largest;
largest = x[i];
@ -23,8 +23,13 @@ int second_of(const int N, const int* const x) {
}
int main() {
constexpr int fixed_array_size = 5;
constexpr auto fixed_array_size = 5;
constexpr int x[fixed_array_size] = {4, 0, 6, 5, 2};
const int t = second_of(fixed_array_size, x);
const auto t = second_of(fixed_array_size, x);
std::cout << t << "\n";
}
/*
Which of these allowed implicit-type declarations would you actually use in programming practice?
I dont think I would use any of these. Auto takes longer time to write than int and makes the code less readable.
*/