Setting up the boilerplate code for the Space Traveller logbook.
00:32
Global List Structure
Using a global list of lists for chapters and entries.
00:56
Adding Chapters and Entries
Adding the first chapter and initial entries using subprograms.
01:59
Addstory Subprogram
Defining the Addstory subprogram to store log entries.
02:30
More Entries and Output
Adding more entries to chapter two and preparing to output.
03:01
Output Subprogram
Writing the Output subprogram to display logbook contents.
03:59
Code Review and Run
Reviewing the code and running the program to see the output.
Transcript
00:02
We start with our boilerplate code.
00:04
In this programme, we're going to create a logbook for Space Traveller.
00:08
The book will have chapters and each chapter will have a number of entries, as we don't know how many chapters or entries the logbook will contain as our traveller continues their journey, we need to use a list instead of an array, appending new chapters and entries as needed.
00:25
Think of the book like a table.
00:27
Each chapter is a row and each entry is a column.
00:32
As our logbook is going to be used by several subprograms, we'll use a global list of lists.
00:38
Static list of chapters is a list of entries, all of which are strings.
00:45
That's the book, which is a new list of list of strings without specifying any data at this stage.
00:52
So open and close brackets.
00:56
In the main programme, let's add the first chapter to our book by calling a subprogram Addchapter.
01:01
all this subprogram does is add another empty list or chapter into the book.
01:08
So book add.
01:11
And the data we want to add is a new list of strings which we won't specify yet as, This subprogram is so short, we don't really need it at all.
01:22
We could just code this line into our main programme when we want to add a new chapter.
01:27
However, doing it this way makes the code in our main programme easier to read and follow.
01:33
Let's add a couple of entries to that first chapter, call a subprogram named Addstory.
01:39
We'll write that in a moment.
01:41
That will use two parameters.
01:43
The chapter, so zero, because we'll start at zero.
01:47
And the first entry to record.
01:49
I find myself alone on a strange world, unequipped and in danger.
01:54
I have no memory of how I got here, no sense of a before.
01:59
Now we'll define that addstory subprogram, static void add storey, which requires the chapter and the entry.
02:09
To store the storey we need book, then the chapter, then add to add to the end, and the entry we want to store back to the main programme.
02:21
And we can add another entry for chapter zero.
02:24
My exosuit at least seems to know what it's doing and I'm not dead yet.
02:30
Now let's add some entries into our logbook for chapter two, or index one.
02:36
I received a set of mysterious coordinates from an unknown source.
02:41
I followed the signal and found the wreckage of an abandoned starship.
02:46
There was little to be gained from the wreck, but the distress beacon contained the hailing frequency labelled Artemis.
02:53
That'll be enough storey to show what this programme does.
02:56
Let's Output the logbook by calling a subprogram output.
03:01
Now writing that subprogram.
03:04
Notice we don't need to pass the book in as data, to any of these subprograms.
03:08
Because we declared book as a global, that made it available to all subprograms.
03:14
Let's assign the first log to be one.
03:17
Next, we want to iterate through the logbook, outputting the entries as.
03:22
It's a list of chapters that contains a list of entries.
03:25
We need a nested loop here for the integer.
03:30
Chapter starts at zero, continues while it's less than the number of rows in the book, add one to the chapter in each iteration.
03:39
For each entry in the book's chapter, output the log number and the entry.
03:45
Increase the log by one.
03:48
Looking at this, we could have used A for each instead of A for the chapters too.
03:53
That would have been neater.
03:55
At the time, I thought I was going to need the chapter number in the output.
03:59
That's the programme written.
04:01
We've stored the data in memory in a logical way to enable us to do more with it later.
04:06
We run it, we can see for now a simple list of entries and log numbers.