Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

⚙️Tutorial: C++ Part 2

This tutorial guides you through more advanced foundational concepts of the C of C++ for edge programming, including control flow, arrays, and memory management. The questions are strongly led by writing code to reinforce the practical application of the notes from Chapter 3, Part 2.

Work through the 10 exercises below. Each question includes a sample solution and an explanation in the associated video. Please try these yourself first before watching the video.

Good luck!


Loops are fundamental to programming, allowing a block of code to be executed repeatedly. The for loop is ideal when you know exactly how many times you want the loop to run.

Write a program that uses a for loop to print the square of the numbers from 0 to 9, each on a new line.


Conditional statements are used to make decisions in your code. The if-else statement executes a block of code if a condition is true and a different block if it is false.

Write a program that declares an integer variable number. Assign it a value and then use an if-else statement to print whether the number is even or odd.


The scope of a variable is the part of the program where it can be used. Variables declared inside a block of code (like a loop or a function) are only accessible within that block.

Write a program that demonstrates variable scope. Declare a variable x outside a for loop and another variable y inside the loop. Print both x and y inside the loop, and then try to print y outside the loop. Note the error.


The switch statement is a cleaner alternative to a long if-else-if chain when you are testing a variable against a fixed list of values.

Write a program that takes an integer from 1 to 5 and uses a switch statement to print the name of the corresponding day of the week. For any other number, print a default message.


An array is a data structure that stores a fixed-size collection of elements of the same type. This is a common and efficient structure for edge programming.

Write a program that declares an array of 5 integers, initialises them, and then uses a for loop to print each element of the array to the console. change Note: Modern C++ often uses std::array (for fixed sizes) or std::vector (for dynamic sizes) for better safety and features. change


Multi-dimensional arrays are essentially arrays of arrays, useful for representing grids or tables of data.

Write a program that declares and initialises a 2 x 3 two-dimensional integer array. Use nested for loops to iterate through the array and print all the elements.


Pointers are variables that store memory addresses. They are central to C and C++ for direct memory manipulation, which is critical in embedded systems.

Write a program that declares an integer variable myVar. Then, declare a pointer ptr and store the address of myVar in it. change Note: Always initialize pointers. If a pointer doesn’t point to anything yet, use nullptr (C++11) instead of the legacy NULL. change

Additionally, declare a void pointer voidPtr and assign the address of myVar to it. Create a separate function that accepts a void pointer, casts it back to the correct type, and then prints the dereferenced value. change (Note: Use static_cast for safe pointer conversion). change


Question 8: Dynamic Memory Allocation (new and delete)

Section titled “Question 8: Dynamic Memory Allocation (new and delete)”

Unlike static arrays, which are fixed in size at compile-time, dynamic memory allows you to allocate memory at runtime. This is essential for applications where memory requirements are not known in advance.

Write a program that dynamically allocates memory for a single integer using new, assigns a value to it, prints the value, and then frees the memory using delete. change Note: In modern C++, manual memory management with new and delete is discouraged in favor of smart pointers (like std::unique_ptr) and containers. change


Logical operators are used to combine multiple conditions. The primary ones are && (AND), || (OR), and ! (NOT).

Write a program that asks the user to input a number. Use an if statement with logical operators to check if the number is within the range of 10 to 20 (inclusive) OR if it is equal to 5.


Question 10: The for-each Loop (Range-based for loop)

Section titled “Question 10: The for-each Loop (Range-based for loop)”

The for-each loop, introduced in C++11, provides a simple and clean way to iterate over the elements of an array or container without manually managing an index.

Write a program that declares an array of strings representing the colours of the rainbow. Use a for-each loop to print each colour to the console.

The video solutions for these questions are available in the next section.


Here are the video solutions — please do not watch these solutions without having attempted the questions first. Please note that these solutions are somewhat warts and all in that I make errors and correct them live without edits.

Alternatively, you can view the video directly on YouTube: https://www.youtube.com/watch?v=0WsGAedDvDM