GCSE Computer Science · Computational thinking, algorithms and programming

Strings, arrays, files and subprograms

String handling, arrays in one and two dimensions, records, reading and writing files, random numbers, and functions and procedures.

317 GCSE-style practice questions

Watch the lesson

Every point below, worked through in one video, and each point links straight to its own part.

10 minutes. Plays from YouTube.

More lessons like this one on our YouTube channel

Learn

Strings, arrays, files and subprograms, explained point by point

Everything the GCSE specification expects you to be able to do, and how to actually do it - the same lesson a signed-in student studies from.

  1. Manipulate strings

    Strings can be measured, joined and cut. The length of a string is the number of characters in it, counting spaces. Concatenation joins two strings into one. Slicing takes part of a string, called a substring, by position, and positions usually count from 0, so the first character is at position 0. You can also change a string to upper or lower case and turn a character into its character code and back. The commands differ from language to language, so learn the ones your exam uses. Python measures with len and cuts with a slice such as word[0:3], which stops before its second number; C# and VB.NET use word.Length and word.Substring(0, 3), whose second number is how many characters to take.

  2. Use one-dimensional arrays

    An array stores several values under one name, each reached by its index, which is its position. In most languages the index starts at 0, so an array of five scores has indexes 0 to 4. An array pairs naturally with a for loop that visits each index in turn, to add up, search or print the values. Python’s lists do the same job, and Edexcel and Eduqas use them for arrays. In C# and VB.NET an array has a fixed size, so a List is used when items have to be added as the program runs.

  3. Use two-dimensional arrays

    A two-dimensional array is a grid, with one index for the row and one for the column. It suits anything that is naturally a table: a seating plan, a game board, or one row of marks for each pupil. To visit every cell, put one loop inside another, the outer one for the rows and the inner one for the columns. Check which index your exam writes first, since this is where marks are lost.

  4. Use records to store related data

    A record keeps the different facts about one thing together, such as a pupil’s name, age and form. Each fact is a field, and unlike an array the fields can have different data types. Many records of the same shape make up a table, which is how a file of data or a database is organised. OCR points out that a two-dimensional array can stand in for a table, with a row for each record and a column for each field.

  5. Read from and write to text filesOCR, Edexcel and Eduqas only

    Variables are lost when a program ends, so data that must be kept is written to a file. The pattern is always the same: open the file, read from it or write to it, usually one line at a time, then close it. Closing matters, because it is what makes sure the data has been saved and lets other programs use the file. OCR, Edexcel and Eduqas examine file handling, and Edexcel’s files are comma-separated text. AQA does not examine it.

  6. Write and use subprograms: functions and procedures

    A subprogram is a named block of code that does one job and can be called whenever that job is needed. A function returns a value to the code that called it. A procedure carries out its instructions and returns nothing. Parameters are the values passed in for it to work with. Subprograms make a program shorter, since the code is written once, and easier to test and to maintain, since each one can be checked on its own. AQA calls them subroutines.

  7. Explain the difference between local and global variables

    A local variable is created inside a subprogram and exists only while that subprogram runs. Code outside cannot see or change it. A global variable is created in the main program and can be used anywhere. Local variables are safer: two subprograms can use the same name without interfering with each other, and a value cannot be changed by accident from somewhere else. AQA examines local variables only.

  8. Generate random numbersOCR, AQA and Edexcel only

    A program can ask for a random number within a range, such as 1 to 6 to model a dice. Each run gives a different result, so a trace has to say which number was produced. Random numbers are used in games, in simulations and for picking an item from a list at random. OCR and AQA examine this directly, and Edexcel provides a random library in its list of permitted Python. Watch the top of the range: Python’s random.randint(1, 6) can give 6, but in C# and VB.NET a Random object’s Next(1, 7) is needed for 1 to 6, since its second number is never given.

  9. Use the further Python features of the on-screen programming examEduqas only

    Eduqas’s Component 2 is a programming exam in Python 3, built on a scenario released months earlier, and it goes further than the other boards. It expects lists, tuples, which are lists that cannot be changed, and dictionaries, which store values under named keys. It also expects programs that use library modules and simple windowed interfaces with text boxes and buttons. In the exam you are given a working program and asked to extend it, test it and evaluate it.

Strings, arrays, files and subprograms key terms

The words the specification and the mark schemes use, each defined the way an examiner wants it.

String
A sequence of characters, such as a word or a sentence.
Concatenation
Joining two or more strings together to make one string.
Substring
A part of a string, taken out by giving its position and length.
Array
A data structure that stores several values of the same type under one name, each reached by its index.
Index
The position of an item in an array or a string, usually counted from 0.
Record
A data structure that holds related fields about one thing, where the fields may have different data types.
Subprogram
A named block of code that carries out one task and can be called from elsewhere in the program.
Function
A subprogram that returns a value to the code that called it.
Procedure
A subprogram that carries out a task and does not return a value.
Parameter
A value passed into a subprogram for it to use.
Local variable
A variable that is created inside a subprogram and can only be used there.
Global variable
A variable that is created in the main program and can be used anywhere in the program.

Common mistakes in Strings, arrays, files and subprograms

The slips that cost marks year after year, and what to do instead.

  • Counting array positions from 1 when the language counts from 0

    In most exam languages the first item is at index 0, so the last item of five is at index 4.

  • Mixing up a function and a procedure

    A function returns a value that the calling code can use. A procedure does its job and returns nothing.

  • Forgetting to close a file after using it

    Closing the file saves what was written and frees it for other programs. Open, read or write, then close.

  • Using a variable outside the subprogram that created it

    A local variable only exists inside its own subprogram. Pass the value out with return, or in with a parameter.

  • Swapping the row and the column in a two-dimensional array

    Check which index comes first in your exam’s notation, and test it on one cell of the grid in the question.

Practice

Try a Strings, arrays, files and subprograms question

A GCSE-style original question from this topic. Have a go before you open the working - deciding on an answer first is what makes the working stick.

In OCR Exam Reference Language, town = "PLYMOUTH". What is output by print(town.substring(2, 4))?

Make Strings, arrays, files and subprograms stick

A free account adds worked examples from our question bank, from a warm-up to a stretch, plus ask-Wiz - a coach you can put any point from this lesson to - and a checklist that remembers which points you've studied. No card needed.

Ready to practise Strings, arrays, files and subprograms?

Sign up free to practise this topic - questions that adapt to how you're doing, with instant encouraging feedback on every answer. No card needed.

Download on the App StoreGet it on Google Play

Already have an account? Log in

More Computational thinking, algorithms and programming topics