Mitarbeiter
Prerequisites for the Program Structures Lab Course
This course assumes that all the participants have basic knowledge of at least one "typical" imperative and statically typed programming language like C, C++, or Java. The following tasks are meant to provide some orientation: You should be able to solve the majority of them with very little effort.
These tasks are not mandatory, but we strongly encourage you to solve them using the programming language that you are most familar with. Feel free to ask for an appointment with Marcus (mri) or Cordula (cei) to get some feedback on your solution. This self-assessment losely covers the following concepts:
- Variables and datatypes
- Expressions
- Conditions and branches
- Iteration with loops
- Arrays
- Functions
The following guidelines are valid for all of the following tasks: Most of the tasks are divided into multiple steps that start of with very concrete values to test for. You may skip these concrete values and head directly for a general implementation.
- Implement each task using one or more functions. Ensure that every value that could be provided by a user is a parameter.
- Your main program should be a sequence of calls to these functions that demonstrate their correctness. None of these programs require any UI or user input, it is perfectly fine to hard code any possible inputs from a user.
- Most tasks can easily be solved in < 20 lines of actual code (ignoring boilerplate sections like #include or enforced classes).
- None of these tasks require the use of any object oriented concepts.
- If your language of choice provides a trivial built-in way to solve one of these problems you are not allowed to use them. You may however use functions you have implemented yourself.
Example: Positive and negative numbers
Test whether a given number is positive, negative or exactly zero.:
Solution in C and solution in python
Tasks
- Leap Years
- Number of days in a month
- Summing up integer ranges
- Output a truth table
- Output a multiplication table
- Output a two-dimensional multiplication table
- Retrieving individual digits
- Calculating the required length to display a natural number
- Converting number bases
- Reversing a string
- String palindromes
- Number palindromes
- Checking for sub-strings
- Output a bar chart
Leap Years
A year is a leap year if it is divisible by 4. But if it is divisible by 100 it is not a leap year, unless it is also divisible by 400.
- Calculate whether 1994 is a leap year.
- Calculate whether 2000 is a leap year.
- Calculate whether 2100 is a leap year.
- Calculate whether any given natural number is a leap year.
Number of days in a month
- Calculate the number of days for a certain month of the Gregorian calendar. Assume that the year is not a leap year.
- For February: Take leap years into account. You may use the function that you have written in the previous task.
Things you should look out for:
- There are multiple sensible ways to design the month parameter. Ensure that your inline-documentation describes them accurately. You could for example use any of the following ways to describe a month:
- Integers (should they start at 0 or 1?)
- Strings (what about upper- and lowercase?)
- Enumerations
Summing up integer ranges
- Sum up all numbers from 1 to 100.
- Sum up all numbers from 1 to an arbitrary number m.
- Sum up all numbers from an arbitrary number n to m.
- Sum up all even numbers from n to m.
Things you should look out for:
- What happens if any of the numbers is negative?
- What happens if m < n?
Output a truth table
The output for a truth table with two variables should look exactly like this:
t AND t = t
t AND f = f
f AND t = f
f AND f = f
- Output the four possible combinations of true and false for the logical AND.
- Output the four possible combinations of true and false for the logical OR.
- Use nested loops to output the four possible combinations.
Output a multiplication table
The output for a multiplication table for a single number (in this example: 5) should look exactly like this:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
- Output the multiplication table for the number 5 for the numbers between 1 - 10 (see above).
- Output the multiplication table for any number n for the numbers between 1 - 10.
- Output the multiplication table for any number n for the numbers between low - high.
Things you should look out for:
- Please align all numbers to the right (as shown in the first and last line of the example)
Output a two-dimensional multiplication table
The output for a multiplication table for two ranges (in the example both ranges are 1 - 10) should look exactly like this:
* | 1 2 3 4 5 6 7 8 9 10
--+----------------------------------------
1| 1 2 3 4 5 6 7 8 9 10
2| 2 4 6 8 10 12 14 16 18 20
3| 3 6 9 12 15 18 21 24 27 30
4| 4 8 12 16 20 24 28 32 36 40
5| 5 10 15 20 25 30 35 40 45 50
6| 6 12 18 24 30 36 42 48 54 60
7| 7 14 21 28 35 42 49 56 63 70
8| 8 16 24 32 40 48 56 64 72 80
9| 9 18 27 36 45 54 63 72 81 90
10| 10 20 30 40 50 60 70 80 90 100
Retrieving individual digits
- Calculate the first, second and third digit of the number 1234.
- Calculate the first, second and third digit of any number m.
- Calculate the n-th digit of any number m.
Things you should look out for:
- What about the number 0?
- What about negative numbers?
Calculating the required length to display a natural number
- Calculate the number of characters that is required to display the number 42.
- Calculate the number of characters that is required to display the number -42.
- Calculate the number of characters that is required to display the number 0.
- Calculate the number of characters that is required to display any natural number.
Converting number bases
The "natural" human representation of numbers is base 10 (decimal). In this assignment you shall convert a number that is given in the decimal system to the equivalent number in another base. You can do this by repeatedly dividing the given number by the given base, the following table explains the algorithm:
33 "divided by" 2 = 16 R 1 ^
16 "divided by" 2 = 8 R 0 |
8 "divided by" 2 = 4 R 0 |
4 "divided by" 2 = 2 R 0 |
2 "divided by" 2 = 1 R 0 |
1 "divided by" 2 = 0 R 1 | Read remainders from bottom to top for
the resulting number in base 2.
- Calculate the base 2 representation of the decimal number 42 using the algorithm demonstrated above (hint: it's 101010).
- Implement this algorithm without actually calculating the resulting number. Your output should strongly mimic the example output above.
- Calculate the the base 4 representation of the decimal number 42 using the algorithm demonstrated above.
- Extend your implementation to work with arbitrary integer number bases.
- Remove the intermediate debugging output and change your implementation of the algorithm to actually return the calculated representation.
Reversing a string
- Reverse the string FH Wedel.
- Reverse any given string.
Things you should look out for:
- What about empty strings?
String palindromes
A palindrome is a sequence of characters that reads identically, no matter whether its read front-to-back or back-to-front.
- Check whether the string aabaa is a palindrome.
- Check whether the string aabbaa is a palindrome.
- Check whether the string aababa is a palindrome.
- Check whether any given string is a palindrome.
Things you should look out for:
- What about empty strings?
- There is no need to reverse the string for this task. Creating a reversed copy of the string wastes memory.
Number palindromes
A number-palindrome is a sequence of digits that reads identically, no matter whether its read front-to-back or back-to-front.
- Check whether the number 1 is a palindrome. This is trivially the case, because a single digit number is always a palindrome.
- Check whether the number 11 is a palindrome.
- Check whether the number 121 is a palindrome.
- Check whether the number 1121 is a palindrome.
Things you should look out for:
- In this assignment you are explicitly not allowed to convert the number into a string.
- You can use the modulo-operation (typically denoted as %) to calculate the remainder of a division. If you take any number % 10 the result will be least significant digit.
- Introducing a helper-function like int getNthDigit(int num, int place) will ease the implementation of the actual palindrome functionality.
Checking for sub-strings
- Find the first occurrence of the string abc inside the string aababcbaa.
- Find the first occurrence of any string inside the string aababcbaa.
- Find the first occurrence of any string inside any given string.
- Find the first occurrence of a inside the string aababcbaa that comes after the 4th character.
- Find the first occurrence of a inside the string aababcbaa that comes after the the n-th character.
Things you should look out for:
- What should the function return if no occurrence is found?
- How does your code behave with empty strings?
- What happens if the n-th character position exceeds the length of the given string?
Output a bar chart
- If provided with the values 4, 2 and 7 your function should calculate the following visual representation:
0 (4): ####
1 (2): ##
2 (7): #######
- Allow the user to provide any number of values, not just exactly three.
- Allow the user to specify a maximum width for the columns.