Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Computer Programming, XB_40011, Fall 2018
- // Vrije Universiteit, Amsterdam
- //
- // (GRADED) Assignment 3.3 "Second smallest"
- //
- // Submit this assignment to Canvas!
- // By doing so you declare to comply to the regulations as stated
- // on the page on "Ethical behaviour and fraud" in the course's
- // Canvas site.
- //
- // Student name : Nicola Vermeulen
- // Student number : 2635010
- // VUnet-id : nvn960
- //
- // Fill in your details above,
- // then enter your code below this header.
- //
- #include <iostream>
- #include <vector>
- #include <climits>
- using namespace std;
- int main() {
- vector<int> numbers(1);
- int item;
- int smallest;
- int secondSmallest;
- cout << "Enter the numbers in random order: (close by entering q)";
- while (cin >> item) {
- numbers.push_back(item);
- }
- smallest = numbers.at(0);
- secondSmallest = INT_MAX;
- for (int i=0; i < numbers.size(); i++) {
- if (numbers.at(i) < smallest) {
- secondSmallest = smallest;
- smallest = numbers.at(i);
- } else if (numbers.at(i) < secondSmallest) {
- secondSmallest = numbers.at(i);
- }
- }
- try {
- if (secondSmallest == INT_MAX || secondSmallest == smallest) {
- throw runtime_error("error: no second smallest");
- }
- cout << "The second smallest number is " << secondSmallest;
- } catch (runtime_error& excpt) {
- cout << excpt.what();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement