99.4k views
2 votes
5.24 LAB: Two smallest numbers Write a program that reads a list of integers, 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. You can assume that the list will have at least 2 integers and less than 20 integers. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 To achieve the above, first read the integers into an array. Hint: Make sure to initialize the second smallest and smallest integers properly. 265236.1527390

2 Answers

3 votes

Answer:

integers=[]

while True:

number=int(input())

if number<0:

break

integers.append(number)

print("",min(integers))

print("",max(integers))

Step-by-step explanation:

User Ansari
by
5.2k points
1 vote

Answer:

Here is the C++ program:

#include <iostream> //to use input output function

#include <vector> //to use vectors

using namespace std; //to identify objects like cin cout

int main() { //start of main method

int size, number; //to store size of list and input numbers

cout<<"Enter list size: "; //prompts user to enter size of the list

cin >> size; //reads list size from user

vector<int> v; //creates a vector named v

for (int i = 0; i < size; ++i) { //iterates till the size of list reaches

cin >> number; //reads the input numbers

v.push_back(number); } //push number into a vector from the back

int num1 = v[0], num2 = v[1], current , temp; //num1 is set to the element at 0-th position of v and num2 is set to be on 1st index of v

if (num1 > num2) { //if num1 is is greater than num2

temp = num1; //assigns value of num1 to temp

num1 = num2; //assigns value of num2 to num1

num2 = temp; } //assigns value of temp to num2

for (int i = 2; i < size; ++i) { //iterates size times

current = v[i]; //set current to the i-th index of v

if (current < num1) { //if current is less than num1

num2 = num1; //assigns value of num1 to num2

num1 = current; } //assigns value of current to num1

else if(current < num2) { //if value of current is less than num2

num2 = current; } } //assigns value of current to num2

cout<<num1<< " "<<num2<< endl; } //dispalys num1 and num2 i.e. two smallest integers in ascending order

Step-by-step explanation:

I will explain the program with an example.

Lets say the list size is 4 and numbers are 1 2 3 4

for (int i = 0; i < size; ++i)

This loop reads each number (1,2,3,4) and adds that number to the vector v which is the sequence container. The method push_back is used which inserts each number into the vector at the end, after the current last number and the vector size is increased by 1.

Next num1 = v[0] becomes num1 = 1

and num2 = v[1] becomes num1 = 2

if (num1 > num2) this condition is false so program moves to the statement:

for (int i = 2; i < size; ++i)

current = v[i]; becomes current = v[2]; which is current = 3

if (current < num1) is false so program moves to :

else if(current < num2) this is also false.

So the program moves to the statement:

cout<<num1<< " "<<num2<< endl;

which prints these two numbers 1 and 2 on output screen as the two smallest integers in the list in ascending order. Hence the output of the program is:

1 2

The screenshot of output of the program is attached.

5.24 LAB: Two smallest numbers Write a program that reads a list of integers, and-example-1
User Rdelrossi
by
5.3k points