Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: Google
IndentWidth: 4
BreakBeforeBraces: Attach
CommentStyle: Aligned
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Environments
.env
.envrc
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Linters / Formatters
.ruff_cache/
51 changes: 51 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"files.associations": {
"iostream": "cpp",
"*.tcc": "cpp",
"limits": "cpp",
"ostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"new": "cpp",
"numbers": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"fstream": "cpp",
"map": "cpp"
}
}
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
68 changes: 68 additions & 0 deletions CPP/Beginner/00. Getting started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Exercise 0: Hello World

## Objective

Getting familiarized with C++ files, their coding style and building the executable.

## Task

Create a file named 'hello_world.cpp'. The file must:

- Have a single function `int main() { }` which returns 0 at the end.
- Print through the terminal 'Hello world!'.

Once created, build the program and execute it.

## Hints

- Include the standard library `<iostream>`.
- Use `sdt::cout << "My message"` to print a message through the terminal.
- End the terminal use with `std::endl`.
- You can use `using namespace std` to use `cout` instead of `std::cout`.

# C++ theory
## Coding in C++

C++ files have an extension `.cpp` or `.cc`.
Inside, the code is defined by the following (basic) rules:

- The executable file usually has a `main()` function, which returns `0` when has run without problems.
- The body of any block is defined by the keys `{ }`.
- Every line command ends with `;`.
- Libraries are included/imported into the code as `#include <std_lib_name>` (for standard libraries) or as `#include "lib_name.h"` for header files.
- Standard calls are defined as `std::CALL`, for example `std::string` or `std::cout`.

## Compiling & building

Assuming a file `main.cpp`, the code is compiled as the following:

```
g++ main.cpp
```

This creates a file `a.exe`, which can be called through the command line to execute it.
It is also possible to create an executable with a custom name:

```
g++ -o my_program main.cpp
```

With the `-o` parameter, the output `my_program.exe` is created.

# Environment setup

This is a guide to setup your end to be able to code in C++.
For it, you will need:

- **Code editor**: We will use Visual Studio Code.
- **Make**: If you are using Linux, you already have it. This has to be installed for Windows.

## Code editor (VS Code)

