Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

A simple notebook.

Notebook

Try

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

Knowledge organiser

Initialising a string[] array

Creating string[] x = new string[z] declares an array of z elements. Each element is null by default; if you want them to be empty strings instead, initialise them explicitly:

string[] x = new string[z];
for (int i = 0; i < x.Length; i++) x[i] = "";

Note that any arrays/lists declared inside a subprogram have a local scope.

Local scope data structures can only be accessed inside their subprogram.

Any arrays/lists declared outside of a subprogram (at the class level as static) have a global scope and can be accessed anywhere in the program.

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. 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

The clear_notes function uses a for loop to set every slot to "". Why can’t this be replaced with notebook = new string[10]? (Assume notebook is passed in as a parameter.)

Reveal answer

Reassigning the parameter notebook = new string[10] only changes the local variable. The caller still sees the original array. To clear in place, assign each slot.

Approach question

Consider the show_notebook procedure. Why has an index-based for loop been chosen instead of foreach?

Reveal answer

The index is needed in the output as well as the data itself. With foreach (var note in notebook) you’d need a separate counter to print the index alongside the value.

Make

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
q. Quit
a
6
Hello
…
🆘 If you're really stuck, use this Parsons code sorting exercise
get_note_number
// Return valid note index (0-9)
static int get_note_number()
{
---
    int note_number = 0;
    bool valid_input = false;
    while (!valid_input)
    {
---
        note_number = Convert.ToInt32(Console.ReadLine());
        if (note_number >= 0 && note_number < 10)
        {
---
            valid_input = true;
---
        }
---
        else
        {
---
            Console.WriteLine("Invalid input. Enter 0-9.");
---
        }
---
    }
---
    return note_number;
}
add_note, clear_notes
// Write a new note
static void add_note(string[] notebook)
{
---
    int index = get_note_number();
    string note = Console.ReadLine();
    notebook[index] = note;
---
}


---
// Clear the whole notebook (after confirmation)
static void clear_notes(string[] notebook)
{
---
    string choice = "";
    while (choice != "y" && choice != "n")
    {
---
        choice = Console.ReadLine();
---
    }
---
    if (choice == "y")
    {
---
        for (int index = 0; index < notebook.Length; index++)
        {
---
            notebook[index] = "";
---
        }
---
    }
}
delete_note, move_note
// Clear a single note
static void delete_note(string[] notebook)
{
---
    int index = get_note_number();
    notebook[index] = "";
---
}


---
// Swap two notes
static void move_note(string[] notebook)
{
---
    int first_note = get_note_number();
    Console.WriteLine("To be moved to...");
    int second_note = get_note_number();
    string temp = notebook[first_note];
    notebook[first_note] = notebook[second_note];
    notebook[second_note] = temp;
}
order_notes, show_notebook
// Sort notes alphabetically
static void order_notes(string[] notebook)
{
---
    Array.Sort(notebook);
---
}


---
// Show the notebook
static void show_notebook(string[] notebook)
{
---
    for (int index = 0; index < notebook.Length; index++)
    {
---
        Console.WriteLine($"{index} : {notebook[index]}");
---
    }
}
menu
// Menu (returns false when the user quits)
static bool menu(string[] notebook)
{
---
    Console.WriteLine();
    Console.WriteLine("a. Add note");
    Console.WriteLine("d. Delete note");
    Console.WriteLine("c. Clear notebook");
    Console.WriteLine("m. Move note");
    Console.WriteLine("o. Order notes");
    Console.WriteLine("q. Quit");
    string choice = Console.ReadLine();
---
    if (choice == "q")
    {
        return false;
    }
---
    switch (choice)
    {
---
        case "a": add_note(notebook); break;
        case "d": delete_note(notebook); break;
        case "c": clear_notes(notebook); break;
        case "m": move_note(notebook); break;
        case "o": order_notes(notebook); break;
---
    }
    return true;
}