What is the primary purpose of a 1-D Array?
It allows programmers to store data in a list, provided that all the data is of the same data type.
What is an “Index” in an array and what is its starting value?
An index is used to identify each variable in the list. The index always starts at the value 0.
What are the three things you must define when creating a 1-D array?
Using a programming language of your choice write code to create an empty array which contains the name of 8 scottish cities.
Python: scottishCites = [” “]*8
The ‘scottishCities’ array is populated with city data in this order Edinburgh, Glasgow, Dundee.
Aberdeen, Inverness, Perth, Stirling, Dunfermline
What possition/index in the array are the following cities Edinburgh & Inverness
Edinburgh: 0
Inverness: 4
An array called ‘scottishCities’ stores the name of the all the cities in Scotland, Using a programming language of your choice write code to loop through the array and print the name of each city
for counter in range(len(scottishCities):
print(scottishCities[counter])
Note: print statement must be indented.
If you declare an empty array of records for 7 items, what is the index range?
0 to 6.
What is a Global Variable?
A variable accessible by every sub-program at all times, retained in RAM throughout the entire execution.
What is a Local Variable?
A variable declared within a specific sub-program, accessible only there, and existing in RAM only while that sub-program runs.
What is the difference in Scope between a global and a local variable?
Global Variable - Scope: The entire program. Local Variable - Scope: Only the sub-program where it was declared.
Why do Global Variables place a greater demand on system resources?
They are retained in memory (RAM) for the entire duration of the program’s execution.
Why are Local Variables considered more efficient for system resources?
They are only held in RAM during the execution of their specific sub-program (only use while the function or procedure is running).
What is the main danger regarding data integrity when using Global Variables?
They are susceptible to accidental change by any part of the code, which can cause errors elsewhere.
What happens when a main program “calls” a sub-program(module)?
The main program pauses to run the code contained within that specific sub-program.
What is a Procedure?
A sub-program designed to perform a series of commands. It does not primarily exist to return a single value.
What is the defining characteristic of a Function?
It is a sub-program designed to always return a single value.
What is a Pre-defined Function?
A function built into the software language (e.g., Modulus, Random) that does not need to be created by the programmer.
State 2 advantages of using pre-defined functions in your code?
Faster Development time - programmers do not need to create code from scratch.
Reliability: Predefined functions have been thoroughly tested to preform the tasks they were created for.
What does the Modulus (MOD) function calculate?
The remainder of a division operation.
What is the output of ‘7 MOD 2’?
1
What symbol is used to calculate the Modulus (remainder) of 2 numbers in Python?
%
A factory manufactures tennis balls and packs them into tubes. The machine packs the tubes with a specific number of balls, but sometimes the total batch of balls doesn’t divide perfectly, leaving some “loose” balls on the conveyor belt.
For example, if the factory made 100 balls and the tubes hold 3 balls each, the machine would fill 33 tubes, and there would be 1 ball left over.
Write a program that asks for the total number of tennis balls manufactured and the capacity of the tubes, then displays how many loose balls are left over.
#Input: Ask for the total number of items
total_balls = int(input("Enter the total number of tennis balls made: "))
#Input: Ask for the size of the container
tube_capacity = int(input("Enter how many balls fit in one tube: "))
#Calculation: Use Modulus (%) to find the remainder
leftover_balls = total_balls % tube_capacity
#Output: Display the result
print("There are "+str(leftover_balls) + "loose balls left over.") What is the purpose of the round predefined function?
To round a number to the appropriate number of decimal points
Using a programming language of your choice, write a program which:
asks the user to enter the length and breadth of a rectangle
calculates the area of the rectangle to 1 decimal place
The syntax is round(number, number_of_digits)
#length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
#Calculate the area
area = length * breadth
#Round the area to 1 decimal place using the round() function
rounded_area = round(area, 1)
#Display the result
print("The area of the rectangle is:" + str(rounded_area))This would also work :rounded_area = round((length * breadth), 1)