This commit is contained in:
2025-12-10 21:50:49 +03:30
parent 0e20e216c7
commit 37ba8ab075
6 changed files with 81 additions and 440 deletions
@@ -1,19 +0,0 @@
## ❓ Question 1
> Question: _“Whats the difference between `int` and `float` in C++?”_
> [!info] **Answer**
`int` stores whole numbers without decimals, while `float` stores numbers with fractional parts.
For example:
> - `int x = 5;`
> - `float y = 5.25;`
---
### ❓ Question 2
> Question: _“Why do I need to use semicolons in C++?”_
> [!info] Answer
> Semicolons mark the end of a statement in C++. They help the compiler understand where one command finishes and the next begins. Without it, the compiler throws an error.
@@ -0,0 +1,57 @@
> [!info] Brief description
> This page lists the **assignment questions** for this topic and provides **downloadable official solutions**.
> These questions are meant to help you apply the concepts learned in class through hands-on coding exercises.
---
> [!tip] How to use this page
> - Start by attempting each question on your own.
> - Use the official solutions only to review and learn better approaches.
> - If something doesnt make sense, **ask a TA**.
---
## 📦 Download All Questions and Solutions
- **All questions (zip):** [Download all questions]()
- **All official solutions (zip):** [Download all official solutions]()
---
## 🧾 Official Questions & Solutions
| Question # | Question (Download) | Official Solution (Download) | Additional Notes |
| ---------- | ------------------- | ---------------------------- | ---------------- |
| 01 | [Download PDF]() | [Download Solution (zip)]() | |
| 02 | [Download PDF]() | [Download Solution (zip)]() | |
| 03 | [Download PDF]() | [Download Solution (zip)]() | |
| 04 | [Download PDF]() | [Download Solution (zip)]() | |
| 05 | [Download PDF]() | [Download Solution (zip)]() | |
| 06 | [Download PDF]() | [Download Solution (zip)]() | |
| 07 | [Download PDF]() | [Download Solution (zip)]() | |
| 08 | [Download PDF]() | [Download Solution (zip)]() | |
| 09 | [Download PDF]() | [Download Solution (zip)]() | |
| 10 | [Download PDF]() | [Download Solution (zip)]() | |
> [!note] Not available yet?
> Some solutions will be uploaded after the workshop or assignment deadline. Check back later or ask your TA.
---
## ❓ Q&A and Support
> [!question] Got stuck or confused?
> If something in a question or solution is unclear:
> - Revisit the lecture notes and workshop slides.
> - Ask your TA — thats what were here for.
- [Topic Q&A]()
- [Professor Slides](01-topics/03-conditions-loops/4-ProfessorSlides.md)
- [TA Slides](01-topics/03-conditions-loops/5-TASlides.md)
---
## ⚖️ Licensing & Usage
> [!important] Usage note
> Official assignment solutions are for **educational use only**. Please do not repost them publicly or use them in ways that violate course policy.
-421
View File
@@ -1,421 +0,0 @@
## ❓ Question 1
> Question: _“Where are Variable Length Arrays (arrays sized using a runtime variable) allowed in C++ and where are they not?”_
> [!info] **Answer**
> Variable Length Arrays (**VLAs**) are **not allowed** in standard C++.
> C++ requires the size of a built-in array to be a **constant expression** known at compile time.
-**Not acceptable in standard C++** (C++11/14/17/20/23/…):
```cpp
int n = 5;
int arr[n]; // Not valid C++
```
- ✔️ **Acceptable in standard C** (since C99): C allows VLAs.
- ✔️ **Allowed in GCC as an extension**, but **not portable** and not standard.
Reference:
- GCC documentation: [https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html)
- C++ restriction explanation: [https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/](https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/)
---
## ❓ Question 2
> Question: _“Is it valid to use a Variable Length Array when compiling with GCC? In which situations is it not valid?”_
> [!info] **Answer**
> GCC **does allow** VLAs in C++ mode, but **only as a non-standard compiler extension**.
✔️ **OK in GCC when:**
- Used inside functions (automatic storage):
```cpp
int n = 10;
int arr[n]; // GCC accepts, but non-standard
```
❌ **Not OK when:**
- Writing portable or standards-compliant C++
- Using MSVC, Clang with strict mode, or compilers that reject VLAs
- Using strict flags:
- `-std=c++20 -pedantic`
- `-Wvla`
- `-Werror`
GCC reference:
[https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html)
---
## ❓ Question 3
> Question: _“Why are Variable Length Arrays considered unacceptable or non-standard in C++?”_
> [!info] **Answer**
> VLAs are rejected by the C++ standard for several reasons:
1. **Compile-time determinism**
C++ requires array sizes to be known at compile time for built-in arrays.
2. **Portability issues**
Many compilers (MSVC, strict Clang modes) do not support VLAs at all.
3. **Safety considerations**
Runtime-sized stack arrays risk unpredictable stack usage and stack overflow.
4. **C++ philosophy**
C++ encourages using **RAII containers** (`std::vector`, `std::array`) instead of raw arrays with runtime sizes.
Reference:
[https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/](https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/)
---
## ❓ Question 4
> Question: _“Can `const` or `constexpr` be used to provide a size for arrays in C++ when the goal is to avoid Variable Length Arrays?”_
> [!info] **Answer**
> Yes — **but only if the value is a compile-time constant expression**.
✔️ Allowed:
```cpp
constexpr int N = 10;
int arr[N]; // Valid C++
```
⚠️ Not allowed:
```cpp
int n = get_input();
const int x = n;
int arr[x]; // Still NOT valid — not a constant expression
```
Summary:
- `constexpr` → always compile-time constant → valid for array sizes
- `const` → **not enough** unless initialized with a compile-time constant
- For real runtime sizes, use `std::vector`.
Reference:
[https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array)
## ❓ Question
> Question: _“How are arrays passed to functions in C++? Is it allowed?”_
> [!info] **Answer**
> Yes, it is allowed.
> In C++, when you pass a built-in array to a function, the array **decays into a pointer** to its first element.
> Example:
```cpp
void foo(int arr[]) { } // arr becomes int*
void foo(int* arr) { } // same thing
```
So even though an array looks like it's being passed, **the function receives only a pointer**, not the entire array.
Reference: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array)
---
## ❓ Question 2
> Question: _“Are arrays passed by reference or by value?”_
> [!info] **Answer**
> A raw array parameter is **never** passed by value.
> The array **decays** to a pointer → this behaves like **pass-by-pointer**, not value.
- ❌ Not pass-by-value (copying an entire array is not what happens)
- ✔️ Effectively pass-by-reference (because the pointer can modify the original array)
If you _do_ want pass-by-value semantics, you must use something like:
```cpp
void foo(std::array<int, 5> arr); // Copies the entire array
```
Reference: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array)
---
## ❓ Question 3
> Question: _“Can you control this behavior? Can you pass arrays by value or by reference explicitly?”_
> [!info] **Answer**
> Yes — but **only using references or std::array**.
✔️ **Pass entire array by reference** (preserves size):
```cpp
void foo(int (&arr)[5]); // Reference to array of 5 ints
```
✔️ **Pass entire array by value** (copies the whole array):
```cpp
void foo(std::array<int,5> arr); // Copy
```
✔️ **Pass entire array by reference (modern way)**:
```cpp
void foo(const std::array<int,5>& arr);
```
❌ You **cannot** pass a built-in array by value directly.
The syntax does not exist in C++.
Reference: [https://en.cppreference.com/w/cpp/language/references](https://en.cppreference.com/w/cpp/language/references)
---
## ❓ Question 4
> Question: _“Should you specify the size of arrays when passing them? For 1D, 2D, 3D, etc.?”_
> [!info] **Answer**
> It depends on the declaration style.
### ⭐ 1D arrays
You do **not** specify the size:
```cpp
void foo(int arr[]); // OK
void foo(int* arr); // Same
```
### ⭐ Multi-dimensional (2D, 3D, …) arrays
All **inner dimensions must be known**:
```cpp
void foo(int arr[][5]); // OK
void foo(int arr[3][5]); // OK
void foo(int arr[][5][10]); // Higher dimensions OK
```
But the **first dimension** may be left unspecified because it becomes a pointer:
```
arr → pointer to an array of 5 ints
```
Reference: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array)
---
## ❓ Question 5
> Question: _“Can you use variables as sizes in function parameters? In standard C++ and GCC?”_
> [!info] **Answer**
### ✔️ Standard C++
- **NOT allowed**:
Inner dimensions must be compile-time constants.
```cpp
void foo(int arr[][n]); // ❌ Not standard C++
```
### ✔️ GCC (as extension)
- GCC allows **Variable Length Arrays (VLA)** in function parameters.
```cpp
void foo(int n, int arr[][n]); // ✔️ GCC extension
```
But this is **non-portable** and not valid standard C++.
References:
- Standard rule: [https://en.cppreference.com/w/cpp/language/array](https://en.cppreference.com/w/cpp/language/array)
- GCC VLA extension: [https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html)
---
## ❓ Question 6
> Question: _“What are the different notations for passing arrays to functions?”_
> [!info] **Answer**
> C++ supports several styles:
### 1️⃣ Pointer style
```cpp
void foo(int* arr);
```
### 2️⃣ Array style (decays to pointer)
```cpp
void foo(int arr[]);
void foo(int arr[10]); // Size ignored by compiler
```
### 3️⃣ Multi-dimensional
```cpp
void foo(int arr[][5]);
```
### 4️⃣ Reference to array (preserves actual size)
```cpp
void foo(int (&arr)[10]);
```
### 5️⃣ Using std::array (recommended for fixed sizes)
```cpp
void foo(std::array<int,10>& arr);
```
### 6️⃣ Using std::vector (recommended for dynamic sizes)
```cpp
void foo(std::vector<int>& arr);
```
Reference: [https://en.cppreference.com/w/cpp/container](https://en.cppreference.com/w/cpp/container)
## ❓ Question: Can you somehow preserve an arrays size and use it in functions?
> **Answer**
> Yes — but only if you pass the array in a way that preserves its compile-time size. By default, when you pass a built-in C-style array to a function, it “decays” into a pointer (so size information is lost). ([GeeksforGeeks](https://www.geeksforgeeks.org/cpp/what-is-array-decay-in-c-how-can-it-be-prevented/?utm_source=chatgpt.com "What is Array Decay in C++? How can it be prevented? - GeeksforGeeks"))
If you want to preserve the size, you can pass by reference to an array. Example:
```cpp
void func(int (&arr)[10]) {
// Here, sizeof(arr)/sizeof(arr[0]) works: size = 10.
}
```
Because `arr` is a reference to an array of 10 ints, the function knows the arrays size at compile time. ([GeeksforGeeks](https://www.geeksforgeeks.org/cpp/pass-array-to-functions-in-cpp/?utm_source=chatgpt.com "Pass Array to Functions in C++ - GeeksforGeeks"))
If you need a function that works for arrays of _any_ compile-time-known size, you can use a template:
```cpp
template <size_t N>
void func(int (&arr)[N]) {
// N is deduced; you can use it inside the function
}
```
This way the function “remembers” the array size. This pattern avoids the “decay to pointer” problem. ([CodeArchPedia.com](https://openillumi.com/en/en-cpp-c-array-decay-type-size-basics/?utm_source=chatgpt.com "C++ Array Decay: Types, Sizes, and Fixing Pointer Conversion Bugs - CodeArchPedia.com"))
Alternatively, many modern C++ codebases avoid raw arrays and instead use container types (e.g. `std::array`, `std::vector`) which carry size information or allow querying their size. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
---
## ❓ Question: How to initialize an array to all zeros? — in 1D, 2D, 3D, etc.
> **Answer**
- For a 1D built-in array in C++, you can zero-initialize like:
```cpp
int arr[5] = {0}; // all 5 elements become 0
```
([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology"))
- For multi-dimensional (2D, 3D, ...) built-in arrays, you can do nested brace initialization (or rely on partial zero initialization). For example:
```cpp
int mat[3][4] = {0}; // all elements become 0
```
Or for 3D:
```cpp
int cube[2][3][4] = {0};
```
The `{0}` initializes the first element to zero, and all other elements are zero-initialized by default (aggregate initialization rules). ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology"))
- Similarly, if you use `std::array`, you can zero-initialize:
```cpp
std::array<int,5> arr = {0};
```
All members will be zero-initialized. ([magodo's blog](https://magodo.github.io/array-string-pointer-reference/?utm_source=chatgpt.com "C++ Array, String, Pointer and Reference"))
So yes — you _can_ initialize multi-dimensional arrays (1D, 2D, 3D, etc.) to zero with a simple initializer (or nested braces).
---
## ❓ Question: Why is there no compile-time error when you access out-of-bounds in a built-in array (i.e. “out of index”)?
> **Answer**
> Because built-in (C-style) arrays in C++ do **not** perform any bounds checking. The language simply allows `arr[i]` for any `i`, and does not verify at runtime whether `i` is valid. Accessing outside the allocated bounds is undefined behavior. ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology"))
That means the compiler does not generate an error or warning (in general) if you index beyond the arrays size — it's up to the programmer to ensure correctness. This is a known risk of raw arrays. ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow"))
Because of this inherent unsafety (lack of bounds checking, size information lost when passing arrays, no easy way to return raw arrays, etc.), many C++ developers prefer safer alternatives (see below). ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow"))
---
## ❓ Question: What is the “standard array” (i.e. `std::array`)? What are the differences between `std::array` and built-in arrays? When is it best to use it?
> **Answer**
> `std::array<T, N>` is a template class in C++ standard library representing a fixed-size array of `N` elements of type `T`. It behaves like a thin wrapper over a built-in array, but with advantages. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
**Differences / Advantages compared to built-in arrays:**
- **No “decay to pointer” when passed to functions**: `std::array` keeps size information. If you pass `std::array<int,5>` to a function by value or by reference, the size is known and preserved. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
- **Supports assignment, copy, move semantics**: Unlike built-in arrays (which are not assignable or copyable as a whole), `std::array` behaves like a regular object. ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow"))
- **Has member functions & safer access**: For example, `.at()` performs bounds checking (throws exception on invalid index), while operator `[]` still gives raw access (no bounds check) — unlike built-in arrays where you only get `[]`. ([magodo's blog](https://magodo.github.io/array-string-pointer-reference/?utm_source=chatgpt.com "C++ Array, String, Pointer and Reference"))
- **Interoperability with standard library algorithms**: `std::array` supports iterators, `std::begin()`, `std::end()`, which makes it easier to integrate with STL algorithms. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
**When is `std::array` best to use:**
- When the array size is known at compile time and fixed.
- When you want safer semantics — e.g. ability to copy/assign arrays, pass around by value or reference, avoid the “array decay” problems, and optionally get bounds-checked access via `.at()`.
- When you want to use standard library features (iterators, algorithms) with array data.
For dynamic or runtime-determined sizes (or if size can change), other types like `std::vector`, or (since C++20) `std::span` or dynamic containers are more appropriate. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
---
## What is string in c++? object? class?
## Are strings passed by ref ?
## Are strnigs objects that are in stack? in heap?
## Do strings use arrays in themselves?
## Are strings immutable? are they not?
@@ -0,0 +1,10 @@
## 🔗 Download
- [Download PDF - Lecture 06](https://drive.usercontent.google.com/u/0/uc?id=1Fc1h0H7tJclM_xRd7ETVEk3evZXMibPs&export=download)
## View PDF Online
### Lecture 06
<iframe src="https://docs.google.com/gview?embedded=true&url=https://drive.usercontent.google.com/u/0/uc?id=1Gqy7QpTBEPg2nktiScECRIiATLNotP8m&export=download" width="100%" height="700px" frameborder="0"> </iframe>
@@ -0,0 +1,7 @@
## 🔗 Download
- [Download PDF - Arrays & Strings](https://drive.usercontent.google.com/u/0/uc?id=1cxhamc82-V95CD5t48hTEqT609rtU0Lv&export=download)
## View PDF Online
### Arrays & Strings
<iframe src="https://docs.google.com/gview?embedded=true&url=https://drive.usercontent.google.com/u/0/uc?id=1cxhamc82-V95CD5t48hTEqT609rtU0Lv&export=download" width="100%" height="700px" frameborder="0"> </iframe>
@@ -0,0 +1,7 @@
## 🔗 Download
- [Download PDF - Functions Part 2 - Recursion](https://drive.usercontent.google.com/u/0/uc?id=1xlsj1je2iED_BpQtEYv0P_MX1bogqGG-&export=download)
## View PDF Online
### Functions Part 2 - Recursion
<iframe src="https://docs.google.com/gview?embedded=true&url=https://drive.usercontent.google.com/u/0/uc?id=1xlsj1je2iED_BpQtEYv0P_MX1bogqGG-&export=download" width="100%" height="700px" frameborder="0"> </iframe>