C (第4章) 基礎程式設計實作
C (第4章) 基礎程式設計實作
4-1
1
[ ]={ 0, 1, };
[ ]
) score_array
) score_array[0]85
) score_array[1]92
IT加油站
0
4 0, 1, 2, 3
4-1
string animals[7] = {"", "", "", "", " ", "", ""};
CHAPTER 04
1, 2, ..., 7
0, 1, ..., 6
4-1
1 2 3 4 5 6 7
) animals
) animals[0]
) animals[1]
) animals[2]
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 int score_array[4] = {85, 92, 88, 96};
8 string animals[7] = {"" , "" , "" , "" , " ", "" , "" };
9
10 cout << score_array[0] << " " << score_array[2] << endl;
11 cout << animals[2] << " " << animals[6] << endl;
12
13 return 0;
14 }
執行結果
85 88
63
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int score_array[4] = {85, 92, 88, 96};
7 int x = score_array[0]; // x85
8 cout << x << endl;
9
10 score_array[0] = 95; // score_array[95, 92, 88, 96]
11 x = score_array[0]; // x95
12 cout << x << endl;
13
14 return 0;
15 }
執行結果
85
95
C++
[ ][ ]={
{ 0 },
{ 1 },
{ 2 },
[ ][ ]
64
CHAPTER 04
C++ 4-2
string animals_2d[2][3] = {
{"", "", ""},
{"", " ", ""}
};
4-2
0 1 2
0
0 1 2
1
) animals_2d
) animals_2d[0]
) animals_2d[0][0]
) animals_2d[0][1]
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 int num_2d_array[3][3] = {
8 {1, 2, 3},
9 {4, 5, 6},
10 {7, 8, 9}
11 };
65
12 string animals_2d[2][3] = {
13 {"" , "" , "" },
14 {"" , " ", "" }
15 };
16
17 cout << num_2d_array[0][0] << " ";
18 cout << num_2d_array[1][1] << endl;
19
20 cout << animals_2d[0][1] << " ";
21 cout << animals_2d[1][1] << endl;
22
23 return 0;
24 }
執行結果
1 5
4-2
animals_2d[0]{"", "", ""}
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int num_array[3][3] = {
7 {1, 2, 3},
8 {4, 5, 6},
9 {7, 8, 9}
66
CHAPTER 04
10 };
11 int x = num_array[0][2]; // x3
12 num_array[2][1] = -8; // num_array[2][1] 8-8
13
14 cout << x << endl;
15 cout << num_array[2][1] << endl;
16
17 return 0;
18 }
執行結果
3
-8
̹̹實例演練
4-3
4-3
70 10 14 7 25 30 50
Step 01
0~6
4-3
Step 02
70 50
67
Step 03
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int num_visitors[7] = {70, 10, 14, 7, 25, 30, 50}; // 0~6
7 cout << num_visitors[0] << " ";
8 cout << num_visitors[1] << " ";
9 cout << num_visitors[2] << " ";
10 cout << num_visitors[3] << " ";
11 cout << num_visitors[4] << " ";
12 cout << num_visitors[5] << " ";;
13 cout << num_visitors[6] << " " << endl;
14
15 int temp = num_visitors[0]; // 70 temp
16 num_visitors[0] = num_visitors[6]; // 50
17 num_visitors[6] = temp; // temp 70
18
19 cout << num_visitors[0] << " ";
20 cout << num_visitors[1] << " ";
21 cout << num_visitors[2] << " ";
22 cout << num_visitors[3] << " ";
23 cout << num_visitors[4] << " ";
24 cout << num_visitors[5] << " ";;
25 cout << num_visitors[6] << " " << endl;
26
27 return 0;
28 }
執行結果
70 10 14 7 25 30 50
50 10 14 7 25 30 70
68
CHAPTER 04
4-2
3
4-2-1
̹̹實例演練
4-3
4-4
4-4
4-4
50 10 14 7 25 30 70
50
10
14
7
25
30
70
Step 01
Step 02
cout
69
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int num_visitors[7] = {50, 10, 14, 7, 25, 30, 70};
7
8 cout << " " << num_visitors[0] << " " << endl;
9 cout << " " << num_visitors[1] << " " << endl;
10 cout << " " << num_visitors[2] << " " << endl;
11 cout << " " << num_visitors[3] << " " << endl;
12 cout << " " << num_visitors[4] << " " << endl;
13 cout << " " << num_visitors[5] << " " << endl;
14 cout << " " << num_visitors[6] << " " << endl;
15
16 return 0;
17 }
執行結果
50
10
14
7
25
30
70
4-2-2
C++
for
for
for( ; ; ){
70
CHAPTER 04
)
for
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 int score_array[4] = {95, 92, 88, 96}; // 4
7 // forcout 40,1,2,34
8 // i0, 1, 2, 3
9 for(int i = 0; i < 4; i++){
10 cout << score_array[i] << endl;
11 }
12
13 return 0;
14 }
執行結果
95
92
IT加油站
88 i
96 i = i + 1i += 1i++
forint i = 0
i 4
i++for
i 0 1 2 3
0 1
71
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 string animals[7] = {"" , "" , "" , "" , " ", "" , "" };
8 for(int i = 0; i < 7; i++){
9 cout << animals[i] << endl;
10 }
11
12 return 0;
13 }
執行結果
̹̹實例演練
for
50
10
14
7
25
30
70
72
CHAPTER 04
Step 01 for
for71day_index0 21
TIP
for
i
day_index
Step 02
forcout
" "
num_visitors
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 int num_visitors[7] = {50, 10, 14, 7, 25, 30, 70};
8 string day_names[7] = {"" , "" , "" , "" ,"" ,
"" , "" };
9
10 for(int day_index = 0; day_index < 7; day_index++){
11 cout << day_names[day_index] << " " << num_visitors[day_index] << " "
<< endl;
12 }
13
14 return 0;
15 }
73
執行結果
50
10
14
7
25
30
70
TIP
num_visitors
)
for
for
forfor
for
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 string animals_2d[2][3] = {
8 {"" , "" , "" },
9 {"" , " ", "" }
10 };
11
12 for(int i = 0; i < 2; i++){ // animals_2d
13 for(int j = 0; j < 3; j++){ // animals_2d
14 cout << animals_2d[i][j] << endl;
15 }
16 }
17
18 return 0;
19 }
74
CHAPTER 04
執行結果
IT加油站
1 for(int i = 0; i < 3; i++){ //
2 for(int j = 0; j < 7; j++){ //
3 cout << "hello" << endl;
4 }
5 }
for
for
̹̹實例演練
4-5
4-5
1 50 10 14 7 25 30 70
2 30 24 14 9 87 63 25
3 100 52 82 89 36 78 22
Step 01
75
1 int num_visitors[3][7] = {
2 {50, 10, 14, 7, 25, 30, 70},
3 {30, 24, 14, 9, 87, 63, 25},
4 {100, 52, 82, 89, 36, 78, 22}
5 };
6 string day_names[7] = {"" , "" , "" , "" , "" ,
"" , "" };
Step 02 for
程式碼
執行結果
50
10
14
7
25
30
70
30
24
( )
Step 03 for
76
CHAPTER 04
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 int num_visitors[3][7] = {
8 {50, 10, 14, 7, 25, 30, 70},
9 {30, 24, 14, 9, 87, 63, 25},
10 {100, 52, 82, 89, 36, 78, 22}
11 };
12 string day_names[7] = {"" , "" , "" , "" , "" ,
"" , "" };
13
14 for(int week_index = 0; week_index < 3; week_index++){ // 3
0,1,2
15 // cout
16 cout << " " << week_index + 1 << "" << endl;
17 for int day_index = 0; day_index < 7; day_index++){
(
18 // cout
19 cout << day_names[day_index] << num_visitors[week_index]
[day_index] << " " << endl;
20 }
21 }
22
23 return 0;
24 }
執行結果
1
50
10
14
7
25
30
70
2
30
24
( )
77
4-2-3
C++
if-else
if-else
if( ){
1
}
else{
2
}
if-else
程式碼
1 #include <stdio.h>
2
3 using namespace std;
4
5 int main(){
6 int score = 92;
7 if(score >= 70){
8 cout << "" << endl;
9 }
10 else{
11 cout << "" << endl;
12 }
13
14 return 0;
15 }
執行結果
ifscore >= 70
if-else
78
CHAPTER 04
if trueifif
falseelse
̹̹實例演練
35
! ...
Step01 if
Step 02
程式碼
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 int main(){
7 int num_visitors[7] = {50, 10, 14, 7, 25, 30, 70};
8 string day_names[7] = {"" , "" , "" , "" , "" ,
"" , "" };
9
10 for(int day_index = 0; day_index < 7; day_index++){
11 if(num_visitors[day_index] > 35){
12 cout << day_names[day_index] << " " << endl;
13 }
14 else{
15 cout << day_names[day_index] << " ..." << endl;
16 }
17 }
18
19 return 0;
20 }
79
執行結果
...
...
...
...
...
IT加油站
if else if else
18.5 <= BMI < 24
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main(){
6 double BMI = 22.5;
7 if(BMI < 18.5){
8 cout << "" << endl;
9 }
10 else if(BMI >= 24){
11 cout << "" << endl;
12 }
13 else{
14 cout << "" << endl;
15 }
16
17 return 0;
18 }
執行結果
80
CHAPTER 04
4-3
C++
(1 1 ,2 2 , ){
[ return ]
}
intaddition ( )
a b
c returnc
addition ( )
程式碼
執行結果 Tip
z = addition (5, 10)
15 z = f(x,y)
81
̹̹實例演練
4-1
Step 01 swap ( )
Step 02 print_array ( )
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 void swap(int a[], int i, int j){
6 int temp = a[i];
7 a[i] = a[j];
8 a[j] = temp;
9 }
10
11 void print_array(int arr[], int arr_len){
12 for(int i = 0; i < arr_len; i++){
82
CHAPTER 04
20 print_array(num_visitors, 7);
21
22 swap(num_visitors, 0, 6);
23 print_array(num_visitors, 7);
24
25 return 0;
26 }
執行結果
70 10 14 7 25 30 50
50 10 14 7 25 30 70
cout
4-4
1×2× ×nfor
1×2×3
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int main() {
6 int n = 3; // n!
7 int product = 1; // product 1
8 for(int i = 0; i < n; i++){ // i0,1,2,...,n-1
9 product = (i+1) * product; // i+11,2,3,...,n
10 }
11 cout << product << endl;
12
13 return 0;
14 }
83
執行結果
n!=n×(n-1)× ×1
f(n)=n×f(n-1) f(1)=1
n=3
f(3)=3×f(2)
f(2)=2×f(1)
f(1)=1
f(3)=3×f(2)=3×2×f(1)=3×2×1=6
f(n) f(n-1)
f( ) 2
n!=n×(n-1)× ×1
1+2+ +n
̹̹實例演練
1 1 1
n×(n-1)!
84
CHAPTER 04
Step 02 3!10!
程式碼
1 #include <iostream>
2
3 using namespace std;
4
5 int factorial(int n){ // factorialn
6 if(n <= 1){ // --
7 return 1;
8 }
9 else{
10 return n * factorial(n-1);
11 } // --
12 }
13
14 int main(){
15 cout << factorial(3) << endl; // 3!
16 cout << factorial(10) << endl; // 10!
17
18 return 0;
19 }
執行結果
6
3628800
85
選擇題
1 #include <iostream>
2 using namespace std;
3 int recursive_add(int n){
4 if(n <= 2){
5 return 5;
6 }
7 return n + recursive_add(n - 3);
8 }
9 int main(){
10 cout << recursive_add(15) << endl;
11 return 0;
12 }
多元練習
1.
30 1 1
86
時況報
導
C ++
2.
100 20 85
95 99 75
89 73 92
C ++ for
3. C ++
40 80 75 20 96 69 50
6090
4. C ++p, q
87