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.
(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.
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
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...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT