You are on page 1of 3

Lab session 1: Imperative Programming

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 the lab computers
and the automatic assessment system Justitia.

1. Log on to the lab system. After log in, 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
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

1
6. If you are convinced that your program works well, you can submit it to the online assessment
system Justitia. You can reach Justitia via the link http://justitia.housing.rug.nl
using your favourite webbrowser. You have completed this first task once Justitia accepted your
submission.

Problem 2: Repro shop


At the start of each academic year it is very busy at the repro shop. Teachers hand in the pdf-files
of the readers that need to be printed as soon as possible. Before the operator of the shop can start
printing, he must first decide how many packs of paper he needs. A pack contains 500 sheets of paper.
Write a program that computes how many packs of paper are needed for printing the readers.
The input consists of a positive integer n, the number of required sheets of paper. The output is the
required number of packs of paper. Note that there are no extra spaces in the output, and that the
output ends with a newline (\n):
Example 1: Example 2: Example 3:
input: input: input:
500 1000 5000
output: output: output:
1 2 10

Problem 3: Enclosing numbers


Let n be an integer, where 1000 ≤ n < 10000. A number m, where 10 ≤ m < 100, is called the
enclosing number of n if and only if the first digit of n is also the first digit of m and the last digit of n
is also the last digit of m. For example, 42 is the enclosing number of the numbers 4112, 4262, 4652,
and 4872.
Write a program that reads an integer n from the input (you may assume that 1000 ≤ n < 10000),
and prints the corresponding enclosing number on the output.

Example 1: Example 2: Example 3:


input: input: input:
2300 4112 1234
output: output: output:
20 encloses 2300. 42 encloses 4112. 14 encloses 1234.

Problem 4: Scheduling jobs


Peter is a good programmer and made a small business out of this. Some day, he is confronted with the
following problem. He has three clients that all have a programming task that needs to be finished as
soon as possible. Because Peter is a good programmer, he knows for each task exactly how much time
he needs to finish it. The problem is that Peter cannot do these programming tasks simultaneously, so
he needs to choose which job to do first, which second and which last. He decides to perform the tasks
in an order such that the average time that a client needs to wait until its task is finished is minimal.
For example: there are three tasks A, B and C that cost 1, 6 and 3 hours respectively to finish. If
Peter performs these in the order A, B, C then after 1 hour task A is finished, after 7 (=1+6) hours
task A and B is finished and after 10=(1+6+3) hours all tasks are finished. This yields an average time

2
of (1 + 7 + 10)/3 = 6.0 hours. If Peter chooses the order A, C, B then the average time drops to
(1 + 4 + 10)/3 = 5.0 hours. After some trying, Peter discovers that the order A, C, B is indeed the
order that results in a minimal average completion time, so he chooses this order.
Write a program that reads from the input the duration of three tasks (these are integer numbers)
and that prints on the output the minimal average completion time for these three tasks. The output
should be a floating point number rounded to one digit after the decimal dot.

Example 1: Example 2: Example 3:


input: input: input:
1 6 3 1 3 5 10 30 60
output: output: output:
5.0 4.7 50.0

Problem 5: Overlap
4
In the figure on the right, you see a grid containing two rectangles. 3 s
One rectangle is defined by the coordinates of the corner points 2
(0, 0) and (2, 3). The other rectangle is determined by the coor- 1 s
0 s
dinates (−2, 1) and (1, −2). Of course, the grid in the figure is
finite, but in reality the grid is assumed to be infinitely large. From -1
-2 s
the figure it is clear that the two rectangles overlap. We call two
-3
rectangles overlapping if they have at least one point in common,
-4
so two rectangles that touch each other are also overlapping. -4 -3 -2 -1 0 1 2 3 4

Make 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
coordinates are grid points, i.e. their coordinates are integers. The program must print on the screen
whether the two rectangles overlap. Make sure that your program produces output in the exact same
format as given in the following examples.

Example 1 (see figure):


input:
0 0 2 3
-2 1 1 -2
output:
overlap
Example 2:
input:
0 0 2 3
-1 0 -2 -2
output:
no overlap

You might also like