Notebook

Notebook

A simple notebook.

Try


Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

data type[] x = new data type[y];

Declares an array x of length y.

E.g.

string[] notebook = new string[10];


Note that any variables or arrays/lists declared inside a sub program have a `local scope`. Local scope data structures can only be accessed inside their subprogram.


Any variables or arrays/lists declared outside of a sub program have a `global scope`. Global scope data structures can be accessed anywhere in the program including inside subprograms.


Global scope data structures should be avoided wherever possible because they can lead to conflicts in the code, especially when more than one person is working on a program and chooses to use the same identifier. Local scope data structures also allow for dynamic use of the memory. That means a program only uses the memory it needs when it needs it. Passing parameters into subprograms and returning values from them is considered a much better practice than using global scope.

Investigate


Questions to think about with this program to check your understanding:

Relation question

Lines 51 and 67 iterate through all the notes in the notebook until the length of the notebook is reached. Why is notebook.Length used in this statement instead of the number 10 since the notebook can only have 10 notes as declared in line 104.

104: string[] notebook = new string[10];

51: for (int index = 0; index < notebook.Length; index++)

REVEAL ANSWER

Using the length of the notebook as a terminating condition means that lines 51, 67 and any other iterators don't need to be changed if the programmer decides to increase the size of the notebook. Only line 104 needs to be changed and the program will still work.

This is an example of making a program "scalable". Only one change to one line of code in the program is necessary to increase the functionality. This reduces the liklihood of logic errors due to missing one of the necessary changes.

Another approach is to use a constant throughout the program instead, but this would require a global scope or passing the constant into each subprogram as a parameter.

Approach question

Consider the `show_notebook` procedure. Why has an iteration by index been chosen instead of iteration by element?

I.e.

for (int index = 0; index < notebook.Length; index++)

Instead of:

foreach (string note in notebook)

REVEAL ANSWER

The index is needed in the output as well as the data itself. An alternative approach might be:


static void show_notebook(string[] notebook)

{

index = 0

foreach (string note in notebook)

{

Console.WriteLine(index + ":" + note);

index++;

}

}


In programming there are often many different ways of approaching the same problem. If the speed of execution and the memory usage is not affected, then the readability of the source code probably matters most. Programmers should aim to create easy to read code wherever possible because it makes debugging easier.

Make


Success Criteria

Change the program so that it:

  1. Includes a `delete_note` function that removes a single note.
  2. Includes a `move_note` function that swaps two notes.

Typical inputs and outputs from the program would be:

0 :

1 :

2 :

3 :

4 :

5 :

6 :

7 :

8 :

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:a

Note number: 6

Enter the note: Hello

0 :

1 :

2 :

3 :

4 :

5 :

6 : Hello

7 :

8 :

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:a

Note number: 3

Enter the note: World

0 :

1 :

2 :

3 : World

4 :

5 :

6 : Hello

7 :

8 :

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:a

Note number: 8

Enter the note: Loops reduce the amount of code.

0 :

1 :

2 :

3 : World

4 :

5 :

6 : Hello

7 :

8 : Loops reduce the amount of code.

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:m

Note number: 3

To be moved to...

Note number: 6

0 :

1 :

2 :

3 : Hello

4 :

5 :

6 : World

7 :

8 : Loops reduce the amount of code.

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:m

Note number: 3

To be moved to...

Note number: 5

0 :

1 :

2 :

3 :

4 :

5 : Hello

6 : World

7 :

8 : Loops reduce the amount of code.

9 :


a. Add note

d. Delete note

c. Clear notebook

m. Move note

o. Order notes

:d

Note number: 8

0 :

1 :

2 :

3 :

4 :

5 : Hello

6 : World

7 :

8 :

9 :

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your program several times with different inputs to check that your program has met the success criteria.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Bett Awards 2024 Finalist