216k views
12 votes
Write a program that reads decimal numbers from the keyboard. The program reads at least three numbers. Find and display the smallest number, the second smallest number, and the third smallest number of the set. The program ends if the largest number encountered does not change for n iterations (the largest number does not change for the next n numbers since it changed). The value of n is read from the keyboard. Value n is greater or equal to 3.

Please enter the value of n: 3
Please enter a demical number: 1.1
The set of the samllest number, the second smallest
number and the third smallest number is {1.100000, I,
/}
Please enter a demical number: 1.2
The set of the samllest number, the second smallest
number and the third smallest number is {1.100000,
1.200000, /}
Please enter a demical number: 4
The set of the samllest number, the second smallest
number and the third smallest number is {1.100000,
1.200000, 4.000000}
Please enter a demical number: 2.3
The set of the samllest number, the second smallest
number and the third smallest number is {1.100000,
1.200000, 2.300000}
Please enter a demical number: 0.4
The set of the samllest number, the second smallest
number and the third smallest number is {0.400000,
1.100000, 1.200000}
Please enter a demical number: 2.2
The set of the samllest number, the second smallest
number and the third smallest number is {0.400000,
1.100000, 1.200000} Program ended with exit code: 0
Please enter the value of n: 1
Please enter a demical number: 4
The set of the samllest number, the second smallest
number and the third smallest number is {4.000000, I,
/}
Please enter a demical number: 2
The set of the samllest number, the second smallest
number and the third smallest number is {2.000000,
4.000000, /}
Program ended with exit code: 0

1 Answer

9 votes

Answer:

Step-by-step explanation:

The following code is written in Pyton and keeps prompting the user for new decimal values, once the user is done it loops through the array getting the highest three values and outputting them to the user.

def atleast_three():

numbers = []

smallest = 0

medium = 0

largest = 0

while True:

numbers.append(float(input("Please enter a decimal value: ")))

answer = input("Add another value? Y/N").lower()

if answer != 'y':

break

else:

continue

for x in numbers:

if x > largest:

largest = x

elif (x > medium) & (x < largest):

medium = x

elif (x > smallest) & (x < medium):

smallest = x

print(str(smallest) + ", " + str(medium) + ", " + str(largest))

User Bill Yan
by
4.7k points