Chapter 2: Programming

2.1 Develop Code

  • A program is an algorithm that has been converted into program code so that it can be executed by a computer. A well-written algorithm should be free of logical errors and easy to code in any high-level language.
  • Syntax error: An error that occurs when a rule of the programming language is broken.
  • When algorithms are converted into programs the computer needs to be told what type of data is stored in each variable. Every programming language has a number of built-in data types.
  • Data type: Specifies what kind of data it can hold. Common data types are integer, real, Boolean and character. The data type of value determines the operations that can be performed upon it.
  • Variable initialisation:
    • When a variable is declared, the computer allocates it a location in memory. Initially, this location is empty, so before a variable can be used it has to be given a value.
    • Once a variable has been initialised an assignment statement is used to change its value.
    • Assignment statement: The SET….TO command is used to initialise variables in pseudocode.
  • Type coercion: The data type variable gets changed during program execution. The process of converting the value stored in a variable from one data type to another.
  • Command sequence: A set of instructions that the computer executes one after another in order. Usually combined with loops ad selection statements.
  • Selection: Used to create a branch in a program. The computer selects which branch to follow based on the outcome of a condition.
  • Loops: Another name for an iteration. Used to make a computer repeat a set of instructions more than once.
  • The four basic data types are integer, float/real, Boolean and character.
  • Variable and type decelerations, command sequences, selection and iteration are four of the structural components of a program.

2.2 Making programs easy to read

  • Techniques used to make programs easy to read and understand:

Technique

Description

Comments

Used to explain what each part of the program does.

Descriptive names

Using descriptive identifiers for variables, constants and subprograms help make their purpose clear.

Indentation

Makes it easier to see where each block of code starts and finishes. Getting the indentation wrong can result in the program not running.

White space

Adding blank lines between different blocks of code makes them stand out.

  •  
  • Proving readable code makes it easier to understand what a program does and how it does it.

2.3 Strings

  • String: A sequence of characters. They can be letters, numbers, symbols, punctuation marks or spaces.
  • String indexing: Each character in a string has an index number, with the first character at position 0. You can use the index to reference individual characters in a string.
  • Length:
    • LENGTH function used to find the number of characters in a string.
    • Function: A subprogram that performs a specific task and can be used at any point in the program.
  • String traversal: Using a loop to cycle through each character in a string. Can use a FOR loop.
  • Concatenation: Involves joining two or more items of information together, Concatenating two strings produces a new string object.
  • Type conversion: Performed when a string and a non-string are joined together. The computer converts the non-string into a string before joining the two strings together.
  • Slicing: The process of extracting part of a string.
  • String formatting: Used to control the way text is displayed on the screen.

2.4 Data Structures

  • Data structure: An organised collection of related elements. Arrays and records are two common data structures used in programming.
  • One-dimensional arrays: A list of elements, each of which has a unique index value representing its position in the list.
  • Two-dimensional arrays: A matrix of rows and columns resembling a table. Two indices are used, one to reference the rows and the other the columns. All the elements of a two-dimensional array share the same data type.
  • All the elements in an array have the same data type.
  • A record consists of a collection of fields. The values stored in a record can be of different data types.

2.5 Input/Output

  • Validation: To check that the data entered by a user or from a file meets specified requirements. Any program that requires data entry should have appropriate forms of validation built-in.
  • Range check: Used to ensure that data is within a specified range.
  • Length check: Used to ensure that data has a length within a specified range.
  • Presence check: Used to ensure the user has entered some data.
  • Look-up check: Used to ensure the data matches one of the values in a predefined list.
  • Who user are required to choose from a list of options, their input should be validated to ensure that their choice is valid.
  • Large sets of data are normally stored in text files. The advantage of writing data to a file is that the data is not lost when the program is terminated. It can be read format he file whenever it is needed.

