PR1EN1: Hello world

Lab materials

Tasks

Most likely everything in this lab is completely new to you. Because of this, most of the lab we’ll be working together – lecturer shows things in front and you’ll follow along on your computer.

In the end of the lab, you’ll have one base task to complete and defend. Faster ones will also be able to complete and defend the extra task.

Task 1 [W01-1]: Parity check (base task)

Requirements
  • User is asked for and must be able to enter an integer
  • Program must prints the entered number
  • Program must print if the entered integer was odd or even

NB! In addition to functional requirements, the program  must also follow the style requirements. Most common errors are related to indentation (number of spaces from the left before the code), missing spaces surrounding operators (e.g. space before and after equal sign) and lack of empty lines to indicate different logical parts of the program.

Task background

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

In this task we are doing modulo 2 division – i.e. dividing the number by 2 and taking the remainder. This allows us to check if the number is odd or even. 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).

Pseudo code

Pseudo code is one way of describing algorithms. It uses similar structure to a program, but does not adhere to language specific rules such as syntax requirements. This means pseudo code is typically human readable without knowledge of programming.

Algorithm

The task is modeled together in a class, to show you some of the techniques around formatting the diagram – e.g. how to create a conditional statement, using right angles when creating control flows etc. For the sake of completeness of the task specification, we have also given one of the two possible forms of this algorithm in this page.

How to solve the task

When solving the task, follow both the algorithm and the code snippets provided on the slides. This task resembles a lego that needs to be assembled correctly.

  1. Start by creating a new file. Save it in a subdirectory for Programming course on your P drive using a   .c  file extension- e.g. paritycheck.c .
  2. Now write the template code from the last slide into your code file. We will be showing you this on the projector as well. Note that we have left a mistake in the template! You should immediately notice it if you try to compile the program at its current state. Fix the mistake before proceeding to work on the task. If necessary, look in your hello.c  program for a similar line – you should find a solution and explanation in the comment of your code.
  3. The next piece of the puzzle is the code structure for a conditional statement. Find how to find the conditional if/else statement from the slides and write the structure into your program. Don’t copy code – writing helps with your muscle memory. Make sure to pay attention to the indentation of the code and the space following the  if keyword. Poorly formatted code will not pass the defense.
  4. Third piece of the puzzle is the condition itself (goes within the parenthesis for the  if  statement. Just as a reminder, the task is to check if an entered number is odd or even. Examples of conditions are provided on the slides. After creating the conditional statements, try to compile and make sure there are no errors!
  5. The last piece of the puzzle is the contents for the code blocks so that the result would be printed correctly. For this you need to write two calls to the  printf()  function. They should look something similar to the following code statement:
    Notice, that we left in two blanks. One of them needs to contain the number entered by the user. The second one whether it was an odd or an even number.
Testing

This program has two 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

Everything worked as intended? Good, you are almost done!

Remember that the code has to be readable and maintainable. To achieve this, all programmers must adhere to coding style. Compare the look of your code to previous programs and examples from this class. Pay attention to

  • Use of spaces (before and after mathematical operators, after the if  keyword
  • Indentation – how many spaces must be left empty from the left of the text document on each line. Indentation by 1 is typically considered to be 1 tab or 4 spaces. The statements inside of a code block (indicated by the curly braces) always increase indentation by 1. This means that the code in the main function should be indented one (e.g. asking for input, reading input, return statement), but the printing of the result should be indented once more, because they are inside of another (nested) code block.
  • Use empty lines to visually separate different parts of code (before main()  function, before the if  statement, before the return statement)

Code both functional and readable? Now let us know that you want to defend your task!

Extra task [W01-2]: Division by 3 and 5

Before solving this task, solve the base task! You should first defend the base task and then the extra 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 print whether the entered number is divisible by 3, divisible by 5, divisible by both or neither of them.
  • The program must find the multiples and remainders by dividing the number by both 3 and 5. All four 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 this 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