---
# System prepended metadata

title: C++ Week 02 array and vector
tags: [c++_beginner]

---

###### tags: `c++_beginner`
# Week 02
> 這周比較輕鬆哈哈，線上題共4題，也是基礎。

教學影片的部分因為進度不同，請依照標題和自身情況跳著看:)。(不會全部4小時看完啦)
[Link](https://www.youtube.com/watch?v=vLnPwxZdW4Y&t=9526s)
<iframe width="560" height="315" src="https://www.youtube.com/embed/vLnPwxZdW4Y?si=jH-Mr9SjWjow0Ku0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>


## Array
### 講解
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store:
```cpp=
string cars[4];
```
Initialized
```cpp=
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
```
To create an array of three integers, you could write:
```cpp=
int myNum[3] = {10, 20, 30};
```

### 題目
1. [Arrays Introduction](https://www.hackerrank.com/challenges/arrays-introduction/problem?isFullScreen=true)

## Vector
### 講解
C++ vector 是一個可以改變陣列大小的序列容器。**C++ vector 是陣列的升級版**，主要因為 vector 能高效地對記憶體進行管理以及動態增長。vector 其實就是將陣列和方法封裝形成的一個類別...

看不懂嗎?呵呵，我也是，大概只有粗體那段比較好懂，**J個影片裡沒有**，不過後面的某一周會再來一遍，所以這周就先學一點幸福快樂簡單的東西吧~

首先，我們來初始化一個簡單的vector吧~
```cpp=
vector<int> v;
```
沒了?!對，沒了呵呵。不用宣告大小:)超快樂。
你要也行啦...
在 vector 建構子帶入數量 n 會初始化 n 個元素且預設初始值為 0。
```cpp=
vector<int> v(3);//v[0]=0,v[1]=0,v[2]=0
```
二維陣列的部分
```cpp=
vector<vector<int>> m(5)
```
初始化子陣列
```cpp=
for(int i=0;i<5;i++){
    m[i].resize(len);//len為該列的大小
}
```
Reference:
* [C++ std::vector 用法與範例](https://shengyu7697.github.io/std-vector/)
* [C++使用vector创建二维数组的方法](https://blog.csdn.net/Mocode/article/details/122794762#:~:text=C%2B%2B%20%E5%8F%AF%E4%BB%A5%E7%9C%8B%E4%BD%9C%E6%98%AF%E4%B8%80%E4%B8%AA%E7%94%B1%E5%A4%9A%E4%B8%AA%E6%95%B0%E7%BB%84%E6%9E%84%E6%88%90%E7%9A%84%E6%95%B0%E7%BB%84%EF%BC%8C%E8%80%8C%20vector%20%E6%98%AF%20C%2B%2B%20STL%E6%8F%90%E4%BE%9B%E7%9A%84%E4%B8%80%E7%A7%8D%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84%EF%BC%8C%E5%8F%AF%E4%BB%A5%E8%87%AA%E5%8A%A8%E6%89%A9%E5%B1%95%E5%A4%A7%E5%B0%8F%E3%80%82%20%E5%9B%A0%E6%AD%A4%EF%BC%8C%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%20%E4%BD%BF%E7%94%A8vector,vector%20%E8%A1%A8%E7%A4%BA%20%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%20%E7%9A%84%E8%A1%8C%EF%BC%8C%E7%AC%AC%E4%BA%8C%E4%B8%AA%20vector%20%E8%A1%A8%E7%A4%BA%20%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%20%E7%9A%84%E5%88%97%E3%80%82)
### 題目
1. [Variable Sized Arrays](https://www.hackerrank.com/challenges/variable-sized-arrays/problem?isFullScreen=true)
## Functions
### 講解
基本上和C一模一樣。

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
```cpp=
// Create a function
void myFunction() {
  cout << "I just got executed!";
}

int main() {
  myFunction(); // call the function
  return 0;
}
```

### 題目
1. [Functions](https://www.hackerrank.com/challenges/c-tutorial-functions/problem?isFullScreen=true)
## Pointer
### 講解
跟C一樣(甚至線上題的預設標頭檔是stdio.h哈哈)。我就直接把解釋貼過來啦。
A pointer in C++ is used to share a memory address among different contexts (primarily functions). They are used whenever a function needs to modify the content of a variable, but it does not have ownership.

In order to access the memory address of a variable,***val***, prepend it with ***&*** sign. For example, ***&val*** returns the memory address of ***val***.

This memory address is assigned to a pointer and can be shared among functions. For example, ***int\*p = &val*** assigns the memory address of ***val*** to pointer ***p***. To access the content of the memory pointed to, prepend the variable name with a **\***. For example, ***\*p*** will return the value stored in  and any modification to it will be performed on ***val***.
### 題目
1. [Pointer](https://www.hackerrank.com/challenges/c-tutorial-pointer/problem?isFullScreen=true)


## Test
> 這次沒有vector，後面有時間可以深入了解。
> 但別擔心!題目還是可以出:)
> 然後題目都是我自己邊寫邊出的，所以放心，沒有解不出來的問題，啾咪ε٩(๑> ₃ <)۶з
### 01 : 又捲
寒假就是要捲起來，請輸入一數n，並再輸入一串有n\*n個數字的一維陣列，並把他捲起來變成一個二維陣列:)。
> *Hint:*
> cout的對齊方法。
> 先include:
> ```cpp=
> #include<iomanip>
> ```
> 然後召喚他~
> ```cpp=
> cout<<setw(3)<<arr[i][j];
> ```
Sample01:
Input
```
3
9 8 5 6 3 2 1 4 7
```
Output
```
  9  8  5
  4  7  6
  1  2  3
```
Sample02:
Input
```
4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
```
Output
```
  1  2  3  4
 12 13 14  5
 11 16 15  6
 10  9  8  7
```
### 02: 報靶大師
由於邦邦手斷掉不能射箭，所以她在射箭場只能幫忙報靶QQ。
輸入一數n，代表該靶從幾分開始。並輸入六支箭的座標(x,y)。
然後依序報靶。
* '+'為擊中的點，'*'為靶的邊框。
* 不能使用全域變數。
* 0<n<9
* -1<x,y<(11-n)*2+1
* 分數等於X或10不須報方位。
* 請務必使用function，這樣才可以練習到。
方位:
![IMG_0064](https://hackmd.io/_uploads/rk88PtBv6.png)
> *Hint:*
> 畫圓方法->
> ![image](https://hackmd.io/_uploads/HJdB_qSwa.png)
>>
> ```cpp=
> void drawCircle(int radius) {
>     for (int y = -radius; y <= radius; y++) {
>         for (int x = -radius; x <= radius; x++) {
>             if (fabs(x * x + y * y - radius * radius) < radius * 0.6) {
>                 cout<<" *";
>             }else{
>             	cout<<"  ";
> 			}
>         }
>         cout<<"\n";
>     }
> }
> ```
> 請自行修改成需要的格式:)
> [如何在 C++ 中將 2D 陣列傳遞給函式](https://www.delftstack.com/zh-tw/howto/cpp/how-to-pass-2d-array-to-function-in-cpp/#%e4%bd%bf%e7%94%a8%e7%ac%a6%e8%99%9f%e5%82%b3%e9%81%9e-2d-%e9%99%a3%e5%88%97%e4%bd%9c%e7%82%ba%e5%87%bd%e5%bc%8f%e5%bc%95%e6%95%b8)
    
Input
```
5
10 9
2 8
7 6
11 11
6 6
3 10
```
Output
```
           * * *
       *           *
                 +
   *                 + *

 *                       *
 *           +10 9 8 7 6 5
 *           +           *

   *                   *
                   +
       *           *   +
           * * *
1->Direction:  5|Score:  6
2->Direction:  1|Score:  6
3->Direction:   |Score: 10
4->Direction:  4|Score:  M
5->Direction:   |Score:  X
6->Direction:  2|Score:  6
```
如果是空的靶應該長這樣:
```
           * * *
       *           *

   *                   *

 *                       *
 *           X10 9 8 7 6 5
 *                       *

   *                   *

       *           *
           * * *
```
{%hackmd @25H8NTF5SwujU3QUSA0oog/BkiNnsk56 %}