From d4ad08dc4c16789f91fb5b518c41a4586356556f Mon Sep 17 00:00:00 2001 From: Mikael Johansson Date: Mon, 15 Jun 2026 09:18:54 +0200 Subject: [PATCH] Adding exercises --- cpp-exercise.md | 102 +++++++++++++++++++++++++++++++++++ git-exercise.md | 119 +++++++++++++++++++++++++++++++++++++++++ wsl-ubuntu-exercise.md | 80 +++++++++++++++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 cpp-exercise.md create mode 100644 git-exercise.md create mode 100644 wsl-ubuntu-exercise.md diff --git a/cpp-exercise.md b/cpp-exercise.md new file mode 100644 index 0000000..a4c4b5f --- /dev/null +++ b/cpp-exercise.md @@ -0,0 +1,102 @@ +# C++ Exercise — "Hello, You!" + +**Goal:** Write, compile, and run a C++ program that asks for your name. Then automate the build with a Makefile. + +**Time:** ~10–15 minutes + +--- + +### Step 1 — Write the program + +```bash +cd ~/my-project +nano greet.cpp +``` + +Type this in: + +```cpp +#include +#include + +int main() { + std::string name; + std::cout << "What is your name? "; + std::getline(std::cin, name); + std::cout << "Hello, " << name << "!" << std::endl; + return 0; +} +``` + +Save: **Ctrl + O**, then **Enter** +Exit: **Ctrl + X** + +### Step 2 — Compile it + +```bash +clang++ -Wall greet.cpp -o greet +``` + +- `-Wall` turns on warnings (catches mistakes) +- `-o greet` names the output file `greet` + +If there are no errors, no message is printed — that's good! + +### Step 3 — Run it + +```bash +./greet +``` + +Type your name and press **Enter**. It should say hello back. + +### Step 4 — Create a Makefile + +```bash +nano Makefile +``` + +Type this: + +```make +CXX = clang++ +CXXFLAGS = -Wall -std=c++20 +TARGET = greet + +$(TARGET): greet.cpp + $(CXX) $(CXXFLAGS) greet.cpp -o $(TARGET) + +clean: + rm -f $(TARGET) + +.PHONY: clean +``` + +**Important:** The indented lines must use a **Tab** character, not spaces. + +Save and exit (**Ctrl + O**, **Enter**, **Ctrl + X**). + +### Step 5 — Build with make + +```bash +make clean +make +``` + +`make clean` deletes the old `greet` binary. `make` rebuilds it. + +### Step 6 — Run again (via make build) + +```bash +./greet +``` + +--- + +**Commands you used:** `nano`, `clang++`, `./` (run), `make`, `make clean` + +**What you learned:** +- Writing a basic C++ program with input and output +- Compiling with `clang++` and useful flags +- Creating a `Makefile` to automate the build +- Using `make` and `make clean` diff --git a/git-exercise.md b/git-exercise.md new file mode 100644 index 0000000..b2ac95a --- /dev/null +++ b/git-exercise.md @@ -0,0 +1,119 @@ +# Git Exercise — "Track a Poem" + +**Goal:** Turn a folder into a Git repo, make commits, experiment with branches, and merge. + +**Time:** ~10–15 minutes + +--- + +### Step 1 — Set up (do once on your machine) + +```bash +git config --global user.name "Your Name" +git config --global user.email "you@example.com" +``` + +### Step 2 — Create a project and turn it into a Git repo + +```bash +cd ~/my-project +git init +``` + +You should see: `Initialized empty Git repository...` + +### Step 3 — Create your first file and commit it + +```bash +echo "Roses are red" > poem.txt +git add poem.txt +git commit -m "Add first line of poem" +``` + +### Step 4 — Add a second line + +```bash +echo "Violets are blue" >> poem.txt +``` + +### Step 5 — See what changed + +```bash +git status +git diff +``` + +`git status` shows the file is modified. `git diff` shows exactly what lines changed. + +### Step 6 — Commit the change + +```bash +git add . +git commit -m "Add second line" +``` + +### Step 7 — View the history + +```bash +git log --oneline +``` + +You should see two commits. + +### Step 8 — Create a branch and switch to it + +```bash +git checkout -b spanish +``` + +This creates a branch named `spanish` and switches to it. + +### Step 9 — Add a Spanish version on this branch + +```bash +echo "Las rosas son rojas" > poem_es.txt +git add . +git commit -m "Add Spanish version" +``` + +### Step 10 — Switch back to main + +```bash +git checkout main +``` + +List the files — notice `poem_es.txt` is gone. It only exists on the `spanish` branch. + +```bash +ls +``` + +### Step 11 — Merge the branch + +```bash +git merge spanish +``` + +Now `poem_es.txt` is back on `main`. + +```bash +ls +git log --oneline +``` + +### Step 12 — Clean up the branch (optional) + +```bash +git branch -d spanish +``` + +--- + +**Commands you used:** `git config`, `git init`, `git add`, `git commit`, `git status`, `git diff`, `git log`, `git checkout`, `git merge`, `git branch` + +**What you learned:** +- Starting a Git repo +- Staging and committing changes +- Viewing history and differences +- Creating and switching branches +- Merging branches together diff --git a/wsl-ubuntu-exercise.md b/wsl-ubuntu-exercise.md new file mode 100644 index 0000000..21bb499 --- /dev/null +++ b/wsl-ubuntu-exercise.md @@ -0,0 +1,80 @@ +# WSL / Ubuntu Exercise — "My First Project" + +**Goal:** Create a project folder, add files, edit them, and explore. + +**Time:** ~10 minutes + +--- + +### Step 1 — Create a folder and go inside + +```bash +mkdir my-project +cd my-project +``` + +### Step 2 — Create a file with some text + +```bash +echo "Hello from WSL!" > hello.txt +``` + +Now look at what's in the folder: + +```bash +ls +``` + +### Step 3 — View the file + +```bash +cat hello.txt +``` + +### Step 4 — Edit the file + +```bash +nano hello.txt +``` + +Inside nano: +- Add a new line (e.g. `Today I am learning Linux commands.`) +- Save: **Ctrl + O**, then **Enter** +- Exit: **Ctrl + X** + +### Step 5 — See your changes + +```bash +cat hello.txt +``` + +### Step 6 — Create a backup folder and copy the file + +```bash +mkdir backup +cp hello.txt backup/ +ls backup/ +``` + +### Step 7 — List everything in detail + +```bash +ls -la +``` + +Notice the file sizes, dates, and permissions. + +### Step 8 — Clean up (go back home) + +```bash +cd ~ +``` + +--- + +**Commands you used:** `mkdir`, `cd`, `echo`, `ls`, `cat`, `nano`, `cp`, `ls -la` + +**What you learned:** +- Creating and navigating folders +- Creating, viewing, and editing files +- Copying files and listing details