-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecondsmallest.cpp
More file actions
33 lines (28 loc) · 797 Bytes
/
secondsmallest.cpp
File metadata and controls
33 lines (28 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <climits>
/* TODO: write a program that reads (arbitrarily many) integers from
* stdin and outputs the *second* smallest one. NOTE: you don't need
* to store many numbers (all at once, that is) to do this! You'll
* only need a few integer variables. Think about invariants! */
int main() {
int smallest = INT_MAX;
int second = INT_MAX;
int input;
while(cin >> input) {
// Input is smaller than absolute minimum
if(input < smallest) {
second = smallest;
smallest = input;
}
// Input is smaller than second minimum
else if(input < second) {
second = input;
}
}
cout << "Minimum: " << smallest << endl;
cout << "Second: " << second << endl;
return 0;
}