IGCSE cs 0478 2023 Jun 23 11

IGCSE cs 0478 2023 Jun 23 11

A two-dimensional (2D) array Contacts[] is used to store names and telephone numbers. All the data is stored as strings. The array must have the capacity to store 100 contacts in the form of:

  • column 1 – contact names as: last name, first name for example: Smith, John

  • column 2 – telephone numbers.

The variable CurrentSize shows how many contacts are in the array. Write a program that meets the following requirements:

  • display a menu of choices:

    • enter new contact details

    • display all the contact details

    • delete all the contact details

  • validate the menu input

  • allow up to a maximum of five new contacts to be added to the array at any one time

  • do not allow more than 100 contacts in total

  • after new contacts have been added, sort the array by contact name, as long as there are at least two contacts in the array

  • output the whole of the array

  • delete the contents of the array.

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 array Contacts[] and the variable CurrentSize

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


import string
import random

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

current_size = 100
contacts = []

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

contacts = [["", ""] for i in range(100)]

print(contacts)

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

图解

Python版答案


import string
import random

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

current_size = 100
contacts = []

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

contacts = [["", ""] for i in range(100)]

print(contacts)

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

current_size = 0
cont = True
choice = 0
new_contacts_number = 0
new_contacts_index = 0
temp_contact = []

while cont:

    # display a menu
    print("Please choose one of the following: ")
    print("Press 1 to enter new contacts ")
    print("Press 2 to display your contacts ")
    print("Press 3 to delete all contacts ")
    choice = int(input("Please input a choose: "))

    # validate the menu input
    while choice == 1 and current_size >= 100:
        print("Your contacts are full, please enter 2 or 3")
        choice = int(input("Please input a choose: "))

    while choice < 1 and choice > 3:
        print("Incorrect entry – please enter 1, 2, or 3")
        choice = int(input("Please input a choose: "))

    if choice == 1:
        new_contacts_number = int(input("How many contacts (1 to 5 only): "))
        while new_contacts_number < 1 or new_contacts_number > 5:
            print("You may only enter between 1 and 5 contacts. Please try again")
            new_contacts_number = input("How many contacts (1 to 5 only): ")

        # do not allow more than 100 contacts in total
        while current_size + new_contacts_number > 100:
            print("Not enough space in your contacts")
            print("The maximum number you may input is ", 100 - current_size)
            new_contacts_number = input("How many contacts (1 to 5 only): ")

        for new_contacts_index in range(new_contacts_number):
            name = input("Enter the contact name as last name, first name: ")
            contacts[current_size][0] = name
            contact_phone = input("Enter the telephone number: ")
            contacts[current_size][1] = contact_phone
            current_size = current_size + 1

        if current_size >= 2:
            # after new contacts have been added, sort the array by contact name
            # as long as there are at least two contacts in the array
            for i in range(current_size - 1):
                for j in range(current_size - i - 1):
                    if contacts[j][0] > contacts[j + 1][0]:
                        temp_contact = contacts[j]
                        contacts[j] = contacts[j + 1]
                        contacts[j + 1] = temp_contact
    if choice == 2:
        # display all the contact details
        if current_size > 0:
            print("Name and Telephone Number")
            for account_index in range(current_size):
                print(contacts[account_index][0], "   ", contacts[account_index][1])
    if choice == 3:
        # delete all the contact details
        for account_index in range(current_size):
            contacts[account_index] = ["", ""]
        current_size = 0

Last updated

Was this helpful?