PR1EN3: Loops

Lab materials

Tasks

This lab has two base tasks for which you need to create a total of four separate programs. There are also two extra tasks, separate from the base tasks, that can be written as a single program.

Task 1 [W03-1]: Sum of 5 numbers

You are tasked with creating a program that will find the sum of five numbers 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 number, except the last one, the program prints the current subtotal
  • After the last number is entered, the program prints the final sum instead of the subtotal
  • Program must give a clear instructions to the user
  • 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
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 most things that are counted in the computer, start from 0. However you should display a number incremented by 1 to the user – non-developers 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 incrementing (permanent).
Testing

This is the expected output of the program

Task 2 [W03-2]: Cash register with input validation

In this task, we will revisit the task from last week and add a few features to it. You can either use the provided starter code or your own solution from last week.

Download the lab task 2 starter code [optional]: https://blue.pri.ee/ttu/files/iax0583/aluskoodid/3_1_cashier_starter_loop.c

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

Extra tasks

Extra task 1 [W03-3]: 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 [W03-4]: 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