Author: Mr Stratton

Computing teacher and a PT at Coltness High School.

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.

Higher – Languages and Environments (Revision)

·         Low-level: language that a computer will understand; consists of 0s, 1s; examples include assembly code and machine code

·         High-level: language that a human will understand; use of English words such as repeat, if, loop, put

·         Procedural: code will processed logically and in a set order; passes data from one procedure to another using parameters

·         Declarative: consists of a knowledge base of facts and rules that can be queried

·         Object-Oriented: objects are created using classes that will contain code and data (encapsulation); data associated is known as attributes and its values are known as states; sub-classes can inherit attributes from a pre-defined class

·         Interpreter: takes each line of source code, translates it into machine code then passes it to processor to carry out that instruction; it works its way through the program one line at a time

Advantages Disadvantages
A program will run even if it is not finished No copy of machine code is saved meaning the source code has to be translated every time taking longer
Easy to spot errors during the translation The process of translating the program slows down the running of it
Program will run as soon as the first line is translated You will need to have a translator program or you cannot run it

·         Compiler: reads the source code and translates the entire program into machine code once; machine code then saved and kept and doesn’t need to be further translated before run

Advantages Disadvantages
The machine code is saved so the program only needs to be translated once You have to wait until the code is complete and the errors have been fixed before the translation can be finished and the machine code is run
The user does not need a translator program to run the machine code therefore the program runs quicker Each time the program is changed it needs to be re-translated

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

National 5 & Higher Course Content

You have a paper copy of this in your folders but I thought you might want an electronic copy.

The document is divided into

  • Yellow (SDD)
  • Orange (SDD & ISDD)
  • Red (ISDD)

For the exam National 5 pupils could be asked question from National 4 & 5. Higher pupils can be asked about content from all three courses.

TIP: Take a highlighter and mark of areas as you study them, use traffic lights to mark areas you need to come back to.

Internet 101

Quite a few students have been asking for a better explanation of how the Internet works. This sequence of videos takes you through all areas and aspects of the Internet. If you have a Khanacademy account (you do S4) then you can even earn a badge for watching the videos.

It is well worth a watch for National 4,5 & higher and it will now be making up part of our future courses.

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