Category: Implementation (Computational constructs)

Higher – Reading Standard Reference Language

I would like to remind you that there is no requirement to write answers using Standard Reference Language (SRL). The SQA can sometimes refer to Standard Reference Language as “Pseudocode”, however, as I have previously explained this is not the case. Pseudocode is an informal design that explains a programs function using English like structure.

Examples

Read in a number representing a temperature in degrees Celsius and write it out as a value in degrees Fahrenheit. If the Celsius value is c, then the Fahrenheit value, f, is calculated as follows: f = ( 9 / 5 ) * c + 32.

RECEIVE c FROM (INTEGER) KEYBOARD 
DECLARE f INITIALLY ( 9.0 / 5.0 ) * c + 32 
SEND f TO DISPLAY

Read in 10 numbers and write out the average of those numbers.

DECLARE total INITIALLY 0 
DECLARE count INITIALLY 0 
WHILE count < 10 DO 
    RECEIVE nextInput FROM (INTEGER) KEYBOARD 
    SET total TO total + nextInput 
    SET count TO count + 1 
END WHILE 
SEND total / 10.0 TO DISPLAY

When reading SRL start at the top, working down each line. Take a note on scrap paper of the values given to variables. In SRL constructs start with their NAME and finish with an END and their name. Subprograms can either be a PROCEDURE or FUNCTION, these are named and have parameters (arguments) in parenthesises, functions will have one or more RETURN statements.

Tips

  • If you are asked for the results of the program then pay attention to the IF statements to make sure that you branch the program correctly. Check the number of repetitions on loops as well.
  • If you are asked to spot an error in the code it will more than likely be a logical error rather than a syntax error.

There is an online Haggis (SRL) checker here, remember you don’t need to write SRL just read it.

Higher – Computational Constructs (revision)

  • Parameter passing: used to pass data from one subprogram to another; can either be passed by value (data will not change) or by reference (changes to the data will be made)
  • Variable scope: where a variable can be used in a program; local (only inside the subprogram in which the variable has been created) or global (can be used in any subprogram)
  • Subprograms: named blocks of code which can be run from within another part of the program; often used to break a large program into smaller steps
    • Functions: a type of subprogram used to return a single value eg round, random
    • Procedures: a type of subprogram that will follow a set of given instructions
    • Methods: a function in an object-oriented language that is defined inside a class

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

National 5 – Computational Constructs (Revision)

Expressions to assign values to variables

Number=0

Expressions to return values using arithmetic operations (+ – * / ^ mod)

Average=total/5

Execution of lines of code in sequence demonstrating input – process- output

1. SEND “Enter First Number” TO DISPLAY

2. RECEIVE first_number FROM (INTEGER) KEYBOARD [input]

3. SEND “Enter Second Number” TO DISPLAY

4. RECEIVE second_number FROM (INTEGER) KEYBOARD [input]

5. SET total TO first_number + second_number [process]

6. SEND total TO DISPLAY [output]

Expressions to concatenate strings and arrays using the & operator

Bob Smith is an example of concatenation

 

firstName=”Bob”

secondName=”Smith”

print(firstName + secondName)

 

BobSmith

Use of selection constructs including simple and complex conditional statements and logical operators.

This is a simple statement as there is one conditions

1. IF pupil_mark >=50 THEN

2.    SEND pass message TO DISPLAY

3. ELSE

4. SEND fail message TO DISPLAY

5. END IF

 

This is a complex statement as there are two conditions

1. IF pupil_mark >=50 AND assessments_mark = 100 THEN

2.     SEND pass message TO DISPLAY

3. ELSE

4.      SEND fail message TO DISPLAY

5. END IF

Iteration and repetition using fixed and conditional loops

Fixed loop below as it will loop a fixed number of times depending on the users answer

number=int(input(“How many smart phones?”))

total=0.0

for phone in range(number):

                Name=input(“Please enter the name of the smart phone:”)

                cost=float(input(“Please enter the cost of the smart phone:”))

                total=total + cost

print(“Total cost = “,total)

 

Below is a Conditional Loop as it depends whether the user has any money left.

money=int(input(“Please enter your pocket money in £”))

while money>0:

    name=input(“Please enter the name of the CD you want to buy”)

    cost=float(input(“Please enter the cost of the CD”))

    money=money-cost

print(“You have ran out of pocket money”)

Pre-defined functions (with parameters) – round(22.34 ) will round numbers 22.34 becomes 22 or sqrt(4) will get the square root 2, or 9 will become 3.

FizzBuzz

So I was reading a programming blog from a few years ago that was asking, why can’t programmers.. program?. The article set a challenge to

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Now technically the answer to the puzzle uses nothing more than Nat 5 constructs, so can you code a Python solution? Share it here if you get a nice one

(hint after the jump)

Read more

Higher – SSD Functions

Still a lot of confusion over the  definition of a function and procedure in Higher Computing Science and how they differ.

A procedure is a sub program that contains a block of code. It uses parameters to pass data to and from the main program.

A function is a sub program that contains a block of code. It uses parameters to get data from the main program and passes back a single piece of data using RETURN.

In the program below you can see a procedure called calcArea and a function called funcArea. Both of these sub programs do the same thing but they way the work is different. The last two lines of code could be merged into

MsgBox(“Function Area =”  & funcArea(10,11)).

The procedure is always called on it’s own. Can you name another function shown in the program below?

 

functions

 

Python Tables

tableI was asked in class how we can create a table in Python. There are a number of ways to do this but the easiest is probably to use the .format method.

The program reads in data from a CSV file before displaying the information in a two column table. There is a lot of internal commentary you can strip out leaving just the code.

The main part of the program is line 28. Where the contents of names and scores are displayed one line at a time with padding to make them appear to be in a table.

Read more

Higher – Python – Reading from a file

Python-logo-notext.svgA lot of people find reading from a sequential file difficult. Well lets break it down, first of all what is a sequential file? A sequential file is a text based (ASCII or UNICODE) file that is made up of both printing and non printing characters. The end of the file is signalled with EoF, often programmers put data in separate lines using a new line character. Sometimes the files are in CSV format, which means that each line holds a records worth of data.

The basic premise of any Python read file access is

  1. Open file for reading
  2. read each line
  3. close file

This post will mostly be focused on different ways of doing step 2.

Read more