Lab materials
- Slides: Preprocessor and compilation
- Slides: Loops
Tasks
This lab has two base tasks for which you need to create 5 programs in total and 2 extra tasks that can be written into a single program.
Task 1: 5-number sum
You are tasked with creating a program that will find the sum of 5 numbers which are entered by the user. The program must be created 3 times – one for each loop type. All 3 programs must do the exact same thing.
Requirements
- Program asks the user for 5 integers.
- After every input, the program outputs the current sum.
- After the last value is entered, the program outputs the final sum instead of the current sum.
- Program must give a clear instructions for the user what is expected of them.
- You cannot have any magical numbers in your code
- Design your program in a way that if you would need to do the same task for 10, 20 or even 1000 numbers, you will only need to change one number in your code. Test it before submitting – change that number to be 7 and see if your program still works and the output makes sense!
- Create the same program using a while, do while and a for loop.
Sample of expected result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
This program takes 5 numbers from the user and adds them up. Subtotal is shown after each addition until the last one. Enter number 1 / 5 9 Subtotal 9 Enter number 2 / 5 3 Subtotal 12 Enter number 3 / 5 2 Subtotal 14 Enter number 4 / 5 1 Subtotal 15 Enter number 5 / 5 5 The final sum is 20 |
Hints
- Think carefully which variables you will need! Regardless of how many numbers you need to add up, the amount of variables should remain the same.
- It’s recommended to start counting loops from 0. This is because many other internals also rely on that number. However you should display a number incremented by 1 to the user – users usually count from 1.
- (i + 1) takes the value if i and uses the incremented value in that place only. The incremented value is not stored and does not affect the program elsewhere. E.g. printf("%d", i + 1);
- Operations such as i++, ++i, i += 1 and i = i + 1 store the updated value after incrementation (permanent).
Task 2: cash register with loops
In this task we’ll revisit the task from last week and add a few features to it.
Download the lab task 2 base code: [ register ]
Requirements
You must add the following features to the program:
1. Input validation for loyalty card and extra discount
- Ask the user for input until either 0 or 1 is entered
- For invalid input, print an error message
- Recommendation: Use an exit-controlled loop.
2. PIN code can be entered up to 3 times
- If the PIN code has been entered incorrectly for 3 times, write an error message and close the program.
- If a correct PIN is entered within 3 attempts, check the balance and if possible, do the transaction.
- Recommendation: Use an entry-controlled counting loop.
Sample of expected result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Enter purchase total: 59.99 Did client present loyalty card? 1 - yes 0 - no 3 Error! Enter 1 for yes or 0 for no! Did client present loyalty card? 1 - yes 0 - no 1 Apply extra discount? 1 - yes 0 - no 0 Invoice total: 53.99 Please enter card PIN code: 3333 Error! Incorrect PIN code! You have 2 attempts left! Please enter card PIN code: 1234 Purchase complete! New account balance is 46.01 Thank you, come again! |
Extra tasks
Extra task 1: Mario tower
- Using nested loops, create a representation of the Mario stairs using the pound symbol. A total of 13 rows of steps.
- Place an ASCII stick man on top of the stairs.
- The width of the top platform must be enough for the stick man to rest his feet comfortably.
- In the end of each row, show the number of blocks used for that row and the total number of used blocks until that moment.
- Alignment should be as shown in the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
O -|- | / \ row total ### 3 3 #### 4 7 ##### 5 12 ###### 6 18 ####### 7 25 ######## 8 33 ######### 9 42 ########## 10 52 ########### 11 63 ############ 12 75 ############# 13 88 ############## 14 102 ############### 15 117 |
Extra task 2: Mirrored tower with specified height
- Ask the user for the width of the platform (range 7 -20 blocks)
- The steps must be mirrored this time as is in the Mario game when approaching the castle.
- The bottom-most step must start from the left edge of the terminal window, no spaces allowed before it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Enter base width [7 - 20]: 25 Error! Allowed base width is 7 - 20! Enter base width [7 - 20]: 13 O -|- | / \ row total ### 3 3 #### 4 7 ##### 5 12 ###### 6 18 ####### 7 25 ######## 8 33 ######### 9 42 ########## 10 52 ########### 11 63 ############ 12 75 ############# 13 88 |
Hints
- Task is based on the idea of nesting loops inside of each other
- Think of which symbol is invisible
- Text and numbers can be padded https://blue.pri.ee/ttu/coding-guides/some-printf-tips/
After the class, you should
- Know what steps need to be gone through to get from the source code that we write to a executable program
- Know what is a #define macro and how it works
- Know what is a magic number and how to handle them
- Know about problems with non-initialized variables.
- Use a switch statement.
- Use all 3 types of loops for a specified number of iterations.
- Know how to create a loop that runs infinitely
- Know the increment/decrement operators.
- Know the difference between loops that check the conditions before or after executing the loop body.
- Be able to nest blocks of code (e.g. conditional statement inside of a loop, loop inside of a loop, …)
- Know about two control statements: break and continue.
- Know, that we DO NOT use goto statements.
- Use expressions and constants as printf arguments.
- Be able to model loops in UML
Additional content
- Magic number (programming)
https://en.wikipedia.org/wiki/Magic_number_(programming) - C preprocessor
https://en.wikipedia.org/wiki/C_preprocessor - GOTO, Goto & Goto – Computerphile
https://www.youtube.com/watch?v=FGAWniPGKjc - Bjarne Stroustrup Quotes
https://www.stroustrup.com/quotes.html - Loops in C and C++
https://www.geeksforgeeks.org/loops-in-c-and-cpp/ - C break and continue
https://www.programiz.com/c-programming/c-break-continue-statement