Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Aqa a level computer science

Rating
-
Sold
-
Pages
447
Uploaded on
14-06-2023
Written in
2022/2023

Programming basics INTRODUCTION In its simplest form a computer program can be seen as a list of instructions that a computer has to work through in a logical sequence in order to carry out a specific task. The instructions that make up a program are all stored in memory on the computer along with the data that is needed to make the program work. Programs (also known as applications or apps) are created by writing lines of code to carry out algorithms. An algorithm is the steps required to perform a particular task and the programming code contains the actual instructions written in a programming language. This language is in itself an application that has been written by someone else to enable you to write your own programs. In the same way that there are lots of different languages you can learn to speak, there are also lots of programming languages, and in the same way that some languages have many different dialects, there are also different versions of some of the more popular programming languages. Another similarity with natural languages is that each programming language has its own vocabulary and rules that define how the words must be put together. These rules are known as the syntax of the language. The difference between learning a foreign (natural) language and a computer language is that there are far fewer words to learn in a computer language but the rules are much more rigid. LEARNING OBJECTIVES In this chapter you will learn: • the basic principles of writing instructions in the form of programming code • what constants and variables are and how to use them • what the main data types are • how to store data using meaningful names. SPECIFICATION COVERAGE 3.1.1.1 Data types 3.1.1.2 Programming concepts 3.1.1.6 Constants and variables in a programming language 3.5.1.2 Integers KEYWORDS Memory: the location where instructions and data are stored on the computer. Algorithm: a sequence of steps that can be followed to complete a task and that always terminates. Syntax: the rules of how words are used within a given language. 3CONSTANTS AND VARIABLES ● Naming and storing data In addition to instructions, the computer program also needs data to work with. For example, to add two numbers together requires an add instruction and then the two numbers that need to be added. You need to give these two data items names so that the computer will know which data to use. The data are stored in memory along with the instructions. You could view memory rather like a series of pigeon-holes, each having a unique address, known as a memory address. It is a really good idea to use names that indicate the purpose of the data – in the case of the example above the two numbers might be called Number1 and Number2. Using meaningful names will help you when they are trying to trace bugs and it also allows other programmers to follow the code more easily. It is good practice to adopt a common naming convention. In this case the first character in upper case and the rest in lower case. This process of giving data values is called ‘assigning’, and it looks something like these two: Number1 23 Name "Derek" The means ‘becomes’ or ‘equals’. Number1 is an example of a variable. In the example above it has been given a value of 23, though this value will change while the program is being run. Name is another example of a variable and has been given the value "Derek". Different programming languages have slightly different ways of assigning values. For example, you may need to use the equals sign to make the assignment in the code you are writing. So a simple algorithm to add two numbers together might look like this: Number1 = 2 Number2 = 3 Answer = Number1 + Number2 Figure 1.1 shows a simplified visualisation of how this program is handled. There will be millions of memory addresses, of which just three are shown in this diagram. Memory address Number1Variable 2Data 3 5 Number2 Answer Figure 1.1 ● Constants and variables Data are stored either as constants or as variables. Constants (as you’d expect from the name) have values that are fixed for the duration of a program. For example, if you were writing a program that converted miles into kilometres you could set the conversion rate as a constant because it will never change. In this case we could call the constant ConvertMilestoKm and assign it a value of 1.6 as there are approximately 1.6 km to the mile. Then whenever we want to convert a distance in miles to its metric equivalent we would multiply it by the constant ConvertMilestoKm. KEYWORD Assignment: the process of giving a value to a variable or constant. KEYWORD Memory address: a specific location in memory where instructions or data are stored. KEYWORDS Constant: an item of data whose value does not change. Variable: an item of data whose value could change while the program is being run. 1 Programming basics 4 Notice that the name given to the constant is self-explanatory. We could have just called it Constant1. However, by giving it a meaningful name it makes the code easier to work with as the program gets bigger. It also makes it easier for anyone else that looks at the code, to work out what the program is doing. This is important for three main reasons: ● It makes it easier to find and correct errors/bugs in code. This is called debugging. ● There are many occasions where there are several programmers working on the same program at the same time, so having a sensible naming convention makes it easier for everyone to understand. ● It will be easier to update the code later on when further versions of the program are created. The value of variables can change as a program is being run. For example, the same conversion program will require the user to type in the number of miles they want to convert. This number will probably be different each time the user enters data. Therefore, you need to have a variable that you could call NumberOfMiles. There are lots of other examples – the number of answers a pupil has got right in a test would (hopefully) increase as they work their way through a test so the data would have to be stored as a variable. The password a user uses to access a network can be changed at any time, so it would also be classed as a variable. Variable and constant declaration Declaring a constant or variable means that when you are writing code you describe (or declare) the variables and constants that you are going to use before you actually use them in your program. Some programming languages force you to declare the variables and constants you intend to use in your program before you start writing any code. The benefits of doing this are that it forces you to plan first and the computer will quickly identify variables it does not recognise. There are two parts to a declaration. You need to supply a suitable name for the constant/variable and you need to specify the data type that will be used. The declarations might look something like this: Dimension Age As Integer Dimension Name As String Dimension WearsGlasses As Boolean Dimension or Dim is one of the command words used in Visual Basic to indicate that a variable is being declared. Once you have declared a variable it starts with a default value. In the above examples Age will start as zero, Name as nothing (also known as the empty string) and WearsGlasses will start with the value False. Other languages may use different default values so it is good practice to assign an initial value to the variable just to make sure it is correct. ● Data types It is important to consider how you want your program to handle data. For example, to create the miles to kilometres conversion program, you have to tell the program that miles and kilometres both need to be stored as numbers. KEYWORD Debug: the process of finding and correcting errors in programs. KEYWORD Declaration: the process of defining variables and constants in terms of their name and data type. DATA TYPES 5 There are lots of data types you might need to use and you need to think carefully about the best type to use. For example, if storing numbers, how accurate do you need the number to be? Will a whole number be accurate enough or will you need decimals? In addition to numbers, you will probably want to store other data such as a person’s name, their date of birth or their gender. All programming languages offer a range of data types but the actual name of the data type may vary from language to language. Here are some of the most common data types: ● Integer: This is the mathematical name for any positive or negative whole number. This might be used to store the number of cars sold in a particular month or the number of pupils in a class. The range of numbers that can be stored depends on how much memory is allocated. For example, an integer in Visual Basic can store numbers between –2 147 483 648 through to . Declaring a number as an integer means that the program will then handle the data accordingly. For example, 2 + 3 will equal 5. In some languages, if you did not set it to integer 2 + 3 would equal 23 (two three). ● Real/Float: This is a number that has a fractional or decimal part, for example 3.5 or 3 1_ 2 . In our miles to kilometres conversion program, you would need to store both miles and kilometres using this data type as the user might want to convert a number that is not a whole number. Other examples might include a person’s height in metres or their weight in kilograms. ● Text/String: This data type is used to store characters, which could be text or numbers. For example, you could use this to store a person’s name or address. Some programming languages refer to this data type as alphanumeric because you can actually store any character you want in a string whilst text implies it can only store letters. Text or string variables are normally shown in quotation marks. For example you might assign the name Frank to a variable like this: Name "Frank". House numbers and phone numbers are often stored as text / string as although they are numbers, you would never need to carry out any calculations on them and in the case of telephone numbers the leading zero is important and would be omitted if stored as a number. ● Boolean: The simplest data type is a simple yes/no or true/false. This is called a Boolean data type. It is named after George Boole who discovered the principles behind logic statements. Boolean data types can be used to store any kind of data where there are two possible values. ● Character: This data type allows you to store an individual character, which might be a letter, number or symbol. All computers have a defined character set, which is the range of characters that it understands. This would commonly be all the upper and lower case letters, plus other keyboard characters and any special characters. ● Date/Time: This will store data in a format that is easily identifiable as a date or time, e.g. 30.04.2014 or 12:30. The program will then handle the data accordingly. For example, if you added 5 to the date, it would tell you the date in five days’ time. 30.05.2014 + 5 would become 04.06.2015. If you did not declare it as a date you may get the wrong answer, for example 30.05.2019. ● Pointer/Reference: This data type is used to store a value that will point to or reference a location in the memory of the computer. If you think of memory KEYWORD Data type: determines what sort of data are being stored and how it will be handled by the program. KEYWORD Integer: any whole positive or negative number including zero. 1 Programming basics 6 as a series of pigeon-holes or addresses where instructions and data are stored, the pointer/reference is used in a program to go to a specific address. For example, you could set up a pointer called Pointer1 and put address 1001 in it. The program would then go to memory address 1001 and take the data from it. In the example below it would be the data assigned to Number2. Other lines of code will then be needed to tell the program what to do with the data it finds there. Figure 1.2 shows how a pointer is used to reference an item of data. Memory address Number1 Pointer1 = 1001 Number2 1002 Add Answer Figure 1.2 ● Array: An array is a collection of data items of the same type. For example, if you wanted to store a collection of names in a school register, you could call this Register and each item of data would be stored as text. Each individual name in the array is called an element. Every element is numbered so that Register(2) would be the second person in the array, Register(4) the fourth person and so on. Note that 0 is often used as the first element of an array, rather than 1. If this was the case then Register(2) would actually be the third person in the array, Register(4) the fifth and so on. Figure 1.3 shows a simple array with six elements. Register(2) = Hussain, Register(4) = Schmidt (assuming array indexing starts at 1). ● Records: This is used to store a collection of related data items, where the items all have different data types. For example, you might set up a record called Book, which is used to store the title, author name and ISBN of a book. Title and Author are text whereas the PublicationDate is set as a Date data type. You could write it like this: Book = Record Title, Author As Text * 50 ISBN As Text * 13 PublicationDate As Date When the program is run, every time data are entered for the book, the user will type in up to 50 characters of text for the title and author and then the ISBN. A variable could now be set up using this record data type and this variable would contain all of this data. ● Built-in and user-defined data types Built-in data types are those that are provided with the programming language that you are using. The list of built-in types varies from language to language, but all will include versions of the types listed above. Most programming languages allow users to make up their own data types, usually by combining existing data types together. These are simply called user-defined data types. For example, if you were making a program to store user names and IDs, you may create a user-defined data type called Logon made up of a set number of characters and numbers. Figure 1.3 Brown Hussain Koening Schmidt Torvill West KEYWORD Pointer: a data item that identifies a particular element in a data structure – normally the front or rear. KEYWORDS Array: a set of related data items stored under a single identifier. Can work on one or more dimensions. Element: an single value within a set or list – also called a member. Record: one line of a text file. BUILT-IN AND USER-DEFINED DATA TYPES 7 Type Logon UserName As String * 10 UserID As Integer * 5 End Type This code will set a new data type called Logon, which will be made up of a 10-character user name followed by a 5-digit user ID. In total, the data type will have 15 characters/digits to store the data. The reasons for creating user-defined types are mainly to do with efficiency. As you start to write your own programs you will find that they can get very long and complex and that debugging can be very time-consuming. Most programmers try to make their code as organised and efficient as possible as this will save them time as the program develops. For example, it is easier to reuse a block of code rather than have to write it all over again. Most programmers aim to create code that is ‘elegant’. This means that it does exactly what it is supposed to do as efficiently as possible. Often this means writing as few lines of code as possible with no repeated coding. Using a user-defined data type is just one example of where it is possible to be more efficient. With our example of storing Logon information using a user-defined variable, because all the data are stored in one variable rather than two, when the program needs this information, we only need to access one variable rather than two. Practice questions can be found at the end of the section on pages 46 and 47. STUDY / RESEARCH TASKS 1 A list of data is also known as a one-dimensional array. Find out what two- and three-dimensional arrays are and give examples of where you might use each. 2 Identify the built-in data types for the main programming language that is used in your school or college. 3 Research data types that are specifically used to store sound and video data. How do they differ from other data types? TASKS 1 Give two reasons why it is a good idea to use meaningful variable names. 2 Use examples to explain the difference between a constant and a variable. 3 Why is it important to declare all variables and constants at the beginning of a program? 4 Explain the difference between a value and a variable. 5 Suggest suitable data types and variable names for: a) the current rate of VAT b) today’s date c) the total takings from a shop d) a person’s date of birth e) which wrist a person wears a watch on. KEY POINTS • Programming languages are used to write applications (apps). • An algorithm is a sequence of instructions that can be followed to complete a task. Algorithms always terminate. • Programming code is made up of algorithms that are implemented within a programming language. • Instructions are stored in memory along with the data required by the program. • Data are stored and named according to certain conventions. • Variables and constants are used to store data and must be declared in some languages. • There are several data types built in to every programming language and the programmer can also define their own. 8 ● Sequencing Sequencing instructions correctly is critical when programming. In simple terms this means making sure that each line of code is executed in the right order. For example, a DVD recorder may have a simple program to record a TV channel at a certain time. The sequence of events would be: Set time to record = 15:00 Set channel to record = Channel 4 Check time If time = 15:00 Then Record If any of these instructions were wrong, missed out or executed in the wrong order, then the program would not work correctly. 2 Programming concepts

