Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab session 1: Imperative Programming

This is the first lab of a series of weekly programming labs. You are required to solve these
programming exercises and submit your solutions to the automatic assessment system Themis.
The system is 24/7 online. Note that the weekly deadlines are very strict (they are given for the
CET time zone)! If you are a few seconds late, Themis will not accept your submission (which
means you failed the exercise). Therefore, you are strongly advised to start working on the
weekly labs as soon as they appear. Do not postpone making the labs! You can find Themis via
the url https://themis.housing.rug.nl

All lab exercises assume that you are running Linux. If you do not, do not expect help from
the teaching assistants in solving computer related problems. If you do not have Linux on your
computer, you are strongly advised to install it. You can install it next to your current operating
system (a so called dual boot system system) or you can run it in a virtual machine. If you
do not have experience installing an operating system (or a dual boot system), then the safest
option is to use a virtual machine. You can run a virtual machine (also on Apple Mac’s) using
the program virtualbox, which is freely available via https://www.virtualbox.org/.
Once you installed virtualbox, you can install any Linux distribution. If you want to install
Linux in a virtual machine yourself, then we advise the Ubuntu distribution that uses the XFCE
desktop which is called xubuntu (see https://xubuntu.org/). However, we made a
pre-configured virtual machine image which you can download from Themis (this is a very large
file (26 GB), so downloading it will take some time).

Note: In lab session 1 you are only allowed to make use of constructs that are taught in
week 1 (so no loops, arrays, functions, etc).

Problem 1: Hello world!


The first exercise is to make a small computer program that outputs Hello world! on the
screen. The goal of the exercise is to get acquainted with the programming environment on a
Linux system and the automatic assessment system Themis.
1. Open a terminal window. In this terminal you can type commands. Start by creating a
directory impprog that you will use for the course Imperative Programming. You create
this directory with the command:
mkdir impprog
2. Navigate to this directory using the command cd (change directory).
cd impprog
3. Create another directory week1, which is a subdirectory of impprog. Create in the
directory week1 another subdirectory hello and navigate to this directory:
mkdir week1

1
cd week1
mkdir hello
cd hello
Create the file hello.c using an editor (for example geany).
geany hello.c &
Type the following program, replace ... by the right information, and save it.

/* file: hello.c */
/* author: ...... (email: ....) */
/* date: ...... */
/* version: ..... */
/* Description: This program prints ’Hello world’ */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[]) {


printf("Hello world!\n");
return 0;
}

4. Compile the program using the command:


gcc -std=c99 -Wall -pedantic hello.c

5. On successful compilation, an executable file a.out is produced. Run the program:


./a.out

6. If you are convinced that your program works well, you can submit it to the online as-
sessment system Themis. You have completed this first task once Themis accepted your
submission.

2
Problem 2: Scrap Paper
During exams, you will get a few sheets of scrap paper. Scrap paper is packaged in bundles. For
example, for an exam with 500 students, packages that contain 200 sheets, and each student gets
2 sheets of scrap paper, we clearly need 5 packages of scrap paper.
You are requested to write a program that reads from the input three positive integers: the
first integer is the number of students that take part in an exam, the second number is the number
of sheets that each student gets, and the last number is the number of sheets in a package. The
output of the program should be the number of packages needed.
Make sure that the output must have the format as in the following examples. Moreover,
your output must end with a newline (\n).

Example 1: Example 2: Example 3:


input: input: input:
100 2 200 100 1 100 100 2 100
output: output: output:
1 1 2

Problem 3: Decimal Number


The decimal notation 1234 denotes actually 1 × 1000 + 2 × 100 + 3 × 10 + 4.
You are requested to write a program that reads an integer n from the input (where 1000 ≤
n < 10000) and outputs the number in the format as given in the examples below.
Example 1: Example 2:
input: input:
1234 8402
output: output:
1234=1*1000 + 2*100 + 3*10 + 4 8402=8*1000 + 4*100 + 0*10 + 2

Example 3:
input:
7264
output:
7264=7*1000 + 2*100 + 6*10 + 4

3
Problem 4: Stock Market
Consider the following scenario. You buy 100 stocks (or crypto coins, or any other alike invest-
ment), priced 1.00C each, so you invest 100.00C. In one year, the stocks go up by 10.0%, so
after one year you have 100 × 1.10 = 110.00C. The next year, the stocks go up again by 10%,
and at the end you have 110 × 1.10 = 121.00C. Overall, after two years your profit is 21.00C.
Write a program that accepts on its input 4 numbers. The first number is a positive integer,
denoting the number of stocks that an invester bought at some point in time. The second number
is a floating point number, denoting the price per stock at that time. The third number is a floating
point number which is a percentage that denotes the rise of the stocks (or fall, in which case this
number is negative) after the first year. The third number is the rise or fall in the second year.
The output of your program should be the profit or loss of the inverstment after two years. Make
sure that your output matches with the following examples. Note that prices are rounded (and
printed) to the level of cents, so a profit (or loss) of less than one cent is considered a tie (no loss,
no profit).
Example 1: Example 2: Example 3:
input: input: input:
100 1.00 10.0 10.0 100 1.00 10.0 -10.0 123 456.78 0.00 0.00
output: output: output:
Profit 21.00 Loss 1.00 Tie

Problem 5: Area of overlap


4
In the figure on the right, you see a grid containing two rect- 3 s
angles. One rectangle is defined by the coordinates of the 2
corner points (−1, 0) and (2, 3). The other rectangle is deter- 1 s
0 s
mined by the coordinates (1, −2) and (−2, 1). Of course, the
grid in the figure is finite, but in reality the grid is assumed -1
-2 s
to be infinitely large. From the figure it is clear that the two
-3
rectangles overlap. The area of the overlap is 2×1 = 2. Note -4
that the area of touching rectangles is defined to be zero. -4 -3 -2 -1 0 1 2 3 4
Write a program that first reads from the input the coordinates of the two corner points of a
rectangle, and then reads the two corner points of a second rectangle. You may assume that the
corner points are grid points, i.e. their coordinates are integers. The program must print on the
screen the area of the overlap of the two rectangles.
Example 1: Example 3:
Example 2:
input: input:
0 0 2 3
-1 0 2 3 0 0 10 10
-1 0 -2 -2
1 -2 -2 1 1 1 9 9
output:
output: output:
0
2 64

You might also like