Programming With 1D Arrays

Programming With 1D Arrays

Arrays are a large feature of the Computer Science GCSE. This is mainly because after the basic data types, they are the fundamental building blocks of any programming language.

programming with arrays - Computer science revision

In their most basic form they are variables that can hold more than one value. You’ll probably see lots of long and complicated words used when people talk about arrays, but hopefully I can dispel a few myths here.

If you’ve ever written a shopping list, then you’ve written an array and they don’t get much more difficult.

When we start our shopping list, we start with a blank list. Each item on the list is written as a word, so we know in Computer Science terms that this is an array of strings. In pseudocode, this might look like:

myShopping = [ ]

Notice how I’ve used square brackets to tell the computer that this variable is going to hold more than one piece of data. Square brackets is the same across almost every programming language.

Now if I wanted to add an item to my shopping list, I would want to add it to the end. In Computer Science, we call this appending (if it was just writing, then we would be writing over the top of data that is already there). In pseudocode, this looks like:

myShopping.Append(“Apple”)

If I had a list of fruit I would write each item on a new line. In code, instead of a new line, we separate each item with a comma. So after adding my apple, a banana, and a strawberry to my list it would look like this:

[“Apple”,”Banana”,”Strawberry”]

But what if I want to tell someone what the 2nd item on my list is without showing the others?

Well this is where lists really make a big difference to your programs. If you want to find out where something is in a book, you look at the index and you’ll find a page number that you can go to. In an array, an index is a number that tells you where to go in the array to find the item of data that you are looking for. So to tell someone what the 2nd item in the array is we use:

OUTPUT myShopping[1]

Now, hang on just a minute! Was that a typo??? Actually no. Unlike us who count from 1, computers start at 0 which means that the 1st item is item 0, the 2nd is item 1… and so on. For those of you looking for higher grades, it’s worth knowing some technical maths terms at this point:

Ordinal Number: 1st 2nd 3rd…

Cardinal Number: 0, 1, 2, 3…

​Activity

Let’s put this into practice with a short Python program. Imagine that we wanted to create a shopping list that our program could remember. Remembering that we refer to a one-dimensional array as a list, we could create a static array where we program our whole list in the program code, or we could create a dynamic array that grows with our shopping list.

Try extending the program code below so that our user can add more than one item to the list using iteration. (looping)

Understanding The CPU Registers

Understanding The CPU Registers

The majority of devices that we use today, will use the Von Neumann Architecture to run the Fetch Decode Execute Cycle that allows the processor to run instructions. The Von Neumann Architecture is often referred to as the Stored Program Concept – what this means is that both the instructions and the data are stored in the same format… binary

CPU registers are specific pieces of memory within the processor that hold memory to allow instructions to be fetched, decoded, and executed.

  • PC (Program Counter)
    • Holds the address of the next instruction to be processed
    • This is passed to the MAR
  • MAR (Memory Address Register)
    • Holds the address of the instruction to be processed
  • MDR (Memory Data Register – aka Memory Buffer Register)
    • Initially holds the instruction found at the address in the MAR
    • Then later, is used to store any data needed for the instruction
  • CIR (Current Instruction Register)
    • Holds the address currently being processed
  • Accumulator
    • Holds the outcome of the instruction
    • Can act like a running total in a calculator as more instructions are processed

Activity

Test out how much you can remember from the video with by matching the components to the correct definition.

Revision Task

Download, print and complete the revision notes below. Don’t forget to add colour to the key words and your own notes in the areas provided.

The Purpose of The CPU

The Purpose of The CPU

Imagine the CPU as the brain of the computer – it’s needed to process all of the data coming into the system, and tells the system how to react to that data.

The purpose of the CPU is to control both hardware and software to the user, provide an interface (hiding the complex systems), and to manage memory. To do this, an Architecture needs to be used to allow the CPU to understand how to process the instructions within the device.

The tasks that the CPU performs can be broken down into Fetch, Decode, and Execute. This is referred to as the Fetch Execute Cycle and allows your computer to function.

Activity

Now you have watched the video on what the CPU is for, can you match the key terms with their definitions?

Revision

Download the notes file and add your own to this while you watch the video above and in the next lesson. These will be useful later as you will need these to revise!