Unit 8:
Programming
Programming
Declare and use variables and constants
Understand the purpose and use of variables.
Understand the purpose and use of constants.
Understand and use the basic data types
Integer
Real
Char
String
Boolean
Understand and use input and output
Capture input from users.
Display output to users.
Understand and use fundamental programming concepts
Sequence: Execute statements in order.
Selection: Use IF and CASE statements.
Iteration: Use count-controlled loops, pre-condition loops, and post-condition loops.
Totalling and counting: Use programming constructs to total values and count occurrences.
String handling: Use string functions such as length, substring, upper, and lower.
Understand and use arithmetic, logical, and Boolean operators
Arithmetic: +, -, /, *, ^ (raised to power of), MOD, DIV
Logical: =, <, <=, >, >=, <> (not equal to)
Boolean: AND, OR, NOT
Understand and use nested statements
Nested selection
Nested iteration (up to three levels of nesting)
Understand what is meant by procedures, functions, and parameters
Define and use procedures and functions, with or without parameters.
Understand the difference between local and global variables.
Understand and use library routines
Use built-in library functions such as MOD, DIV, ROUND, RANDOM.
Understand how to create a maintainable program
Use meaningful identifiers for variables, constants, arrays, procedures, and functions.
Use appropriate commenting features provided by the programming language.
Comment relevant and appropriate syntax.
Implement procedures and functions for modular programming.
Declare and use one-dimensional (1D) and two-dimensional (2D) arrays
Understand the use of arrays.
Write values into and read values from an array using iteration.
Use variables as indexes in arrays.
Implement nested iteration with arrays.
Understand the purpose of storing data in a file to be used by a program
Open, close, and use a file for reading and writing.
Read and write single items of data.
Read and write lines of text.
As the names suggest, variables store data that can change as the program is run, while constants store data which will never change
Python has a number of different built in methods which we can use to format strings
We can also extract different characters within a string using indices
Some other ways to play with strings ...
A nested statement is one or more selection and/or iteration statements inside another selection and/or iteration statement
A nested if statement is an if-else statement with another if statement as the if body or the else body.
Some examples:
A subroutine is a named section of code that can be called by writing the subroutine's name in a program statement. Subroutines can also be referred to as procedures or functions.
A procedure is a set of instructions that will be executed when the procedure is called, whilst a function also contains a set of instructions that will be executed when the function is called and it will also return a value when it has finished executing.
Using subroutines makes the code more readable and reusable, as the code is broken into smaller sections.
Most programming languages come with a standard library of built-in subroutines to perform common functions, but also have the capability to allow the programmer to write their own custom subroutines.
As programs grow in their complexity, it becomes useful to structure the program in a way that is logical and manageable. Like many complex tasks, rather than trying to complete the tasks all in one go, you can divide the work up into smaller chunks, each of which performs a particular task. This is why we use subroutines. A subroutine is a named block of code that performs a specific task. Once a subroutine is created, the details of how it works can almost be forgotten about. In this way, the detail is abstracted, allowing the programmer to focus on the bigger picture.
Once a programmer defines a subroutine, they can then run (or call) it whenever they need it, simply by using its name.
You could compare a program with how chocolate is produced in a factory. Inside your factory, you could have lots of individual machines making chocolates from beginning to end, starting with mixing all the way to wrapping the finished product.
More likely, though, there is a series of machines that each complete a sub process which is one part of the process or task:
Preparing
Mixing
Moulding
Cooling
Wrapping
Each sub process is a specific part of the whole process carrying out a small clearly defined job. Each sub process has inputs, such as the raw ingredients into the preparation and also has outputs such as the finished wrapped chocolate from the wrapping stage. This model also means that if you want to adapt the process — change the machines in some way, or the way they work together — you can do so easily. Your factory, therefore, can grow in complexity quickly and with ease.
An array is a data structure that contains a group of linked elements which are usually of the same data type. They allow the storing of multiple pieces of data in one variable.
1D arrays are just one row of values, while 2D arrays contain a grid of values that has several rows/columns.
A 1D array example might look like this - Day 1 - 11 12 5 2 which can be shown as a variable like this - Day1= [11, 12, 5, 2]
and a 2D array may look like this - Day 1 - 11 12 5 2, Day 2 - 15 6 10, Day 3 - 10 8 12 5, Day 4 - 12 15 8 6 which can be shown as a variable like this daysData = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
An array is a data structure that holds a number of data items or elements of the same data type. When you want to store many pieces of data that are related and have the same data type, it is often better to use an array instead of many separate variables.
An array can be declared using a single identifier and can be processed efficiently using iteration techniques.
Each position in an array is identified by an index. Most languages use zero-based indexing which means that the indexing starts at 0, 0.
The contents of an array are stored contiguously (next to each other in sequence) in main memory such that the address of each position can be computed from the array's base address. This means that each position can be accessed directly, a feature which is necessary for some high performance algorithms.
The values stored in an array are mutable, which means they can be changed while the program is running, just like the contents of a variable.
What is the diference between a procedure and a function?
Explain how subroutines help to make programms managable
Identify the result of the statement: ROUND(3.142, 1)
Explain the diference betweena variable and an array
Explain the difference between a variable and a constant