In: Computer Science
Write and test a Python program to print a set of real numbers in descending order. The program should also print out the median of the numbers input and how many numbers were input. The program should read in numbers until a negative number is read. The negative number serves as a sentinel or marker telling the program when to stop reading numbers. The program should then sort the numbers and print them out in order from largest to smallest. Note, the sentinel number is not to be considered as one of the numbers, it should not be printed. The program should be able to handle up to 100 input numbers. If the user attempts to enter more than 100 numbers, the program should print an error message and quit.
Also do not use list methods like list.append etc. or import any libraries to solve this problem.
Here is the template to work off of:
# get the numbers and input into the list def get_numbers(nums): # stop when -99.0 is entered i = 0; while i < 10: nums[i] = input("Enter a number: ") if nums[i] == -99.0: break i = i + 1 return i # returning the total number of numbers entered # output all num numbers def print_numbers(nums, num): # output each number one line at a time for i in range(num): print nums[i] # output the numbers joining each with a space between on one line print (" ".join(str(x) for x in nums[0:num])) # output the list print nums ##################### # the "boss" function ##################### def main(): num_numbers = 0 # initialize num_numbers to 0 a = [0.0] * 10 # initialize a list of 10 floating point values to 0.0 num_numbers = get_numbers(a) print_numbers(a, num_numbers) # if python program was executed from this source code if __name__ == "__main__": main()
####indentations(tabs) may get disturbed...please refer pic
# get the numbers and input into the list
def get_numbers(nums):
# stop when -ve is entered
i = 0;
while i < 100:## only hundred numbers
nums[i] = float(input("Enter a number: "))
if nums[i]<0:
break
i = i + 1
return i # returning the total number of numbers entered
# output all num numbers
def print_numbers(nums, num):
###sort
for i in range(num):
max_idx = i
for j in range(i+1, num):
if nums[max_idx] < nums[j]:
max_idx = j
# Swap the found maximum element with
# the first element
nums[i], nums[max_idx] = nums[max_idx], nums[i]
# output each number one line at a time
for i in range(num):
print(nums[i])
# output the numbers joining each with a space between on one line
print(" ".join(str(x) for x in nums[0:num]))
# output the list
print(nums[:num])
#####################
# the "boss" function
#####################
def main():
num_numbers = 0 # initialize num_numbers to 0
a = [0.0] * 10 # initialize a list of 10 floating point values to 0.0
num_numbers = get_numbers(a)
print_numbers(a, num_numbers)
if(num_numbers%2==1):
print("Median=",a[num_numbers//2])
else:
print("Median=",(a[(num_numbers//2)-1]+a[(num_numbers//2)])/2)
# if python program was executed from this source code
if __name__ == "__main__":
main()