Laboratory Overview
What is new in this laboratory.
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-elseandswitchin a slightly more advanced calculator, - define and use a simple user-defined function,
- use a
do-whileloop 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
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
student.agh.edu.pl, use nano, compile with -Wall,
and run programs with ./program.
Task 7 — Extended calculator with modulo
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.
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.
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
Task 8 — Repeated calculator using do-while
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.
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
scanf(" %c", &choice);
Task 9 — ASCII codes from 33 to 126
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
Task 10 — Multiplication table 5×5
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
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, anddo-whilecorrectly, - 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.