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 : Mateusz Belka
- // Student number : 2637578
- // VUnet-id : mba325
- //
- // Fill in your details above,
- // then enter your code below this header.
- //
- #include<iostream>
- #include<vector>
- #include<algorithm>
- using namespace std;
- int main()
- {
- //Vars
- vector<int> list;
- int listEle;
- int listEleTmp;
- //Question
- cout << "Enter the numbers in random order: (close by entering q)" << endl;
- //FIXME: Check the order of operations in while loop.
- //FIXME: You might be able to not need the next if statement and pop_back stuff
- while (!cin.fail()) //Detects if non-int is entered
- {
- cin >> listEle;
- list.push_back(listEle);
- }
- if (cin.fail()) //Gets rid of unwanted non-int entrance to vector
- {
- cin.clear();
- list.pop_back();
- }
- //Sorting from lowest to highest
- sort(list.begin(), list.end());
- //Check if second smallest exists
- //FIXME: make sure its the best method of going about this problem
- if (list.size() > 1) //inside of if statement will fail in case of list with 0 or 1 element
- {
- listEleTmp = list.at(0);
- for (int i = 0; i < list.size() - 1; ++i)
- {
- if (list.at(i) != list.at(i + 1))
- {
- listEleTmp = list.at(i);
- }
- }
- if (list.at(list.size() - 1 != list.at(list.size() - 2)))
- {
- listEleTmp = list.at(list.size() - 1);
- }
- }
- else
- {
- cout << "error: no second smallest";
- return 0;
- }
- //Print
- if (listEleTmp == list.at(0)){
- cout << "error: no second smallest";
- }
- else
- {
- //FIXME: make a loop that checks for same same variables so no fake second smallest is created
- if (list.at(0) == list.at(1))
- {
- cout << "The second smallest number is " << list.at(2);
- }
- else
- {
- cout << "The second smallest number is " << list.at(1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement