SlideShare a Scribd company logo
第五章 陣列與結構
前言
2
 如果要處理的變數數量十分龐大,應使用陣列(Array)
方式宣告
陣列宣告
3
int a[6] = {0};
陣列宣告
4
int a[n];
【陣列的資料型態】:int
【陣列名稱】:a
【陣列大小(元素個數)】:n
【元素計有】:a[0],a[1],…,a[n-1]
【索引】 : a[i]
陣列宣告與初值設定
5
char day[8] = "Sunday" ;
系統會在第七個字元補上‘0’
int a[3] = {0,11,22,33,44,55}; //錯誤的宣告
int a[] = {1,2,3,};
char id[]="10001";
陣列宣告與初值設定
6
// 程式名稱:5_array_test1.cpp
// 程式功能:一維陣列的宣告
#include <iostream>
using std::cin;
void main(void)
{
int i;
const int n = 6;
int a[6]; // correct !
float b[n]; // correct !
int c[n-1]; // correct !
cin >> i;
int d[i]; // Error ! Constant expression required
return;
}
陣列宣告與初值設定(二維陣列)
7
a[5][3] ;
int b[5][3] = {
{1,2,3}, // 設定第0列初值
{4,5,6}, // 設定第1列初值
{7,8,9}, // 設定第2列初值
{10,11,12}, // 設定第3列初值
{13,14,15} // 設定第4列初值
};
b[3][3] = 123;
陣列資料的存取
8
1. #include <iostream.
2. #include <conio.h>
3. int main(int argc, char* argv[])
4. {
5. using std:: cout;
6. using std::cin;
7. const int row = 3;
8. const int col = 3;
9. int a[row][col] = {0};
10. int count = 1, sum = 0;
11. for (int i=0; i<row; ++i)
12. for (int j=0; j<col; ++j)
13. {
14. cout << "Enter the value of a[" << i << "][" << j << "]: ";
15. cin >> a[i][j];
16. }
陣列資料的存取(續)
9
17. for (int i=0; i<row; ++i)
18. for (int j=0; j<col; ++j)
19. {
20. cout << "a[" << i << "][" << j << "]=";
21. cout.width(2);
22. cout << a[i][j] << " ";
23. if (j == col -1)
24. cout << endl;
25. }
26. _getch();
27. return 0;
28. }
陣列的應用
10
 轉置矩陣
1 2 3
4 5 6
7 8 9
10 11 12
1 4 7 10
2 5 8 11
3 6 9 12
轉置矩陣
1. #include <iostream>
2. #include<conio.h>
3. using std::cin; using std::cout; using std::endl; using std::right;
4. void main(void)
5. {
6. int a[9][9], b[9][9]; // 預設最大矩陣為 9 X 9
7. int i, j, m, n;
8. cout << "請輸二維矩陣a的大小 m * n 的 m 與 n 值 : ";
9. cin >> m >> n;
10. cout << "請輸入矩陣a的元素值(按列順序):" << endl;
11. for(i = 0; i < m; i++){
12. for(j = 0; j < n; j++){
13. cin >> a[i][j]; // 輸入矩陣a的元素值
14. b[j][i] = a[i][j]; // b為矩陣a的轉置矩陣
15. }
16. }
轉置矩陣 (續)
12
18. cout << endl;
19. cout << "矩陣a的轉置矩陣b如下:" << endl;
20. for(i = 0; i < n; i++){
21. for(j = 0; j < m; j++){
22. cout.width(3); // 用三格空格的空間來印出資料
23. cout << right << b[i][j]; // right 為靠右排列
24. }
25. cout << endl;
26. }
27.
28. _getch();
29. return;
30. }
排序法
13
 將n筆資料按數值由小排到大的演算法
 選擇排序法
 插入排序法
選擇排序法
1. #include <iostream>
2. #include <iomanip>
3. #include <conio.h>
4. #define N 6 //定義巨集指令
5. using namespace std;
6. void main(void)
7. {
8. int data[N] = {88,30,66,38,21,8};
9. int i, j;
10. int step = 0, min, temp;
11.
12. cout << "陣列data [0] [1] [2] [3] [4] [5]" << endl ;
13. cout << "---------------------------------------"<< endl ;
14. cout << " ";
15. for(i = 0; i < N; i++){
16. cout << setw(5) << right << data[i];
17. }
18. cout << endl ;
19. // 選擇排序法
20. for(i = 0; i < N-1; i++){
21. min = i;
22. // 找出data[i]至data[n-1]中最小者
23. for(j = i+1; j < N; j++){
24. if(data[j] < data[min]){
25. min = j;
26. }
27. else ;
28. }
29. // 將最小值與data[i]交換位置
30. temp = data[min];
31. data[min] = data[i];
32. data[i] = temp;
33.
34. // 印出每步驟的結果
35. cout << "步驟 " << step++ << "> ";
36. for(j = 0; j < N; j++){
37. cout << setw(5) << right << data[j];
38. }
39. cout << endl;
40. }
41. _getch();
42. return;
43. }
插入排序法
16
1. #include <iostream>
2. #include <iomanip> // 關於輸出格式的標頭檔
3. #include <conio.h>
4. #define N 6
5. using namespace std;
6. void main(void)
7. {
8. int data[N] = {88,30,66,38,21,8};
9. int i, j;
10. int step = 0, temp;
11.
12. cout << "陣列data [0] [1] [2] [3] [4] [5]" << endl ;
13. cout << "---------------------------------------"<< endl ;
14. cout << " ";
15. for(i = 0; i < N; i++){
16. cout << setw(5) << right << data[i]; // setw 定義在 iomanip中
17. } // 設定輸出寬度 ( = cout.width())
18. cout << endl ;
17
19. // 插入排序法
20. for(i = 1; i <= N-1; i++){
21. temp = data[i];
22. // 檢查data[i-1]至data[0],如果小於data[i]值就將之往右移一位
23. for(j = i-1; j >= 0; j--){
24. if(data[j] > temp){
25. data[j+1] = data[j];
26. }
27. else
28. break;
29. }
30.
31. // 將data[i]放入data[j+1]
32. data[j+1] = temp;
33.
34. // 印出每步驟的結果
35. cout << "步驟 " << step++ << "> ";
36. for(j = 0; j < N; j++){
37. cout << setw(5) << right << data[j];
38. }
39. cout << endl;
40. }
41. _getch():
42. return;
43. }
結構(Structure)
18
 表格(Table)
 記錄(Record) 、列(Row)
 欄位(Field) 、屬性(Attribute)
 學號:char、姓名:char、成績:int、總平均: float
結構的宣告與初值設定
19
struct Student{
char id[6];
char name[9];
int score[5]; //結構裡的陣列
float average;
};
Student Eng;
Student English[30]; //結構陣列
struct Student{
char id[6];
char name[9];
int score[5];
float average;
} Eng,English[30],
C_plusplus[30];
結構的宣告與初值設定
20
// 宣告一個型態為Student 的陣列變數C_plusplus,
// 並設定C_plusplus[0],C_plusplus[1]的初值
Student C_plusplus[30] = {
{"10001","張三",0,0,0,0,0,0.0},
{"10002","李四",0,0,0,0,0,0.0}
};
結構資料成員的存取
21
 結構數值指定
C_plusplus[0].id = "10001";
C_plusplus[0].name = "張三";
C_plusplus[0].score[0] = 0;
C_plusplus[0].score[1] = 0;
C_plusplus[0].score[2] = 0;
C_plusplus[0].score[3] = 0;
C_plusplus[0].score[4] = 0;
C_plusplus[0].average = 0.0;
結構中的結構
22
struct Student{
char id[6];
char name[9];
Date Birthday;
int score[5];
float average;
};
Student C_pp[30];
struct Date{
int yy;
int mm;
int dd;
};
結構資料成員的存取
23
cin >> C_pp[0].id;
cin >> C_pp[0].name;
cin >> C_pp[0].Birthday.yy;
cin >> C_pp[0].Birthday.mm;
cin >> C_pp[0].Birthday.dd;
cin >> C_pp[0].score[0];
cin >> C_pp[0].score[1];
cin >> C_pp[0].score[2];
cin >> C_pp[0].score[3];
cin >> C_pp[0].score[4];
int sum = 0; // 計算平均分數
for (int j = 0; j <= 4; j++){
sum += C_pp[0].score[j];
}
C_pp[0].average = sum / 5.0;
問題練習
24
1. 設計一程式可計算MN與 NM矩陣的乘積(設M = 3,
N = 4)
提示:設矩陣 A、B、C  C = AB


N
k
jkBkiAjiC
1
]][[]][[]][[
問題練習
25
2. 試利用結構設計一程式,可輸入全班同學(5人)的成績
(英文、數學、國文) ,並計算出平均成績。程式需要
可按照需求以不同科目為索引,由大至小排列學生成
績。
 結構欄位中應有學號欄位,其餘自定
 此題為Optional,有交有加分,沒交不扣分

More Related Content

PDF
Ch10 習題
PPTX
数据结构回顾
PDF
Ch11 教學
PDF
Ch12 範例
PDF
Ch11 範例
PDF
Num py basic(2) - v01
PDF
Ch12 教學
PDF
Ch9 範例
Ch10 習題
数据结构回顾
Ch11 教學
Ch12 範例
Ch11 範例
Num py basic(2) - v01
Ch12 教學
Ch9 範例

What's hot (20)

PDF
Appendix B 範例
PDF
Ch5 範例
PDF
Ch5 教學
PDF
Ch7 範例
PDF
Ch7 教學
PDF
Ch10 教學
PDF
Ch8 教學
PDF
Num py basic(1) - v01
PDF
Ch2 教學
PDF
Ch5 習題
PDF
Ch10 範例
PDF
Ch1 教學
PDF
Ch4 教學
PDF
Ppt 120-126
PDF
Math basic - v01
PDF
Ch9 教學
PDF
Appendix B 教學
PDF
Processing 03
PDF
Ch9 習題
PDF
Ppt 120-126
Appendix B 範例
Ch5 範例
Ch5 教學
Ch7 範例
Ch7 教學
Ch10 教學
Ch8 教學
Num py basic(1) - v01
Ch2 教學
Ch5 習題
Ch10 範例
Ch1 教學
Ch4 教學
Ppt 120-126
Math basic - v01
Ch9 教學
Appendix B 教學
Processing 03
Ch9 習題
Ppt 120-126
Ad

Similar to Chapter 5 array and struct (20)

PPT
第5章数组
PPT
第二章 线性表
PDF
資料結構-20個經典題型
PPT
C程式-陣列與指標
PDF
Essential C/C++
PPT
Ch06
PPT
dspch01資料結構資料結構資料結構資料結構資料結構資料結構資料結構資料結構.ppt
PPTX
C語言重要觀念快速複習與基本資料結構之觀念(適合資工電機相關科系,大一程度閱讀)
PPT
Ch 6
PDF
C語言陣列與字串
PDF
sorting
PPTX
Intro to C++ Basic
PPTX
期中考課輔
PPT
Data Structure
PPT
08 指標
PDF
1_10307.pdf
PDF
正課第10週模擬試題_解答.pdf
PDF
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
PPTX
Chapter 3 basic syntax and operator
PPT
第9章 查找
第5章数组
第二章 线性表
資料結構-20個經典題型
C程式-陣列與指標
Essential C/C++
Ch06
dspch01資料結構資料結構資料結構資料結構資料結構資料結構資料結構資料結構.ppt
C語言重要觀念快速複習與基本資料結構之觀念(適合資工電機相關科系,大一程度閱讀)
Ch 6
C語言陣列與字串
sorting
Intro to C++ Basic
期中考課輔
Data Structure
08 指標
1_10307.pdf
正課第10週模擬試題_解答.pdf
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
Chapter 3 basic syntax and operator
第9章 查找
Ad

Chapter 5 array and struct