Laboratory 3

Laboratory 3 – Loops, ASCII Tables and Repeated Calculations

Continuation of the calculator task, introduction to repeated execution with loops, printing ranges of ASCII characters, and generating a simple multiplication table.

functions do-while ASCII nested loops tables

Laboratory Overview

What is new in this laboratory.

In this laboratory, you will write your first user-defined function and your first loop that repeats a calculation.

Loops allow programs to repeat actions automatically, which is essential for processing multiple values, generating tables, and building interactive programs that do more than one calculation.

Learning Outcomes

  • work in the correct laboratory directory on the AGH UNIX server,
  • extend a previous C program instead of rewriting everything from the beginning,
  • use if-else and switch in a slightly more advanced calculator,
  • define and use a simple user-defined function,
  • use a do-while loop for repeated program execution,
  • print a range of ASCII codes together with their character representations,
  • generate a formatted multiplication table using nested loops,
  • compile and run all tasks using the workflow introduced in previous laboratories.

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 3

mkdir lab3
cd lab3

Step 4 — General rules

  • Work only in ~/I2PL/lab3.
  • 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 7 — Extended calculator with modulo

Extend the calculator from Laboratory 2, add your first user-defined function, and support the modulo operator.

Step 1 — Create the file

nano ex7.c

Step 2 — Start from the previous calculator

You may reuse your solution from Laboratory 2 or rewrite the calculator from scratch. The program should still read:

  • a number,
  • an operator,
  • another number.

For example:

12+5
10%3

Step 3 — Add your own function

Declare and define the following function:

int isInteger(float x);

The function should check whether the given value is an integer. Return 0 for false and a non-zero value for true.

Step 4 — Add modulo as a new operator

Extend the calculator with an additional case for the % operator.

Modulo should only be calculated for integer values.

Step 5 — Check whether modulo is allowed

Use an if-else statement to decide whether modulo can be calculated for the values provided by the user.

You may need to change the variable types from the previous version of the calculator.

Step 6 — Handle invalid cases

  • If the operator is not supported, print an error message.
  • If division by zero is requested, print an error message.
  • If modulo is requested for non-integer values, print an error message.

Step 7 — Compile and run

gcc -Wall ex7.c -o ex7
./ex7
The goal is not only to add one more operator, but also to practice writing your own function and using it inside the main program logic.

Task 8 — Repeated calculator using do-while

Extend the previous program so that the user can repeat calculations in a loop.

Step 1 — Create the file

nano ex8.c

Step 2 — Start from Task 7

You may reuse your solution from Laboratory 2 or rewrite the calculator from scratch.

Step 3 — Use a do-while loop

Place the calculator logic inside a do-while loop.

At the end of each calculation, ask the user whether the program should continue.

Step 4 — Ask for continuation

After printing the result, ask the user a question such as:

Do you want to continue? (Y/N)

Only the answers Y and N are supported.

Step 5 — Repeat or exit

  • If the answer is Y, repeat the calculator.
  • If the answer is N, finish the program.

Step 6 — Compile and run

gcc -Wall ex8.c -o ex8
./ex8
When reading a character after numeric input, remember that whitespace may remain in the input buffer. A safe form is:
scanf(" %c", &choice);
This task introduces one of the most important ideas in programming: repeating a block of code until a condition becomes false.

Task 9 — ASCII codes from 33 to 126

Display a list of printable ASCII characters together with their numeric codes.

Step 1 — Create the file

nano ex9.c

Step 2 — Print the header

Print a simple two-column header, for example:

Code  Character

Step 3 — Use a loop from 33 to 126

Write a loop that goes through all ASCII codes from 33 to 126.

Step 4 — Print the number and the corresponding character

Each row should contain:

  • the ASCII code as a number,
  • the character corresponding to that code.

Examples:

33 !
34 "
35 #
...
126 ~

Step 5 — Compile and run

gcc -Wall ex9.c -o ex9
./ex9
This task practices loops and also reinforces the idea that characters in C are represented by numeric codes.

Task 10 — Multiplication table 5×5

Display a formatted multiplication table using a field width of 5 characters.

Step 1 — Create the file

nano ex10.c

Step 2 — Print a 5×5 multiplication table

The program should display the products for numbers from 1 to 5.

The result should look similar to:

    1    2    3    4    5
    2    4    6    8   10
    3    6    9   12   15
    4    8   12   16   20
    5   10   15   20   25

Step 3 — Use nested loops

Use one loop for rows and another loop for columns.

Step 4 — Use field width in printf

Each number should be printed in a field of width 5 characters.

printf("%5d", value);

Step 5 — Print line breaks correctly

After printing one row, move to the next line.

Step 6 — Compile and run

gcc -Wall ex10.c -o ex10
./ex10
The table should be aligned. Use the same field width for every printed number.
This task is a standard introduction to nested loops and formatted tabular output.

What will be checked during the next laboratory

  • working in the correct laboratory directory on student.agh.edu.pl,
  • extending an existing program instead of rewriting it unnecessarily,
  • defining and using a simple user-defined function,
  • using if-else, switch, and do-while correctly,
  • handling invalid cases such as unsupported operators and division by zero,
  • printing ASCII codes and characters in a loop,
  • using nested loops to generate a multiplication table,
  • writing readable and properly indented code.