From ac988f1962f0e477e86fe50d4cad281141a27a6e Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 27 Feb 2024 18:40:25 +0100 Subject: [PATCH] Ferdig med 9 --- lab_2/9_const-array.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lab_2/9_const-array.cpp b/lab_2/9_const-array.cpp index 916f3f0..cff2df2 100644 --- a/lab_2/9_const-array.cpp +++ b/lab_2/9_const-array.cpp @@ -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::min(); + auto second_largest = std::numeric_limits::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. +*/