You are on page 1of 4

MCD4720 Lab 3 MCD4720 Fundamentals of C++

Written By: Matthew Butler, Haidar AL-Khalidi and Leslie Hughes

Topics
 Decisions and Loops

Learning Objective
 Be able to write and compile basic C++ code to solve a simple programming problem continents
decision(s) and loop(s)

Small Coding Exercises:


Task 1: Number Modifier Program
a. Create a new solution called ‘NumberModifier’. This program will accept 2 integers and then
a char, one at a time. The char can be any of the following: +, -, *, or /. Based on the char
entered your program should add, divide, subtract or multiply the 2 numbers.

b. Have the user enter their name when they start the program. Add some instructions to tell
the user what the program does, what the program accepts as inputs and what the program
outputs, addressing the player by their name.

Example output can be seen below:

Lab 3
1
MCD4720 Lab 3 MCD4720 Fundamentals of C++

Task 2: Number Modifier Program continued


a. Continuing on from the last exercise, it’s time to add some validation. Validate the char
entered to make sure it’s one of the 4 possible modifiers you can use. Output an error
message if it’s wrong.
b. Validate both numbers entered and make sure they fit within the range limits of the signed
data type short (2 bytes… clue… what are the upper and lower limits of this number?).
Output an error message if the numbers aren’t within the limits. To prove that the numbers
are both within the range, store the numbers in a short data type variable.

An example output might be:

Task 3: Your Personal Name Number


This small program will calculate a person’s personal name number. In numerology, a name
number is believed to reveal insight into the personality of a person based on their name.

Have the user enter their first name and store this in a string object. Run a basic loop to look at
each letter in the string and calculate the users name number. It is calculated as follows:

 Add up the letters based on their corresponding number in the alphabet. For example,
‘a’ = 1, ‘b’ = 2, all the way up to ‘z’ = 26

 This number is your “preliminary” name number

 To derive your final name number, you should add up the digits that make that number.
If the total is a result of double figures, you add again. For example “matt” gives the
preliminary name number 54. Adding 5 + 4 gives a final number of 9. Another example is
“nick”. His “preliminary” name number is 37. 3 + 7 = 10, so we then add the digits 1 + 0
to arrive at the final answer of 1.

 Hints… how can you get the single digits in a large number? Will modulus help?

Lab 3
2
MCD4720 Lab 3 MCD4720 Fundamentals of C++

Task 4: Is Prime?
Write a small piece of code that will determine if a number entered by the user is a prime
number. Remember that a prime number is one that is only divisible by 1 or itself.

Task 5: Troubleshooting
The following code is designed to have the user enter an integer between 1 and 10, and print the
times tables up to (but not over) 100. Alas, it doesn’t do it. Copy and paste the code into a new
project and get it working! Be sure to use real-time debugging to trace the loop. At the bottom is
an example of working code.

integer nNum = 0;

cout <<"Times Table Program\n";

cout << "This program will accept an integer between 1 and 10,";

cout << "and display the times tables up to 100." << \n;

cout << "\nWhat number would you like? ";

cin << Num;

for (int i = 1; nNum <= 100; i++) {

cout << nNum << " times i is " << (nNum * i) << endl;

i++;

Lab 3
3
MCD4720 Lab 3 MCD4720 Fundamentals of C++

Task 6: Advanced Exercise: QuizMaster continued


This week’s advanced exercise is to complete last weeks QuizMaster game.

Now that you have designed the game logic and created the start of the code, you can now finish the
game! Features you should now be able to add on to those coded last week include:

 Reading from file appropriately


 Using a loop to manage the whole game loop
Using d

Lab 3
4

You might also like