3. lab: loops

Lab materials

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

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

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