Lab materials
- Slides: Introduction
- Slides: Hello
- Slides: Conditionals
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.
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 |
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
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. |
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
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 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
- 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