Try   HackMD

多維陣列與字元

03.17 鄭余玄
2018 資訊之芽 語法班

複習陣列


一維陣列

int fib[6] = {1, 1, 2, 3, 5, 8}
[0] [1] [2] [3] [4] [5]
1 1 2 3 5 8

一維陣列操作

fib(i)=fib(i1)+fib(i2)
fib(0)=fib(1)=1

// 初始化邊界條件
int fib[6]= {1, 1};
// i 從 2 開始
for (int i = 2; i < 6; ++i)
    fib[i] = fib[i - 1] + fib[i - 2];

補充

  • 儲存空間不同
int a[1000000];

多維陣列


二維陣列

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


宣告方式

  • 資料型態 陣列名稱[][]
int table[9][9];
double matrix[100][50];
char names[10][100];

存取

  • 透過方括號中的索引值 (index) 存取
  • 索引值也可以是變數
  • 0 開始計數
int s[9][9];
s[1][1] = s[0][1] + s[1][0];
int i = 1, j = 2;
s[i][j] = 100;

初始化

int a2[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11};
int a2[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9 ,10, 11}};
int a3[3][4] = {};

99 乘法表

  1. 將答案存入 2 維陣列
  2. 讀入兩個整數
  3. 利用乘法表,查詢並且輸出答案
  • 挑戰:n * n 乘法表(!?

19 * 19 乘法表

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


補充:矩陣相乘

  • A
    m×n
    矩陣,B 為
    n×p
    矩陣,則他們的乘積
    AB
    會是一個
    m×p
    矩陣

(AB)ij=r=1nairbrj=ai1b1j+ai2b2j++ainbnj


三維?多維?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


字元


什麼是字元?

  • 英文字母
  • 數字
  • 特殊符號
  • #@sokjnbv$(qws*&)

宣告

// 宣告
char c;
// 初始化
char d = 'D';

ASCII 編碼

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


字元運算(!?

char c;
c = 'A';
cout << c << endl;

c = 95;
cout << c << endl; // ?

c = c + 1;
cout << c << endl; // ???

特殊字元

  • 不可見字元
    • 換行 '\n'
    • TAB '\t'
    • 字串結尾 '\0'
  • 用反斜線 \ 輸出特殊字元
    • \"
    • \'
    • \\