• API
• FAQ
• Tools
• Archive
daily pastebin goal
32%
SHARE
TWEET

# Untitled

a guest Sep 20th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // Computer Programming, XB_40011, Fall 2018
2. // Vrije Universiteit, Amsterdam
3. //
4. // (GRADED) Assignment 3.3 "Second smallest"
5. //
6. // Submit this assignment to Canvas!
7. // By doing so you declare to comply to the regulations as stated
8. // on the page on "Ethical behaviour and fraud" in the course's
9. // Canvas site.
10. //
11. // Student name   : Mateusz Belka
12. // Student number : 2637578
13. // VUnet-id       : mba325
14. //
15. // Fill in your details above,
17. //
18. #include<iostream>
19. #include<vector>
20. #include<algorithm>
21.
22. using namespace std;
23.
24. int main()
25. {
26.   //Vars
27.   vector<int> list;
28.   int listEle;
29.   int listEleTmp;
30.
31.   //Question
32.   cout << "Enter the numbers in random order: (close by entering q)" << endl;
33.   //FIXME: Check the order of operations in while loop.
34.   //FIXME: You might be able to not need the next if statement and pop_back stuff
35.   while (!cin.fail()) //Detects if non-int is entered
36.   {
37.     cin >> listEle;
38.     list.push_back(listEle);
39.   }
40.   if (cin.fail()) //Gets rid of unwanted non-int entrance to vector
41.   {
42.     cin.clear();
43.     list.pop_back();
44.   }
45.
46.   //Sorting from lowest to highest
47.   sort(list.begin(), list.end());
48.
49.   //Check if second smallest exists
51.   if (list.size() > 1) //inside of if statement will fail in case of list with 0 or 1 element
52.   {
53.     listEleTmp = list.at(0);
54.     for (int i = 0; i < list.size() - 1; ++i)
55.     {
56.       if (list.at(i) != list.at(i + 1))
57.       {
58.         listEleTmp = list.at(i);
59.       }
60.     }
61.     if (list.at(list.size() - 1 != list.at(list.size() - 2)))
62.     {
63.       listEleTmp = list.at(list.size() - 1);
64.     }
65.   }
66.   else
67.   {
68.     cout << "error: no second smallest";
69.     return 0;
70.   }
71.
72.   //Print
73.   if (listEleTmp == list.at(0)){
74.     cout << "error: no second smallest";
75.   }
76.   else
77.   {
78.     //FIXME: make a loop that checks for same same variables so no fake second smallest is created
79.     if (list.at(0) == list.at(1))
80.     {
81.       cout << "The second smallest number is " << list.at(2);
82.     }
83.     else
84.     {
85.       cout << "The second smallest number is " << list.at(1);
86.     }
87.   }
88.   return 0;
89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top