2.6 Subprograms

  • Subprogram:
    • A self-contained module of code that performs a specific task.
    • Using subprograms reduces the complexity of programs and makes them easier to understand.
    • There are two types of subprograms : functions and procedures.
    • Procedures: A subprograms contains a set of statements that are executed when the procedure is called. Unlike a function, a procedure does not return a value to the main program.
    • Functions: A subprogram that returns a value to the main program.
  • Abstraction: The process of removing or hiding unnecessary detail so that only the important points remain.
  • The two variables that store the random number generated by the function:
    • Local variable: Can be accessed only from within the subprogram in which it is created.
    • Global variable: Can be accessed from anywhere in the program, including inside subprograms.
  • Built-in functions: Functions that are provided in most high-level programming languages to perform common tasks.
  • Procedure: A subprogram that does not return a value to the main program.
  • Parameters: Values that are passed to a subprogram when it is called.

2.7 Testing and evaluation

  • Testing: This is the only way ensuring that the program functions correctly and meets all of the specified requirements.
  • Evaluating programs: Need to able to identify the strengths and weaknesses of the programs as well as those created by other programmers and identifying areas for improvement.1
  • Runtime errors: An error that occurs while the program is running - the operation the computer is asked to do is impossible to execute.
  • Logic errors occur when there is an error in the logic of the code, causing the program to produce an unexpected result.
  • Syntax error: Occurs when part of the code breaks the rules of the programming language.
  • Runtime error: Occurs while the program is running and it is asked to do something that is impossible.
  • Truth tables: Can be used to manually trace the execution of an algorithm, allowing you to track the changes in variable values.

Chapter 3: Data

3.1 Binary

  • Binary: Information represented by only two values. There are no communication errors or misunderstandings because there are no small differences. It is needed to represent data and program instruction because of how computers work.
  • A system with separate states is said to be digital and if there are two states it is binary.
  • Digital: Information represented by certain fixed values.
  • A system such as this, where there is a continuous range between two values, is said to be analogue.
  • Analogue: Using signals or information represented by a continuously variable quantity.
  • Representing information
    • There are only two digits, 1 and 0, but to represent the symbols in text there must be at least 52 separate items of information - 26 lower-case and 26 upper-case letters.
    • In a similar way, the two binary digits (bits) can be combined into groups.
    • Binary digits: The smallest unit of data that is represented in a computer. It has a single binary value, either 1 or 0.
  • Number systems
    • Binary is a number system based on two digits, 0 and 1.
    • The difference is that the denary system works in powers of 10, whereas the binary system uses powers of 2.
  • Denary system
    • Every digit has a value and the one to the left has a value 10 times higher than the one to the right.
  • Binary system
    • The binary system has a similar place value but they increase by powers of 2.
    • Byte: The basic combination of bits used to represent an item of information. A byte typically consists of 8 bits.
  • Binary arithmetic
    • Binary numbers can be manipulated in the same way as denary ones.
  • Denary addition
    • When addition is performed n denary, a ‘carry-over’ is used if the result is greater than 9.
    • Binary addition works int he same way but a ‘carry-over’ is needed if the result is greater than 1.
  • Binary addition
    • We are adding eight-bit numbers and this has caused a problem. All eight bits have been used and the 1 that was carried over in the last column has nowhere to go - it has been carried out.
    • Therefore the result of the calculation would be wrong. This is called an overflow error.
    • This condition occurs when a calculation produces a result that is greater than the computer can deal with or store.
    • The processor is informed that an error has occurred.
  • Signed and unsigned numbers
    • When an integer is indicated as being positive or negative it is described as being signed.
    • Sign and magnitude
      • In a multiple-bit binary number, the left-most it, the one with the greatest value, is called the most significant bit (MSB). We can use this to represent signed integers.
  • Two’s complement
    • The most common method used to represent signed integers in modern computers is two’s complement.
    • This method works on the principle above-the result of any number added to its negative equivalent should be zero.

Untitled

CreativeKai 03
Module by CreativeKai 03, updated more than 1 year ago
No tags specified