import random # Author: James Tam # Version: November 4, 2021 # Learning objective: illustrating the proper approach to copy data from one # list to another (via a deep copy). MAX_ROWS = 2 MAX_COLUMNS = 20 PERCENTAGE_BOOST = 10 # args(reference to a list # increases each grade by the amount specified by PERCENTAGE_BOOST # return(nothing) def boostGrades(aList): r = -1 c = -1 bonus = -1 for r in range(0,MAX_ROWS,1): for c in range(0,MAX_COLUMNS,1): bonus = aList[r][c] * (PERCENTAGE_BOOST/100) aList[r][c] = aList[r][c] + bonus # args(2List) # return (float) # Function calculates and returns the average of the grades in the list def calculateAverage(aList): r = -1 c = -1 gradeAverage = -1 gradeTotal = 0 r = 0 while (r < MAX_ROWS): c = 0 while (c < MAX_COLUMNS): gradeTotal = gradeTotal + aList[r][c] c = c + 1 r = r + 1 gradeAverage = gradeTotal / (MAX_ROWS * MAX_COLUMNS) return(gradeAverage) # args(none) # Creates a 2D list using the repetition operator # return (reference to the list, integer,integer) def createList(): #Declare variables for function aList = [] tempRow = [] tempGrade = -1 r = -1 c = -1 for r in range(0,MAX_ROWS,1): tempGrade = random.randrange(0,5) tempRow = [tempGrade] * MAX_COLUMNS aList.append(tempRow) return(aList) # args(reference to a list, float # displays each element of list with a row on it's own line and with the grade average # return(nothing) def display(aList,anAverage): #Declare variables for function r = -1 c = -1 print("Average GPA=%.2f" %(anAverage)) # Each iteration of the outter loop accesses another row r = 0 while (r < MAX_ROWS): c = 0 # Each iteration of the inner loop accesses another element along that row while (c < MAX_COLUMNS): #Display each element along a row on the same line print("%.2f " %(aList[r][c]),end="") c = c + 1 print("") #After each row printed add a new line (EOL) r = r + 1 # args(reference to a list # displays each element of list with a row on it's own line # return(a new list with a copy of data in the elements of original list) def makeCopy(original): r = -1 c = -1 copy = createList() for r in range(0,MAX_ROWS,1): for c in range(0,MAX_COLUMNS,1): copy[r][c] = original[r][c] return(copy) #Starting execution point for the program def start(): #Declare variables local to start originalGrades = [] boostedGrades = [] originalAverage = -1 boostedAverage = -1 originalGrades = createList() originalAverage = calculateAverage(originalGrades) boostedGrades = makeCopy(originalGrades) boostGrades(boostedGrades) boostedAverage = calculateAverage(boostedGrades) print("Original grades and the average GPA") display(originalGrades,originalAverage) print() print("Boosted grades and the boosted average GPA") display(boostedGrades,boostedAverage) start()