Laboratory 4

Laboratory 4 – Patterns, Algorithms and Simple Functions

Practice with loops and formatted output continues with pattern printing, a simple combinatorial problem with banknotes, Fibonacci sequence calculation, and the first function that remembers how many times it was called.

patterns nested loops algorithms Fibonacci functions

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.

This laboratory builds directly on loops introduced in Laboratory 3.
Main idea of this lab:
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 printf to 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

Before starting the exercises, log in to the AGH server and prepare the working directory.

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
This laboratory continues the workflow introduced in previous laboratories: work on student.agh.edu.pl, use nano, compile with -Wall, and run programs with ./program.

Task 11 — Equilateral triangle of asterisks

Display a centered triangle of asterisks. Read the height from the user.

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
This is a classic exercise for nested loops and formatted output. It also prepares you for more advanced pattern-printing tasks.
You may find field width in 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

Display the triangle pattern shown below, again using the height from the user.

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
Start by deciding how many asterisks should be printed in each row.
Make sure each row ends with a newline. Otherwise the pattern will not be readable.

Task 13 — Hollow square of asterisks

Display a square pattern. Read the side length from the user.

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
This task is a good exercise in combining nested loops with conditional statements.

Task 14 — Banknote decompositions for 20, 50 and 100 PLN

Display all possible decompositions of a given amount using 20, 50 and 100 PLN banknotes.

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.

A good strategy is:
  • 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
Avoid brute-force solutions with many unnecessary loops. Try to design a simple and efficient algorithm.

Task 15 — Fibonacci sequence

Calculate and display the value of the n-th element of the 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
Fibonacci is a classic exercise in algorithmic thinking. The main challenge is not syntax, but organizing the logic clearly.

Task 16 — Function call counter

Write a function that displays how many times it has been called, then test it several times.

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.

Since global variables should be avoided, think about a variable declared inside the function that keeps its value between calls.

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
Do not solve this by using a global variable.
This task introduces a very important concept: a function may have its own internal state.

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.