Laboratory Overview
What this laboratory develops.
In this laboratory, you will use loops not only for repeated calculations, but also for building structured output and exploring simple algorithms. The exercises move from visual patterns to a small algorithmic problem with banknotes, then to the Fibonacci sequence, and finally to a function that keeps internal state.
loops → patterns → algorithmic thinking → simple functions
In this laboratory, functions are used not only to organize code, but also to keep internal state between function calls.
Learning Outcomes
- work in the correct laboratory directory on the AGH UNIX server,
- use loops to print structured patterns of characters,
- use field width in
printfto control alignment, - solve a simple banknote decomposition problem using a small number of loops,
- calculate the n-th Fibonacci number iteratively,
- write and test a function that remembers how many times it was called,
- write readable, properly indented C programs without global variables.
Task 0 — Reminder and preparation
Step 1 — Log in to the AGH UNIX server
ssh your_login@student.agh.edu.pl
Step 2 — Go to the course directory
cd ~/I2PL
Step 3 — Create the directory for Laboratory 4
mkdir lab4
cd lab4
Step 4 — General rules
- Work only in
~/I2PL/lab4. - Keep all your programs in this directory.
- Do not use global variables.
- Keep the code readable and properly indented.
- Compile with:
gcc -Wall program.c -o program
- Run with:
./program
student.agh.edu.pl, use nano, compile with -Wall,
and run programs with ./program.
Task 11 — Equilateral triangle of asterisks
Step 1 — Create the file
nano ex11.c
Step 2 — Read the height
Read the height of the triangle from the user using scanf.
Step 3 — Print a centered triangle
Print a centered triangle made of asterisks. Each next row should contain more asterisks than the previous one and be aligned symmetrically.
A typical output for height 6 may look like:
*
***
*****
*******
*********
***********
Step 4 — Think in two parts
In each row, you need to print:
- some leading spaces,
- then the required number of asterisks.
Step 5 — Compile and run
gcc -Wall ex11.c -o ex11
./ex11
printf useful here, for example:
printf("%*d", 10, 20);
The original exercise explicitly suggests more advanced use of printf.
Task 12 — Triangle with increasing number of asterisks
Step 1 — Create the file
nano ex12.c
Step 2 — Read the height
Read the height of the triangle from the user using scanf.
Step 3 — Follow the pattern shown below
Print the following sequence of rows:
*
***
*****
*******
*********
***********
That means the number of asterisks grows by 2 in each next row.
Step 4 — Use one outer loop and one inner loop
Use one loop to control the current row and another loop to print the right number of asterisks.
Step 5 — Compile and run
gcc -Wall ex12.c -o ex12
./ex12
Task 13 — Hollow square of asterisks
Step 1 — Create the file
nano ex13.c
Step 2 — Read the side length
Read the side size from the user using scanf.
Step 3 — Print a hollow square
Print a square with asterisks on the border and spaces inside.
******
* *
* *
* *
* *
******
Step 4 — Distinguish border and inside
For each printed position, decide whether it belongs to:
- the first row,
- the last row,
- the first column,
- the last column,
- or the inside of the square.
Step 5 — Compile and run
gcc -Wall ex13.c -o ex13
./ex13
Task 14 — Banknote decompositions for 20, 50 and 100 PLN
Step 1 — Create the file
nano ex14.c
Step 2 — Read the total amount
Read the amount of money in PLN from the user using scanf.
Step 3 — Print all possible combinations
Use only the banknotes 20, 50, and 100 PLN.
Display all possible decompositions, starting with the largest nominal value.
For example, for 120 PLN:
120 PLN = 1*100 + 0*50 + 1*20
120 PLN = 0*100 + 2*50 + 1*20
120 PLN = 0*100 + 0*50 + 6*20
Step 4 — Use no more than two loops
Use an efficient idea and no more than two loops.
- iterate over the number of 100 PLN banknotes,
- iterate over the number of 50 PLN banknotes,
- compute the remaining amount for 20 PLN banknotes directly.
Step 5 — Print only valid decompositions
A decomposition is valid only if the remaining amount is non-negative and divisible by 20.
Step 6 — Compile and run
gcc -Wall ex14.c -o ex14
./ex14
Task 15 — Fibonacci sequence
Step 1 — Create the file
nano ex15.c
Step 2 — Read n
Read the element number from the user using scanf.
Step 3 — Use the standard definition
Use the following definition:
a_n = a_(n-2) + a_(n-1)
a_1 = 1, a_2 = 1
Step 4 — Prefer an iterative solution
At this stage, use variables and a loop instead of recursion. Keep track of the two previous values and update them step by step.
Step 5 — Handle small values correctly
Make sure the cases n = 1 and n = 2 work correctly.
Step 6 — Compile and run
gcc -Wall ex15.c -o ex15
./ex15
Task 16 — Function call counter
Step 1 — Create the file
nano ex16.c
Step 2 — Use the required declaration
Use the following declaration:
void function();
Step 3 — Make the function remember its previous calls
The function should print how many times it has been called so far.
Step 4 — Call the function several times from main
Write a short test program that calls the function multiple times and shows that the counter increases.
Step 5 — Compile and run
gcc -Wall ex16.c -o ex16
./ex16
What will be checked during the next laboratory
- working in the correct laboratory directory on
student.agh.edu.pl, - using nested loops to print triangles and squares,
- using formatting and alignment in
printf, - solving the banknote decomposition problem with a sensible algorithm,
- calculating the Fibonacci sequence iteratively,
- understanding how a function can remember how many times it was called,
- writing readable and properly indented code without global variables.