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 CC++, 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

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.

  1. Calculate whether 1994 is a leap year.
  2. Calculate whether 2000 is a leap year.
  3. Calculate whether 2100 is a leap year.
  4. Calculate whether any given natural number is a leap year.

 

 

Number of days in a month

  1. Calculate the number of days for a certain month of the Gregorian calendar. Assume that the year is not a leap year.
  2. 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

  1. Sum up all numbers from 1 to 100.
  2. Sum up all numbers from 1 to an arbitrary number m.
  3. Sum up all numbers from an arbitrary number n to m.
  4. 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


 
  1. Output the four possible combinations of true and false for the logical AND.
  2. Output the four possible combinations of true and false for the logical OR.
  3. 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 
  1. Output the multiplication table for the number 5 for the numbers between 1 - 10 (see above).
  2. Output the multiplication table for any number n for the numbers between 1 - 10.
  3. 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

  1. Calculate the first, second and third digit of the number 1234.
  2. Calculate the first, second and third digit of any number m.
  3. 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

  1. Calculate the number of characters that is required to display the number 42.
  2. Calculate the number of characters that is required to display the number -42.
  3. Calculate the number of characters that is required to display the number 0.
  4. 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.

 

  1. Calculate the base 2 representation of the decimal number 42 using the algorithm demonstrated above (hint: it's 101010).
  2. Implement this algorithm without actually calculating the resulting number. Your output should strongly mimic the example output above.
  3. Calculate the the base 4 representation of the decimal number 42 using the algorithm demonstrated above.
  4. Extend your implementation to work with arbitrary integer number bases.
  5. Remove the intermediate debugging output and change your implementation of the algorithm to actually return the calculated representation.

 

 

Reversing a string

  1. Reverse the string FH Wedel.
  2. 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.

  1. Check whether the string aabaa is a palindrome.
  2. Check whether the string aabbaa is a palindrome.
  3. Check whether the string aababa is a palindrome.
  4. 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.

  1. Check whether the number 1 is a palindrome. This is trivially the case, because a single digit number is always a palindrome.
  2. Check whether the number 11 is a palindrome.
  3. Check whether the number 121 is a palindrome.
  4. 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

  1. Find the first occurrence of the string abc inside the string aababcbaa.
  2. Find the first occurrence of any string inside the string aababcbaa.
  3. Find the first occurrence of any string inside any given string.
  4. Find the first occurrence of a inside the string aababcbaa that comes after the 4th character.
  5. 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

  1. If provided with the values 4, 2 and 7 your function should calculate the following visual representation:
    0 (4): #### 
    1 (2): ## 
    2 (7): #######
  2. Allow the user to provide any number of values, not just exactly three.
  3. Allow the user to specify a maximum width for the columns.