Write a program that reads a list of integers into a vector, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 and 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers into a vector. Hint: Make sure to initialize the second smallest and smallest integers properly. Submit your .cpp code with this question's link. Output runs are not required.

Respuesta :

Answer:

In C++:

#include <bits/stdc++.h>

#include <iostream>

#include <vector>

using namespace std;

int main(){

vector<int> vectItems;

cout << "Vector length: ";

int ln; cin>>ln;

int num;

for (int ikk = 0; ikk < ln; ikk++){

 cin >> num;

 vectItems.push_back(num);}

int small, secsmall;

small = secsmall = INT_MAX;  

for (int ikk = 0; ikk < ln; ikk++){

 if(vectItems[ikk] < small){

     secsmall = small;  

           small = vectItems[ikk];   }

 else if (vectItems[ikk] < secsmall && vectItems[ikk] != small) {

           secsmall = vectItems[ikk];} }

 cout<<small<<" "<<secsmall;

 return 0;}

Explanation:

See attachment for program file where comments are used for explanation

Ver imagen MrRoyal