Question: Write a small main program that reads integers from standard input and keeps track of the SECOND SMALLEST integer, outputting that value at the end.

Write a small main program that reads integers from standard input and keeps track of the SECOND SMALLEST integer, outputting that value at the end. (Use the typical while (cin << n) loop. Example: if the numbers 5, 2, 9, 3, 4 are fed into the program, 3 would be the output. Do Not use the sort function.

I wrote the code below but it is not working!

Can you fix it and explain what i was doing wrong!

#include #include using namespace std; int main(){ vector v; int n; while(cin >> n){ v.push_back(n);

int smallest = 0; int secsmallest = 0; for(int i = 0; i < v.size(); i++){ if(smallest < v[i]) smallest = v[i]; if (secsmallest < v[i] && v[i] < smallest) secsmallest = v[i]; } cout << "The second smallest: " << secsmallest << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!