Ask Question

the median of an ordered set of measurments is a number seperating the lower half from the upper half. If the number of measurments is odd, the median is the middle measurment. If the number of measurments is even, the median is the average of the two middle meaurments. Write a program that requests a number n and set of n measurments as input and then displays the median of the measuremtns.

+1
Answers (1)
  1. 10 May, 10:02
    0
    The solution code is written in Python 3.

    import math n = int (input ("Enter number of measurement: ")) measurements = [] for i in range (0, n) : m = int (input ("Enter a measurement: ")) measurements. append (m) if (n % 2 = = 0) : midIndex = int (len (measurements) / 2) median = (measurements[midIndex - 1] + measurements[midIndex]) / 2 print (median) else: midIndex = math. floor (len (measurements) / 2) median = measurements[midIndex] print (median)

    Explanation:

    This program is written based on the presumption that the user will input an ordered set of numbers and therefore not sorting required.

    We can use Python input () function to prompt user for number of measurement and assign it to n variable (Line 3).

    We create a list to hold the values of measurement (Line 4).

    Next, create a for-loop by passing the n as the second argument in the range () function. This will enable the for-loop to run through n-number of times and prompts user for an input measurement per time (Line 6-7). Each input measurement is appended into the measurement list (Line 8).

    Prior to calculating the median, we need to check if the number of measurement, n, is odd or even. This can be done using the modulus operator, %.

    If n % 2 = 0, this means n is even number and therefore the median calculation should be the average of two middle measurement (Line 11 - 12). else, the median can directly be obtained from the measurement positioned in the middle index (Line 15-16).

    At last, we use print () function to print the median (Line 13 & 17).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “the median of an ordered set of measurments is a number seperating the lower half from the upper half. If the number of measurments is odd, ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers