Question

In: Computer Science

Write and test a Python program to print a set of real numbers in descending order....

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()

Solutions

Expert Solution

####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()


Related Solutions

Beginning Python Programming - Sorting: Write and test a Python program to print a set of...
Beginning Python Programming - Sorting: 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...
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
Write a program in Python to print all possible combinations of phone numbers. The length of...
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers...
Write a program IN PYTHON of the JUPYTER NOOTBOOK that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. This should the answer when finished Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. Example: Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending...
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending order. Use a stack to store the prime numbers.
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT