Introduction
Separating data from code is a key software-engineering idea. Here the questions live in an editable text file; the program parses it, randomises the order reproducibly, and produces a scored report with per-topic results.
Problem statement
questions.txt, ask a chosen number of them in shuffled order, accept answers A–D (re-prompting on invalid input), and finish with the score, a per-topic breakdown and a review of incorrect answers.Objectives
- Parse a line-based text format into structs
- Implement the Fisher–Yates shuffle
- Aggregate results by category
- Accept a file name from the command line
Features
- Editable question bank with topics
- Reproducible shuffling from a seed
- Input re-prompting
- Score, per-topic breakdown and answer review
Concepts demonstrated
- Parsing
- Shuffling algorithms
- Arrays of structs
- Aggregation
System architecture
load reads the file line by line, filling a Question as it sees Q:, T:, A:–D: and ANS: lines; a question is accepted only when all parts are present. An index array is shuffled with Fisher–Yates using a seeded LCG. Topic totals are kept in parallel arrays.
Folder structure
Complete source code
Makefile
What each file does
- src/main.c
- File parser, LCG + Fisher–Yates shuffle, the quiz loop and the result report.
- 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.
7 5 B C x A D B
Testing
- Diff with
tests/expected-output.txt(seed 7, 5 questions, including an invalid answer "x"). - Add a new question to questions.txt and confirm it is loaded.
- Delete the ANS line of one question and check that it is skipped.
Common errors
"Could not load questions"
Run the program from the project folder, or pass the file path: ./app path/to/questions.txt.
A question is silently skipped
Each question needs exactly the lines Q, T, A, B, C, D and ANS in that order.
Possible improvements
- Timed questions
- Negative marking
- Save high scores
Additional challenges
- 1Shuffle the options of each question too (and track the new correct letter).
- 2Support questions with code snippets spanning several lines.
- 3Read the question bank in CSV format instead.
Built and tested it yourself? Mark it to update your dashboard.