Basics of C++
For this activity, you will need to be using an Integrated Development Environment (IDE). The recommended IDE for this module is JetBrains IntelliJ IDEA for Python and JetBrains CLion for C++. You can follow instructions at the following page to set up your development environment:
Setting up your Development Environment
Task 1 - Writing Hello World
As the C++ programming language is new to you, and the syntax is relatively different, it is best to begin with a simple
script. For this task, you are concerned with displaying the string Hello World
to the terminal window.
Hint
To assist you with this task, you may want to use the iostream
library.
Task 2 - Declaring Variables
For this task, you will want to declare a variable and assign a value to it. Using the table below, you are to implement a solution in C++ declaring the variables with the names provided and an appropriate value for the data type provided.
Variable Name | Data Type |
---|---|
a | Null |
b | Integer |
c | Float |
c | Double |
d | String |
e | Boolean |
f | Character |
Task 3 - Arithmetic Operations
For this task, you will be testing your knowledge on arithmetic and in particular, the process of calculating interest. You are required to provide an equation that will calculate the interest earned on a principal figure over a defined period of time for a set interest rate. The values and variables are defined below:
Variable | Definition | Value |
---|---|---|
\(p\) | The principal amount. | £10,492 |
\(t\) | The length of time. | 7 Years |
\(r\) | The rate of interest. | 0.025% |
Task 4 - More Arithmetic Operations
For this task, you will be testing your knowledge on arithmetic and assigning the result of an equation to a variable. To complete this task, you will need to answer the questions below, providing a solution in C++.
- Write an equation:
- to calculate 8 to the power of 4
- to calculate the sum of 5 and 6, multiplied by the exponent of 9 and 3
- that finds the remainder of 87 divided by 8
Task 5 - Comparison Operators
For this task, you will be using the comparison operators. You are required to provide solutions that would satisfy the questions provided below.
- Declare a variable \(x\) and have it evaluated to True when variable \(a\) is less than \(10\)
- Declare a variable \(y\) and have it evaluated to False when variable \(a\) is greater than or equal to \(5\)
- Declare a variable \(z\) and have it evaluated to True when \(a\) is greater than or equal to \(5\)
Hint
For this task you are expected to use the same value for \(a\) to solve each of the equations above.
Task 6 - Logical Operations
For this task, you are required to answer the following questions with an appropriate solution:
- Declare variables \(x\) and \(y\) to be \(10\) and \(50\), respectively.
- write an expression that checks whether \(x\) and \(y\) are the same object
- write an expression that checks whether \(x\) is less than \(20\), and \(y\) is equal to \(x\)
Task 7 - Bitwise Operations
For this task, you are required to answer the following questions with an appropriate solution:
- Write an expression that uses the binary AND on two numbers:
40
and128
- Write an expression that uses the binary OR on two numbers:
92
and34
- Write an expression that converts the integer number
128
to binary
Struggling?
If you are struggling with this lab activity, you may want to get some additional support. You can speak to the module leader/team in the room of when the lab week is active, or you can visit the Additional Support page for more information on how you can get extra support.