cpp
exercises
exercises.cpp⚙️cpp
/**
* Module 01: Foundations
* Topic: Introduction to C++
* File: exercises.cpp
*
* Complete the exercises below to practice what you learned.
* Each exercise has a TODO section for you to complete.
*
* Compile: g++ -std=c++17 -Wall -Wextra exercises.cpp -o exercises
* Run: ./exercises
*/
#include <iostream>
#include <string>
using namespace std;
// ============================================================
// EXERCISE 1: Your Personal Greeting
// Difficulty: ⭐ (Beginner)
//
// Task: Create a personalized greeting message that displays:
// - Your name
// - Your favorite programming language
// - Why you want to learn C++
// ============================================================
void exercise1() {
cout << "\n=== EXERCISE 1: Personal Greeting ===" << endl;
// TODO: Declare variables and print your personal greeting
// Example output:
// "Hello! My name is [Your Name]"
// "My favorite language is [Language]"
// "I want to learn C++ because [Reason]"
// Write your code below:
}
// ============================================================
// EXERCISE 2: ASCII Art
// Difficulty: ⭐ (Beginner)
//
// Task: Create a simple ASCII art using cout statements.
// Make a house, tree, face, or any simple shape.
// ============================================================
void exercise2() {
cout << "\n=== EXERCISE 2: ASCII Art ===" << endl;
// TODO: Create ASCII art using multiple cout statements
// Example (a simple house):
// /\
// / \
// /----\
// | |
// | [] |
// |____|
// Write your code below:
}
// ============================================================
// EXERCISE 3: Escape Sequences
// Difficulty: ⭐ (Beginner)
//
// Task: Print a formatted receipt using escape sequences.
// Use \t for tabs and \n for new lines.
// ============================================================
void exercise3() {
cout << "\n=== EXERCISE 3: Escape Sequences ===" << endl;
// TODO: Create a formatted receipt like this:
// ================================
// STORE RECEIPT
// ================================
// Item Qty Price
// --------------------------------
// Apple 3 $1.50
// Bread 1 $2.99
// Milk 2 $3.50
// --------------------------------
// Total: $7.99
// ================================
//
// Use \t for alignment and \n for new lines
// Write your code below:
}
// ============================================================
// EXERCISE 4: Namespace Creation
// Difficulty: ⭐⭐ (Intermediate)
//
// Task: Create your own namespace called "MyInfo" with:
// - A constant for your birth year
// - A constant for your country
// - A function that prints your info
// ============================================================
// TODO: Create the MyInfo namespace here (before the function)
// namespace MyInfo {
// // Your constants and function here
// }
void exercise4() {
cout << "\n=== EXERCISE 4: Namespace Creation ===" << endl;
// TODO: Use your namespace to print information
// Example: MyInfo::printInfo();
// cout << "Born in: " << MyInfo::BIRTH_YEAR << endl;
// Write your code below:
cout << "(Complete the namespace above, then uncomment the code below)" << endl;
}
// ============================================================
// EXERCISE 5: Compilation Exploration
// Difficulty: ⭐⭐ (Intermediate)
//
// Task: Use preprocessor macros to display compilation info.
// Also, create a DEBUG flag that shows extra messages.
// ============================================================
// TODO: Define a DEBUG macro (1 for on, 0 for off)
// #define DEBUG 1
void exercise5() {
cout << "\n=== EXERCISE 5: Compilation Exploration ===" << endl;
// TODO: Print the following compilation information:
// 1. The current file name (__FILE__)
// 2. The current line number (__LINE__)
// 3. The compilation date (__DATE__)
// 4. If DEBUG is on, print "Debug mode enabled"
// Write your code below:
}
// ============================================================
// EXERCISE 6: Calculator Preview
// Difficulty: ⭐⭐ (Intermediate)
//
// Task: Create a simple calculator that adds two numbers.
// Declare the numbers, add them, and display the result.
// ============================================================
void exercise6() {
cout << "\n=== EXERCISE 6: Calculator Preview ===" << endl;
// TODO:
// 1. Declare two integer variables with values
// 2. Declare a third variable to store the sum
// 3. Calculate the sum
// 4. Display: "X + Y = Z" format
// Write your code below:
}
// ============================================================
// EXERCISE 7: Multiple Output Formats
// Difficulty: ⭐⭐ (Intermediate)
//
// Task: Display the same information in different formats.
// Show how data can be presented in various ways.
// ============================================================
void exercise7() {
cout << "\n=== EXERCISE 7: Multiple Output Formats ===" << endl;
string product = "Laptop";
double price = 999.99;
int quantity = 5;
// TODO: Display this information in 3 different formats:
//
// Format 1 (Simple):
// Product: Laptop, Price: 999.99, Quantity: 5
//
// Format 2 (Detailed):
// ┌─────────────────────────┐
// │ Product Details │
// ├─────────────────────────┤
// │ Name: Laptop │
// │ Price: $999.99 │
// │ Quantity: 5 │
// └─────────────────────────┘
//
// Format 3 (One item per line):
// === PRODUCT INFO ===
// Name: Laptop
// Price: $999.99
// Quantity: 5
// ====================
// Write your code below:
}
// ============================================================
// EXERCISE 8: Constants and Calculations
// Difficulty: ⭐⭐⭐ (Advanced)
//
// Task: Create a program that calculates various circle properties.
// Use proper constants and display formatted output.
// ============================================================
// TODO: Define PI as a constant using the 'const' keyword
// const double PI = 3.14159265359;
void exercise8() {
cout << "\n=== EXERCISE 8: Circle Calculator ===" << endl;
double radius = 7.5;
// TODO:
// 1. Calculate the circle's diameter (2 * radius)
// 2. Calculate the circle's circumference (2 * PI * radius)
// 3. Calculate the circle's area (PI * radius * radius)
// 4. Display all results with descriptive labels
// Write your code below:
}
// ============================================================
// EXERCISE 9: Program Documentation
// Difficulty: ⭐⭐ (Intermediate)
//
// Task: Write a well-documented function that converts
// temperature from Celsius to Fahrenheit.
// ============================================================
// TODO: Write the function with proper documentation comments
// Use this format:
/**
* @brief [What the function does]
* @param celsius [Description of input]
* @return [Description of output]
*
* Formula: F = (C × 9/5) + 32
*/
// double celsiusToFahrenheit(double celsius) {
// // Your code here
// }
void exercise9() {
cout << "\n=== EXERCISE 9: Temperature Converter ===" << endl;
// TODO: Test your function with these values:
// 0°C, 100°C, 37°C (body temperature), -40°C
// Example output:
// 0°C = 32°F
// 100°C = 212°F
// 37°C = 98.6°F
// -40°C = -40°F
// Write your code below:
cout << "(Complete the function above, then write test code here)" << endl;
}
// ============================================================
// EXERCISE 10: Complete Program
// Difficulty: ⭐⭐⭐ (Advanced)
//
// Task: Create a complete "About Me" program that demonstrates
// everything you've learned:
// - Proper structure
// - Constants and variables
// - Namespaces
// - Formatted output
// - Comments
// ============================================================
void exercise10() {
cout << "\n=== EXERCISE 10: Complete About Me Program ===" << endl;
// TODO: Create a comprehensive "About Me" display that includes:
//
// 1. A decorative header (ASCII art or box drawing)
// 2. Your personal information:
// - Name
// - Age (calculate from birth year if you want)
// - Location
// - Hobbies (at least 3)
// - Programming experience
// 3. Your goals:
// - What you want to build with C++
// - Where you see yourself in 1 year
// 4. A decorative footer
//
// Use proper formatting with tabs and newlines
// Include comments explaining your code
// Write your code below:
}
// ============================================================
// BONUS EXERCISE: Interactive Version (Optional)
// Difficulty: ⭐⭐⭐ (Advanced)
//
// Task: Make exercise 10 interactive using cin to get
// user input instead of hardcoded values.
//
// Note: This exercise requires keyboard input when running.
// ============================================================
void bonusExercise() {
cout << "\n=== BONUS: Interactive About Me ===" << endl;
// TODO: Use cin to get input from the user
// Example:
// string name;
// cout << "Enter your name: ";
// cin >> name; // Note: Only reads one word
// getline(cin, name); // Reads entire line including spaces
// Uncomment and complete if you want to try:
/*
cout << "This exercise requires keyboard input." << endl;
string name;
int age;
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your age: ";
cin >> age;
cout << "\nHello, " << name << "!" << endl;
cout << "You are " << age << " years old." << endl;
*/
cout << "(Uncomment the code above to try the interactive version)" << endl;
}
// ============================================================
// MAIN FUNCTION
// ============================================================
int main() {
cout << "╔══════════════════════════════════════════════════════╗" << endl;
cout << "║ C++ Introduction - Exercises ║" << endl;
cout << "║ Complete the TODO sections in each exercise ║" << endl;
cout << "╚══════════════════════════════════════════════════════╝" << endl;
// Run all exercises
exercise1();
exercise2();
exercise3();
exercise4();
exercise5();
exercise6();
exercise7();
exercise8();
exercise9();
exercise10();
bonusExercise();
cout << "\n╔══════════════════════════════════════════════════════╗" << endl;
cout << "║ Exercises Complete! ║" << endl;
cout << "║ ║" << endl;
cout << "║ Next Steps: ║" << endl;
cout << "║ 1. Check your solutions compile without errors ║" << endl;
cout << "║ 2. Verify output is formatted correctly ║" << endl;
cout << "║ 3. Move on to: 02_Syntax_And_Structure ║" << endl;
cout << "╚══════════════════════════════════════════════════════╝" << endl;
return 0;
}
// ============================================================
// ANSWER KEY (For reference - try on your own first!)
// ============================================================
/*
* Exercise 1 Solution:
* string name = "John";
* string favLang = "Python";
* string reason = "game development";
* cout << "Hello! My name is " << name << endl;
* cout << "My favorite language is " << favLang << endl;
* cout << "I want to learn C++ because " << reason << endl;
*
* Exercise 6 Solution:
* int a = 15;
* int b = 27;
* int sum = a + b;
* cout << a << " + " << b << " = " << sum << endl;
*
* (Other solutions are left for you to figure out!)
*/