1. Lab: Hello world

Lab materials

Tasks

In this lab, most of the tasks are done together.  Later in class, you will have one base task and one extra task to solve on your own.

Task 1: Parity check (base task)

Requirements
  • User is asked for and must be able to enter an integer.
  • Program outputs if the entered integer was odd or even.
Pseudo code

Pseudo code is one way of describing algorithms. It has a similar structure to a program, but does not adhere to language specific rules such as syntax requirements. The written “code” should be well structured, but remain human readable.

Background

Modulo division is widely used as a means of creating a checksum. E.g. it can be used to validate your id code or bank card number to be valid.

In this task we are doing modulo 2 division – e.g. parity check. Parity check is one of the simplest ways to do error checking in data transfers – e.g. if the amount of bits in a data packet with the value ‘1’ is odd or even? This can be used to check if there was a data transmission error (though as said before, this will only catch extremely simple errors).

Testing

This program has 2 possible outcomes – the number is either even or odd. To test your program, you need to run the program twice, covering both of these tests.

Test 1: even number

Test 2: odd number

If everything works as expected, make sure to present your task!

Extra task: Division by 3 and 5

First complete the base task!

Read on how to use logical operators: https://blue.pri.ee/ttu/coding-guides/conditional-statements/#Logical_operators

Requirements
  • User is asked for and must be able to enter an integer.
  • Program must output whether the number is divisible by 3, divisible by 5, divisible by both or neither of them.
  • The program will find the multiples and remainders by dividing the number by both 3 and 5. All 4 results must be printed regardless of the divisibility.
  • Calculations (e.g. divisions) can only be done once. If you need to use the result multiple times in your code, store it in a variable.
Background

The task is based on a classical interview question used for recruiting software developers. This is a modified version of the FizzBuzz task.

Testing

Based on the divisibility checks, you have 4 possible outcomes that must all be tested.

Test 1: Number is divisible by both 3 and 5

Test 2: Number is divisible by 3 only

Figure the last 2 tests out on your own and make sure it works in all cases!

After the class, you should

  • Understand the requirements for this subject, including how to get a grade
  • Be able to log into Linux on the class computer
  • Know the main software we use in this class
  • Know your way around the environments – where to find what
  • Know what is the C programming language, how does the program structure look like and how to write a basic program.
  • Know the structure of a C program and be able to write, compile and execute a simple program.
  • Understand the basics of the following programming concepts.
    •  #include statements
    • why is main() function special
    • declaring variables, data types
    • print statements for basic text, changing line, printing the contents of a single or multiple variables with text and integer data types.
    • reading a value from keyboard and storing it in a variable (scanf statement)
    • basic math operations and how they are written
    • if/else statement (conditional statement)
    • Understand integer division and modulo division
  • Understand what an algorithm is
  • Understand what is UML and why it is used. Understanding the basic elements (start, end, action statement, fork, join) in the design. Being able to create a basic diagram in UML.

Additional content