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
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
Get step-by-step solutions from verified subject matter experts
