Introduction
Pattern problems are how most C courses teach nested loops, and they appear in nearly every lab exam. This project collects the important ones in one program and solves each with a short, readable function.
Problem statement
Objectives
- Derive the number of spaces and symbols per row
- Use nested loops with dependent limits
- Compute binomial coefficients incrementally
- Fill a 2D array in spiral order using boundary variables
Features
- Seven patterns selectable from a menu
- Pascal's triangle computed without factorials
- Spiral matrix filled with shrinking boundaries
- An array of function pointers maps menu choices to patterns
Concepts demonstrated
- Nested loops
- 2D arrays
- Function pointers
- Combinatorics
System architecture
Each pattern is a void f(int n) function. main stores them in an array of function pointers alongside their names, so the menu is a loop over that array and the chosen pattern is called through patterns[choice - 1](size).
Folder structure
Complete source code
Makefile
What each file does
- src/main.c
- One function per pattern plus the menu;
pascaluses C(i, k+1) = C(i, k) × (i − k) / (k + 1). - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
2 4 3 3 6 5 7 4 0
Testing
- Compare with
tests/expected-output.txt(pyramid 4, diamond 3, Floyd 5, spiral 4). - Print each pattern at size 1 — every pattern must still look correct.
Common errors
Pyramid leans to one side
The space loop must run n - i times before the stars.
Spiral has repeated or missing numbers for even sizes
Check the if (top <= bottom) and if (left <= right) guards before the bottom row and left column passes.
Possible improvements
- Let the user choose the symbol
- Add alphabet patterns (A, AB, ABC…)
- Write patterns to a text file
Additional challenges
- 1Print a hollow diamond.
- 2Print the spiral counter-clockwise.
- 3Print Pascal's triangle up to 30 rows using
unsigned long longand check where it overflows.
Built and tested it yourself? Mark it to update your dashboard.