Show more Read less
Institution
Course

Content preview

, AQA A-level




Computer
Science
Includes AS and A-level
Bob Reeves

Approval message from AQA
This textbook has been approved by AQA for use with our qualification. This means that we
have checked that it broadly covers the specification and we are satisfied with the overall
quality. Full details of our approval process can be found on our website.
We approve textbooks because we know how important it is for teachers and students to
have the right resources to support their teaching and learning. However, the publisher is
ultimately responsible for the editorial control and quality of this book.
Please note that when teaching the AQA A-level Computer Science course, you must
refer to AQA’s specification as your definitive source of information. While this book has
been written to match the specification, it cannot provide complete coverage of every
aspect of the course.
A wide range of other useful resources can be found on the relevant subject pages of our
website: www.aqa.org.uk

,The Publishers would like to thank the following for permission to reproduce copyright material:
P.11 © chombosan - Fotolia.com; P.24 © VvoeVale - iStock via Thinkstock.com; P.69 Courtesy of Wikipedia,
The Opte Project (http://creativecommons.org/licenses/by/2.5/); P.111 © Hodder & Stoughton; P.136 middle
© Sergey Kamshylin - Fotolia.com, bottom © mark huls - Fotolia.com; P.137 © Jenny Thompson - Fotolia.com;
P.142 screenshot from TRANSYT from TRL Software (trlsoftware.co.uk); P.214 © ra3rn - Fotolia.com;
P.217 © davemhuntphoto - Fotolia.com; P.218 © Bob Reeves; P.231 top © TheVectorminator - Fotolia.com,
bottom © R+R - Fotolia.com; P.267 top © Maksym Yemelyanov - Fotolia.com, bottom © finallast -Fotolia.com;
P.271 © KarSol - Fotolia.com; P.289 © Igor Mojzes - Fotolia.com; P.295 Courtesy of Wikimedia Commons, author
Ordercrazy, Creative Commons CC 1.0 (http://creativecommons.org/publicdomain/zero/1.0/deed.en);
P.313 © Maxim Pavlov - Fotolia.com
Every effort has been made to trace all copyright holders, but if any have been inadvertently overlooked the
Publishers will be pleased to make the necessary arrangements at the first opportunity.
Although every effort has been made to ensure that website addresses are correct at time of going to press, Hodder
Education cannot be held responsible for the content of any website mentioned. It is sometimes possible to find a
relocated web page by typing in the address of the home page for a website in the URL window of your browser.
Hachette UK’s policy is to use papers that are natural, renewable and recyclable products and made from
wood grown in sustainable forests. The logging and manufacturing processes are expected to conform to the
environmental regulations of the country of origin.
Orders: please contact Bookpoint Ltd, 130 Milton Park, Abingdon, Oxon OX14 4SB.
Telephone: (44) 01235 827720. Fax: (44) 01235 400454. Lines are open 9.00–17.00, Monday to Saturday,
with a 24-hour message answering service. Visit our website at www.hoddereducation.co.uk
© Bob Reeves 2015
First published in 2015 by
Hodder Education
An Hachette UK Company,
Carmelite House
50 Victoria Embankment
London EC4Y 0DZ
www.hoddereducation.co.uk
Impression number 5 4 3 2 1
Year 2019 2018 2017 2016 2015
All rights reserved. Apart from any use permitted under UK copyright law, no part of this publication may be
reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying and
recording, or held within any information storage and retrieval system, without permission in writing from the
publisher or under licence from the Copyright Licensing Agency Limited. Further details of such licences (for
reprographic reproduction) may be obtained from the Copyright Licensing Agency Limited, Saffron House, 6–10
Kirby Street, London EC1N 8TS.
Cover photo © LaCozza – Fotolia
A catalogue record for this title is available from the British Library
ISBN 978 1 447 183951 1

, Contents
Introduction v
Section One Fundamentals of programming
1 Programming basics 2
2 Programming concepts 8
3 Basic operations in programming languages 14
4 Subroutines, local and global variables 22
5 Structured programming 28
6 Object-oriented programming concepts 35
Practice questions 46
Section Two Fundamentals of data structures
7 Data Structures and abstract data types 50
8 Queues and stacks 57
9 Graphs and trees 67
10 Hash tables and dictionaries 77
11 Vectors 84
Practice questions 90
Section Three Fundamentals of algorithms
12 Graph and tree traversal 92
13 Dijkstra’s shortest path algorithm 101
14 Search algorithms - binary, binary tree and
linear search 110
15 Reverse Polish Notation 117
16 Sorting algorithms – bubble and merge 124
Practice questions 132
Section Four Fundamentals of computational thinking
17 Abstraction and automation 134
18 Finite state machines 145
19 The Turing machine 150
20 Regular and context-free languages 156
21 Maths for regular expressions 164
22 Big O notation and classification of algorithms 169 iii
Practice questions 179
Section Five Fundamentals of data representation
23 Number systems 182
24 Number bases 187
25 The binary number system 194
26 Coding systems 207
27 Encryption 220
Practice questions 228

Written for

Course

Document information

Uploaded on
June 14, 2023
Number of pages
447
Written in
2022/2023
Type
SUMMARY

Subjects

$13.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
LearnWithLeo Havard college
Follow You need to be logged in order to follow users or courses
Sold
2595
Member since
2 year
Number of followers
13
Documents
6157
Last sold
1 day ago

4.8

218 reviews

5
195
4
15
3
5
2
2
1
1

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions