2. lab: conditional statements

Lab materials

Tasks

Task 1: cash register

With this task, we’ll go over a simple cash register application with the goal of trying to compose different conditional statements. Look for the comments in the base code and write the described lines of code after the comment!

Requirements:
  • Program must be logically identical to the given algorithm
  • Program must be built on the base code given. Your task is to add the missing printf , scanf ,conditional statements and calculations. Use the comments and the UML as a reference to where and what you need to write.
  • Don’t change the variable naming or the program structure.
  • Recommendation: To check that the values are correct and program behaves correctly, add printouts to check the values  in variables after operations. You can also check for entry into conditional statements.

Download task 1 basecode: [et] [en]

Algorithm

Testing

I’m proposing a series of tests for the program you are about to create. NB! I’ve left at least one important test case untested. Can you figure out which one?

Click me to see the test cases!

Test 1: Client is not offered any discount, successfully completes the payment. Also testing floating point values.

Test 2: Client has the loyalty card and gets an extra discount.

Test 3: Client has exactly the required amount of assets on their card.

Test 4: Client that is only able to afford the purchase after the discount.

Test 5: Client with not enough assets.

Test 6: Client, who does not know their PIN code.

Task 2: food scale

In this task you will have to create a program that simulates the food scale in a grocery store.

The UML activity diagram that the program will be structured upon is created in class. Your program must match or exceed the functionality described.

Requirements
  • You must have at least 4 products available.
  • Products are represented as pairs of product codes (integer) and names (e.g. 1 – banana).
  • You must handle matching the product code to the price of 1 kg in the switch  statement.
  • User will be displayed the total price only if a valid product code and weight are entered.
  • The given price(s) must be given with 2 decimal places.
  • Invalid product code and weight must trigger an error. There is no need to specify which error occurred. In case of an error, price must not be shown!

Download the base code for task 2: 2_2_scale_base.c

Testing

Test 1: Correct inputs will give us a total cost.

Test 2: Invalid product code triggers an error.
Test 3: Invalid weight triggers an error.

After the class, you should 

  • Know what is a truth table and how to read them
  • Be able to form conditional statements consisting of multiple conditions
  • Know what De Morgan’s law is
  • Know how to use inversion and short forms for conditional statements
  • Be able to initialize integers and floating point values when declaring them
  • Be able to nest code blocks in each other as done. Nest conditional statements.
  • Be able to use the same variable on both sides of the equal sign
  • Be able to use and print floating point values (simple cases)
  • Be able to use a switch statement
  • Be able to model a switch statement in UML
  • Know how to use swim lanes
  • Know how to create programs with multiple endings

Additional content