Adding exercises

This commit is contained in:
Mikael Johansson 2026-06-15 09:18:54 +02:00
parent 9012a85e30
commit d4ad08dc4c
3 changed files with 301 additions and 0 deletions

102
cpp-exercise.md Normal file
View File

@ -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:** ~1015 minutes
---
### Step 1 — Write the program
```bash
cd ~/my-project
nano greet.cpp
```
Type this in:
```cpp
#include <iostream>
#include <string>
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`

119
git-exercise.md Normal file
View File

@ -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:** ~1015 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

80
wsl-ubuntu-exercise.md Normal file
View File

@ -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