Category: Implementation (Data types and structures)

Higher – Random Number Array Function

The class was asked to create a function that generated 100 number dice throws and stored them in an array.

This function is used to create an array of random integers. The size of the array and the magnatude of the values is set when the function is called.


def randomNumbers(size,min,max):
    # creates an array of (size) random numbers between min and max
    import random
    numbers=[]
    for loop in range(size):
        numbers.append(random.randint(min,max))
    return numbers

rndNumbers=randomNumbers(100,1,6)

Higher – Procedures – Horse Hands

A computer program stores the names, ages and height (the height of horses is measured in ‘hands’ – for example, 16) of fifteen horses in a riding school. The user of the program will be asked to select a horse by entering a maximum age and height of the horse they wish to ride. The data for the fifteen horses will be used to provide the user with a list of suitable. A horse is suitable if its age and height are both less than or equal to the values entered by the user.

To solve the program above we first of all have to break the program into smaller steps.

  1. A computer program stores the names, ages and height (the height of horses is measured in ‘hands’ – for example, 16) of fifteen horses in a riding school.
  2. The user of the program will be asked to select a horse by entering a maximum age and height of the horse they wish to ride.
  3. The data for the fifteen horses will be used to provide the user with a list of suitable. A horse is suitable if its age and height are both less than or equal to the values entered by the user.

We then have to look at how data is passed inside the program

This allows us to create a program that uses procedures to solve the problem.

def storeHorses():
    names=["Bob","Frank","Sue"]
    ages=[4,5,6]
    heights=[12,13,14]
    return names,ages,heights

def getSearch():
    maxAge=int(input("What is the max age you want to ride > "))
    maxHeight=int(input("What is the max height you want to ride > "))
    return maxAge,maxHeight

def displayHorses(names,ages,heights,maxAge,maxHeight):
    for horse in range(3):
        if ages[horse]<=maxAge and heights[horse]<=maxHeight:
            print(names[horse],ages[horse],heights[horse])

def main():
    #set up up variables
    names=[]
    ages=[]
    heights=[]
    maxAge=0
    maxHeight=0
    
    names,ages,heights=storeHorses()
    maxAge,maxHeight=getSearch()
    displayHorses(names,ages,heights,maxAge,maxHeight)

main()

Higher – Data Types and Structures (Revision)

  • String: text (str)
  • Integer: whole numbers (int)
  • Real: decimal numbers (float)
  • Boolean: one of two possible values e.g. True/False
  • 1-D Arrays: two or more values of the same data type stored in the same construct
  • Records: two or more values of different data types stored in the same construct
  • Sequential Files: text files that can be opened, closed, read from, written to, created or deleted; identified by a path and filename which is treated as a STRING e.g. “n:\myfiles\testfile.txt” would identify the file testfile.txt in the folder n:\myfiles.

 

Thank you to C O’Toole & A Madill from Braidhurst High School for allowing me to publish this here.

National 5 – Data types and structures (Revision)

String – text variable like name
Integer – a whole number used for numberOfPeople or items
Real – contains decimals used for distances or measurements
Graphical objects – pictures
Boolean variables – True or False
One Dimensional (1D) arrays – used for a data structure with many pieces of data all of the same data type, such as a list of names. Pupilname[20]

National 5 – Boolean

A boolean variable is used to store True or False values. This can be very useful in programs, as you can use it in the program logic.

In the example below the user has to choose to exit the program, this sets the quitProg boolean variable to True which makes the result of the condition in line 3 False. Notice that True/False have capitals at the start of the value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
quitProg=False

while not quitProg:

    #Program does stuff here
    
    choice=input('Do you want to quit (Y/N):')
    if choice in 'Yy':
        quitProg=True

print('Exiting program')    

Homework for 3rd Nov 2014

  1. What is the number system used by computers? (1)
  2. State two advantages of using binary numbers rather than decimal numbers in a computer system. (2)
  3. The reserve price of a vase is £42·50. State how a real number such as 42·50 would be represented in the computer. (1)
  4. A program calculates the average rainfall for one week as 18.6mm. Describe how floating point representation is used to represent real numbers. (2)
  5. Convert the decimal number 13 into a binary number. (1)
  6. Convert the following binary numbers in to decimal (2)
    1. 00001011
    2. 10011111
  7. Convert the following decimal numbers into binary  (2)
    1. 63
    2. 193
  8. Convert the binary number 11011 into a decimal number. (1)
  9. State one reason why computer systems use binary numbers rather than decimal numbers. (1)
  10. Convert the decimal number 27 into an 8 bit binary number. (2)

Total 15