IGCSE cs 0478 2023 Mar 22 11

IGCSE cs 0478 2023 Mar 22 11

The one-dimensional (1D) array TeamName[] contains the names of teams in a sports league. The two-dimensional (2D) array TeamPoints[] contains the points awarded for each match. The position of each team’s data in the two arrays is the same. For example, the team stored at index 10 in TeamName[] and TeamPoints[] is the same.

The variable LeagueSize contains the number of teams in the league. The variable MatchNo contains the number of matches played. All teams have played the same number of matches.

The arrays and variables have already been set up and the data stored.

Each match can be played at home or away. Points are recorded for the match results of each team with the following values:

  • 3 – away win

  • 2 – home win

  • 1 – drawn match

  • 0 – lost match.

Write a program that meets the following requirements:

  • calculates the total points for all matches played for each team

  • counts the total number of away wins, home wins, drawn matches and lost matches for each team

  • outputs for each team:

    • name

    • total points

    • total number of away wins, home wins, drawn matches and lost matches

  • finds and outputs the name of the team with the highest total points

  • finds and outputs the name of the team with the lowest total points.

You must use pseudocode or program code and add comments to explain how your code works.

You do not need to declare any arrays, variables or constants; you may assume that this has already been done.

All inputs and outputs must contain suitable messages.

You do not need to initialise the data in the arrays TeamName[] and TeamPoints[] or the variables LeagueSize and MatchNo.

Python版初始化代码,练习的时候使用如下的代码

import string
import random

#################################################
# Declare
#################################################

league_size = 20
match_no = 10
team_name = []
team_points = []

#################################################
# Initialize Data (not include)
#################################################

for team_index in range(league_size):
    team_name.append(''.join(random.choice(string.ascii_letters) for name_letter_index in range(6)))
    cur_team_points = [random.randint(0, 3) for match_index in range(match_no)]
    team_points.append(cur_team_points)

#################################################
# For you to write
#################################################

图解

Python版答案


import string
import random

#################################################
# Declare
#################################################

league_size = 20
match_no = 10
team_name = []
team_points = []

#################################################
# Initialize Data (not include)
#################################################

for team_index in range(league_size):
    team_name.append(''.join(random.choice(string.ascii_letters) for name_letter_index in range(6)))
    cur_team_points = [random.randint(0, 3) for match_index in range(match_no)]
    team_points.append(cur_team_points)

#################################################
# For you to write
#################################################

team_counter = 0
match_counter = 0

total_point = [0] * league_size

for team_counter in range(league_size):
    total_point[team_counter] = 0

for team_counter in range(league_size):
    away_win_no = 0
    home_wim_no = 0
    draw_no = 0
    lost_no = 0

    highest_result = 0
    lowest_result = 0
    top_team = 0
    bottom_team = 0

    for match_counter in range(match_no):

        total_point[team_counter] = total_point[team_counter] + team_points[team_counter][match_counter]

        # calculates the total points for all matches
        if team_points[team_counter][match_counter] == 3:
            away_win_no = away_win_no + 1
        elif team_points[team_counter][match_counter] == 2:
            home_wim_no = home_wim_no + 1
        elif team_points[team_counter][match_counter] == 1:
            draw_no = draw_no + 1
        elif team_points[team_counter][match_counter] == 0:
            lost_no = lost_no + 1

        # out put each team
        print("Team ", team_name[team_counter])
        print("Total points ", total_point[team_counter])
        print("Away wins ", away_win_no)
        print("Home wins ", home_wim_no)
        print("Draws ", draw_no)
        print("Losses ", lost_no)

        if team_counter == 1:
            highest_result = total_point[team_counter]
            lowest_result = total_point[team_counter]

        # Check for highest and lowest results
        if total_point[team_counter] > highest_result:
            highest_result = total_point[team_counter]
            top_team = team_counter

        if total_point[team_counter] < lowest_result:
            lowest_result = total_point[team_counter]
            bottom_team = team_counter

    # find the name of team with highest and lowest total points
    print("Top team ", team_name[top_team])
    print("Bottom team", team_name[bottom_team])

Last updated

Was this helpful?