C++ Code Examples

This directory contains code examples organized by topic to help students learn C++ concepts through practical implementations.

Directory Structure

Basic Examples (basic/)

Description: Fundamental C++ programs demonstrating basic syntax and concepts.

Files:

Topics: Basic syntax, I/O, pointers, memory


File Handling (file-handling/)

Description: Examples demonstrating file I/O operations in C++.

Files:

Topics: File I/O, string processing, pattern matching

Related Lectures: Lecture 12, 13


BMP Image Processing (bmp/)

Description: Working with BMP image format - reading, modifying, and creating bitmap images.

Files:

Topics: Binary files, image processing, structs, file formats

Related: Assignment 2, Assignment 2.5


Algorithms (algorithms/)

Description: Implementation of various algorithms and problem-solving techniques.

Files:

Topics: Backtracking, recursion, constraint satisfaction

External: Sudoku Solver Repository


Lecture Code (lectures/)

Description: Code examples from specific lectures.

Files:

Topics: Various (see lecture notes)


Exam Review (exam_review/)

Description: Review materials and example problems for exams.

Files:


Compilation Instructions

For Most Examples (Linux/macOS)

g++ -std=c++11 -o output filename.cpp
./output

For Most Examples (Windows)

g++ -std=c++11 -o output.exe filename.cpp
output.exe

For BMP Examples

cd bmp
g++ -std=c++11 -o test test.cpp
./test

Compilation Flags Explained


How to Use These Examples

  1. Start with basics: Begin with examples in basic/ directory
  2. Read the code: Understand what each line does before running
  3. Modify and experiment: Change values, add features, break things!
  4. Compile frequently: Compile early and often to catch errors
  5. Use debugging tools: Learn to use gdb or IDE debuggers


External Code Repositories


Tips for Learning from Code Examples

  1. Don’t just copy-paste: Type the code yourself to build muscle memory
  2. Experiment: Modify the code to see what happens
  3. Debug: When something breaks, figure out why
  4. Comment: Add your own comments explaining what you learned
  5. Extend: Add new features to existing examples

Compilation Best Practices


Need Help? Check the resources/ directory for books, cheat sheets, and external learning materials.