More Related Content
Similar to Chapter 5 array and struct
Similar to Chapter 5 array and struct (20)
Chapter 5 array and struct
- 5. 陣列宣告與初值設定
5
char day[8] = "Sunday" ;
系統會在第七個字元補上‘0’
int a[3] = {0,11,22,33,44,55}; //錯誤的宣告
int a[] = {1,2,3,};
char id[]="10001";
- 7. 陣列宣告與初值設定(二維陣列)
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. 陣列資料的存取
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. 陣列資料的存取(續)
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. }
- 11. 轉置矩陣
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. 轉置矩陣 (續)
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. }
- 14. 選擇排序法
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 ;
- 15. 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. 插入排序法
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. 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. }
- 19. 結構的宣告與初值設定
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. 結構的宣告與初值設定
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. 結構資料成員的存取
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;
- 23. 結構資料成員的存取
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;