Ferdig med 9
This commit is contained in:
parent
1fcf466144
commit
ac988f1962
@ -5,14 +5,14 @@
|
|||||||
/* return the second-largest element of an array
|
/* return the second-largest element of an array
|
||||||
* of N integer values (must contain at least two values)
|
* 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);
|
assert(N >= 2);
|
||||||
int largest = x[0];
|
auto largest = x[0];
|
||||||
|
|
||||||
// smallest possible int value
|
// 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) {
|
if(x[i] > largest) {
|
||||||
second_largest = largest;
|
second_largest = largest;
|
||||||
largest = x[i];
|
largest = x[i];
|
||||||
@ -23,8 +23,13 @@ int second_of(const int N, const int* const x) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
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};
|
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";
|
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.
|
||||||
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user