The first step is to have a code editor.
At this repo, I will continue with the VisualStudio code.
You can download it from the [official website](https://code.visualstudio.com/download).
Once downloaded, install is but don't open it yet.

## Make for Windows

9 changes: 9 additions & 0 deletions CPP/Beginner/00. Getting started/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

// using namespace std;
using std::cout, std::endl;

int main() {
cout << "Hello World!" << endl;
return 0;
}
42 changes: 42 additions & 0 deletions CPP/Beginner/01. Basic IO/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Exercise 1: Basic Input and Output

## Objective
Learn how to take user input and display it using `std::cin` and `std::cout`.

## Task
Write a program that:
- Asks the user to input their name.
- Asks the user to input their age.
- Outputs a message saying: "Hello, [Name]! You are [Age] years old."

## Expected Result
If the user inputs "Alice" as their name and "30" as their age, the program should output:
```commandline
Hello, Alice! You are 30 years old.
```

# C++ theory
## Using the console

To use the terminal in C++ it is necessary to include the `iostream` library.

```cpp
#include <iostream>
```

This library allows to use `std::cin` (console input) and `std::cout` (console output).
The symbols `<<` are used for printing through the console output, and `>>` to obtain data through the console input.

```cpp
int number;

std::cout << "Write a number: ";
std::cin >> number;
std::cout << "You wrote the number " << number << std::endl;
```

The manipulator `std::endl` writes the special character `\n` (new line) and flushes the buffer.
In other words, the output is inmediately shown on the terminal.

Writing `std::endl` and `\n` are different, as `\n` does not flush the buffer and it is faster.
Try to use `std::endl` only when the output is to be shown immediately or the code is done using the console.
16 changes: 16 additions & 0 deletions CPP/Beginner/01. Basic IO/greetings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <string>

int main() {
int age;
std::string name;

std::cout << "What is your name?" << std::endl;
std::cin >> name;
std::cout << "How old are you?" << std::endl;
std::cin >> age;

std::cout << "Hello, " << name << "! You are " << age << " years old."
<< std::endl;
return 0;
}
85 changes: 85 additions & 0 deletions CPP/Beginner/02. Basic operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Exercise 2: Basic Arithmetic Operations
## Objective
Learn how to perform basic arithmetic operations and work with different data types.

## Task
Write a program that:

- Asks the user to input two integers.
- Performs the following operations on those integers:
- Addition
- Subtraction
- Multiplication
- Division
- Outputs the result of each operation in a readable format.

## Expected Result
If the user inputs 10 and 5 as the two integers, the program should output:

```commandline
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
```

# C++ Theory

## Types of variables

When defining a variable, the type of this one must be specified before its name.
Then, the variable must be initialized (its value must be set to a known value) before it is used.

```cpp
#include <string>

int number; // integer variable, not initialized.
std::string dayOfTheWeek = "Monday"; // string variable, initialized
```

Some of the **basic types** for variables are:

- `int`: Defines signed integers.
- `unsigned int`: Defines unsigned integers.
- `float`: A single-precision floating-point number (usually 32 bits).
- `double`: A double-precision floating-point number (usually 64 bits).
- `char`: A character type, usually 8 bits, which can represent ASCII values.
- `bool`: Represents a boolean, either 'true' or 'false'.
- `void`: Represents the absence of type.

From C++11 and onwards, there are **fixed-width integer types** using the library `<cstdint>`:

- `int8_t, int16_t, int32_t, int64_t`: Signed integers of fixed width (8, 16, 32, and 64 bits).
- `uint8_t, uint16_t, uint32_t, uint64_t`: Unsigned integers of fixed width.

There exist also **standard types**, importing the correct library.
For example `std::string` can be defined when including `<string>`.

## Operations with variables

The **basic operations** that can be perfomed in C++ are:

- Sum (`+`): `int a = 1 + 2;`. As a result, `a = 3`.
- Substraction (`-`): `int a = 1 - 2;`. As a result, `a = -1`.
- Multiplication (`*`): `int a = 1 * 2;`. As a result, `a = 2`.
- Division (`/`): `float a = 1 / 2;`. As a result, `a = 0.5`.
- Modulus/Reminder (`%`): `int a = 1 % 2`. As a result, `a = 1` (reminder of the division 1 / 2).

These operation can be directly assigned over variables.

- `int a += 2;` is equivalent to `int a = a + 2;`
- `int a -= 2;` is equivalent to `int a = a - 2;`
- `int a *= 2;` is equivalent to `int a = a * 2;`
- `float a /= 2;` is equivalent to `int a = a / 2;`
- `int a %= 2;` is equivalent to `int a = a % 2;`

When defining these operations with numbers, each number also has a type.
If a type is not define, these are the **literal types**:

- In `a + 2`, the number '2' is a literal integer (type `int`).
- In `a / 2.0`, the number '2.0' is a literal decimal point (type `double`).

It is of good practise to always define the type of the values you work with:

- `2u` defines that '2' is type `unsigned`.
- `2.0f` defines that '2' is type `float`.
17 changes: 17 additions & 0 deletions CPP/Beginner/02. Basic operations/farenh2celsius.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Here mathematic operations are performed over a value given by the user. */

#include <iostream>

int main() {
float temp_f;
float temp_c;

// Without 'std::endl' the user writes right next to the sentence:
std::cout << "Enter a temperature in fahrenheit: ";
std::cin >> temp_f;

temp_c = (temp_f - 32u) * 5.0f / 9.0f;
std::cout << "Your temperature in celsius is " << temp_c << std::endl;

return 0;
}
Loading