20.9k views
3 votes
A teacher uses the following program to adjust student grades on an assignment by adding 5 points to each student's original grade. However, if adding 5 points to a student's original grade causes the grade to exceed 100 points, the student will receive the maximum possible score of 100 points. The students' original grades are stored in the list gradeList, which is indexed from 1 to n.

i ← 1 REPEAT n TIMES i ← i + 1

The teacher has the following procedures available.

min (a, b): Returns the lesser of the two values a and b max (a, b): Returns the greater of the two values a and b

Which of the following code segments can replace so that the program works as intended? Select two answers.
1) for i in range(1, n+1):
gradeList[i] = min(gradeList[i] + 5, 100)
2) for i in range(1, n+1):
gradeList[i] = max(gradeList[i] + 5, 100)
3) for i in range(1, n+1):
gradeList[i] = min(gradeList[i], 100) + 5
4) for i in range(1, n+1):
gradeList[i] = max(gradeList[i], 100) + 5

1 Answer

3 votes

Final answer:

The correct code to adjust student grades without exceeding a maximum of 100 points should use the min function as in option 1. This will ensure that if adding 5 exceeds 100, the grade is set to 100.

Step-by-step explanation:

The student is asking how to adjust student grades in a program with the condition that added points should not allow a grade to exceed 100. To solve this, we'll use two functions min and max which return the minimum and maximum values of their respective arguments.

The correct code segments that would adjust the grades according to the requirements are:

  1. for i in range(1, n+1): gradeList[i] = min(gradeList[i] + 5, 100)
  2. This code loops over the grades and adds 5 points, but it uses the min function to ensure the adjusted value does not exceed 100 points.
  3. for i in range(1, n+1): gradeList[i] = max(gradeList[i], 100) + 5
  4. This second code segment incorrectly adds 5 points to all grades even if they exceed 100, which is not according to the requirement.

The correct answer is therefore only the first code segment provided in the question which is option 1).

User Igloczek
by
7.3k points