Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges:
0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200.
Output the score ranges and the number of students. (Run your program with the following input data:
76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)

Respuesta :

Answer:

The solution code is written in Python:

  1. with open("text1.txt") as file:
  2.    data = file.read()
  3.    numbers = data.split(",")
  4.    group_0_24 = 0
  5.    group_25_49 = 0
  6.    group_50_74 = 0
  7.    group_75_99 = 0
  8.    group_100_124 = 0
  9.    group_125_149 = 0
  10.    group_150_174 = 0
  11.    group_175_200 = 0
  12.    for x in numbers:
  13.        if int(x) > 174:
  14.            group_175_200 = group_175_200 + 1
  15.        elif int(x) > 149:
  16.            group_150_174 = group_150_174 + 1
  17.        elif int(x) > 124:
  18.            group_125_149 = group_125_149 + 1
  19.        elif int(x) > 99:
  20.            group_100_124 = group_100_124 + 1  
  21.        elif int(x) > 74:
  22.            group_75_99 = group_75_99 + 1  
  23.        elif int(x) > 49:
  24.            group_50_74 = group_50_74 + 1
  25.        
  26.        elif int(x) > 24:
  27.            group_25_49 = group_25_49 + 1
  28.        
  29.        else:
  30.            group_0_24 = group_0_24 + 1
  31. print("Group 0 - 24: " + str(group_0_24))
  32. print("Group 25 - 49: " + str(group_25_49))
  33. print("Group 50 - 74: " + str(group_50_74))
  34. print("Group 75 - 99: " + str(group_75_99))
  35. print("Group 100 - 124: " + str(group_100_124))
  36. print("Group 125 - 149: " + str(group_125_149))
  37. print("Group 150 - 174: " + str(group_150_174))
  38. print("Group 175 - 200: " + str(group_175_200))

Explanation:

Firstly, open and read a text file that contains the input number (Line 1 -2).

Since each number in the text file is separated by a comma "," we can use split() method and put in "," as a separator to convert the input number string into a list of individual numbers (Line 3).

Create counter variables for each number range (Line 5 - 12).

Next create a for-loop to traverse through the number list and use if-else-if block to check the range of the current number and increment the relevant counter variable by 1 (Line 14 - 37).  

Display the count for each number range using print() function (Line 39 - 46)