Lab materials
- Slides: Introduction
- Slides: Hello
- Commented version of the example code: hello.c
- Slides: Conditionals
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.
1 2 3 4 5 6 |
The user will enter an integer If the entered number is even: Print: „The entered number is even“ Else: Print: „The entered number is odd“ End of program |
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.
- 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 .
- 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.
- 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.
- 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!
- 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:
1printf("The number <.........> is an <........> number\n");
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
1 2 3 |
Please enter an integer: 2 The number 2 is an even number. |
Test 2: odd number
1 2 3 |
Please enter an integer: 19 The number 19 is an 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
1 2 3 4 |
Enter a number to check: 15 Divisible by both 3 and 5. By dividing 15 with 3, we get 5 multiples of 3 and a remainder of 0. By dividing 15 with 5, we get 3 multiples of 5 and a remainder of 0. |
Test 2: Number is divisible by 3 only
1 2 3 4 |
Enter a number to check: 27 Divisible by 3 only. By dividing 27 with 3, we get 9 multiples of 3 and a remainder of 0. By dividing 27 with 5, we get 5 multiples of 5 and a remainder of 2. |
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
- Use of AI in university, including study regulations
https://taltech.ee/en/study-regulations-and-documents - Why it’s so important that the algorithms are written in a way that everyone understands them the same way.
Youtube: Exact Instructions Challenge – THIS is why my kids hate me. | Josh Darnit
https://www.youtube.com/watch?v=cDA3_5982h8 - Things I Wish I Knew When I Was Learning to Code
https://www.youtube.com/watch?v=TvDFJpGnQZo - “C” Programming Language: Brian Kernighan – Computerphile
https://www.youtube.com/watch?v=de2Hsvxaf8M - Programming languages popularity index
https://www.tiobe.com/tiobe-index/ - C vs Python 3 performance comparisons
https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python3-gcc.html - C is the greenest programming language
https://hackaday.com/2021/11/18/c-is-the-greenest-programming-language/ - printf documentation
http://cplusplus.com/reference/cstdio/printf/ - scanf documentation
http://cplusplus.com/reference/cstdio/scanf/ - UML standard documentation
https://www.omg.org/spec/UML/2.5.1/PDF - Data types in C
https://en.wikipedia.org/wiki/C_data_types