5節:陣列,隨機數
陣列可以使用多種方法進行初始化
陣列名稱只能當數字使用,永遠不能對陣列名稱賦值
陣列名稱代表陣列中第一個變數的地址
陣列名稱加下標可以獲得下標對應變數的地址
如果有陣列arr則*(arr + n)表示下標為n的變數
對陣列名稱做sizeof計算可以得到整個陣列的大小
C99規範中支援變長陣列
變長陣列不支援初始化語法
/* 陣列練習 */ #include <stdio.h> int main() { int num[5], pos = 0; //陣列宣告語句 num[3] = 7; printf("num[3]是%d\n", num[3]); for (pos = 0;pos <= 4;pos++) { printf("num[pos]是%d\n", num[pos]); } return 0; }
/* 陣列練習 */ #include <stdio.h> int main() { int num[4], pos = 0; for (pos = 0;pos <= 3;pos++) { printf("請輸入第%d個數據:", pos + 1); scanf("%d", &num[pos]); } for (pos = 3;pos >= 0;pos--) { printf("%d ", num[pos]); } printf("\n"); return 0; }
/*
陣列初始化練習
*/
#include <stdio.h>
int main() {
//int num[3] = {1, 2, 3};
//int num[] = {4, 5, 6};
int num[3] = {};
int pos = 0;
for (pos = 0;pos <= 2;pos++) {
printf("%d ", num[pos]);
}
printf("\n");
return 0;
}
/* 陣列名稱練習 */ #include <stdio.h> int main() { int num[3] = {}; //num = 0; 不可以對陣列名稱賦值 printf("num是%p, &num[0]是%p\n", num, &num[0]); printf("num + 2是%p,&num[2]是%p\n", num + 2, &num[2]); printf("陣列大小是%d\n", sizeof(num)); printf("陣列中變數個數是%d\n", sizeof(num) / sizeof(num[0])); return 0; }
/*
變長陣列
*/
#include <stdio.h>
int main() {
int size = 0, pos = 0;
printf("請輸入個數:");
scanf("%d", &size);
int num[size]; //變長陣列宣告
for (pos = 0; pos <= size - 1; pos++) {
printf("%d ", num[pos]);
}
printf("\n");
return 0;
}
rand函式可以獲得隨機數
srand函式可以設定隨機數種子,每個程式只應該使用一次
time函式可以獲得當前時間,使用整數表示
/*
隨機數練習
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(0));
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
作業:
1.編寫程式製作一張彩票,其中包括7個不同的數字,每個數字都在1到36之間。
2.身份證校驗
身份證號碼前17為分別按順序乘如下數字(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)。把成績結果求和,然後再除以11取餘。根據餘數從下面這些數中找到對應的數字(1,0,X,9,8,7,6,5,4,3,2)
/*
彩票練習
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int lottery[7] = {}, num = 0, value = 0, pos = 0;
srand(time(0));
do {
value = rand() % 36 + 1;
for (pos = 0;pos <= num - 1;pos++) {
if (value == lottery[pos]) {
break;
}
}
if (!(pos <= num - 1)) {
lottery[num] = value;
num++;
}
} while (num < 7);
for (pos = 0;pos <= 6;pos++) {
printf("%2d ", lottery[pos]);
}
printf("\n");
return 0;
}
/*
身份證練習
*/
#include <stdio.h>
int main() {
int num[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
int num1[] = {1, 0, -1, 9, 8, 7, 6, 5, 4, 3, 2};
int card[] = {5, 4, 2, 4, 7, 9, 1, 9, 1, 1, 0, 3, 2, 4, 4, 0, 5};
int pos = 0, sum = 0;
for (pos = 0; pos <= 16; pos++) {
sum += card[pos] * num[pos];
}
for (pos = 0;pos <= 16;pos++) {
printf("%d", card[pos]);
}
/*printf("%c\n", num1[sum % 11] < 0 ? 'X' : '0' + num1[sum % 11]);*/
if (num1[sum % 11] < 0) {
printf("X\n");
}
else {
printf("%d\n", num1[sum % 11]);
}
return 0;
}
二維陣列是由多個一維陣列構成的
二維陣列中的每個一維陣列也有一個下標,範圍是從0開始到一維陣列個數減一。
二維陣列在使用時要提供一個組下標用來對應一個一維陣列,另外還要提供一個組內下標用來對應某個變數。
使用迴圈巢狀結構操作二維陣列中的變數。
二維陣列即可以按照多個一維陣列的方式初始化,也可以按照多個變數的方式初始化。
如果提供了足夠多的初始化資料則可以不指定組數
可以給二維陣列指定一個下標來表示其中的某個一維陣列
二維陣列名稱也可以使用sizeof關鍵字,計算結果是整個二維陣列的大小
二維陣列名稱加一的結果是加上一個一維陣列的大小
/*
二維陣列練習
*/
#include <stdio.h>
int main() {
//int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
//int arr[2][3] = {{1, 2}, {4, 5, 6}};
//int arr[2][3] = {1, 2, 3, 4, 5, 6};
//int arr[2][3] = {1, 2, 4, 5, 6};
//int arr[][3] = {1, 2, 3, 4, 5};
int arr[][3] = {{1, 2}, {3, 4, 5}};
int row = 0, col = 0;
//int arr[2, 3];
//arr[1][1] = 5;
/*for (row = 0; row <= 1;row++) {
for (col = 0;col <= 2;col++) {
arr[row][col] = 3 * row + col + 1;
}
}*/
for (row = 0; row <= 1; row++) {
for (col = 0;col <= 2;col++) {
printf("%d ", arr[row][col]);
}
printf("\n");
}
return 0;
}
/*
二維陣列練習
*/
#include <stdio.h>
int main() {
int arr[2][3] = {};
printf("sizeof(arr[0])是%d\n", sizeof(arr[0]));
printf("sizeof(arr)是%d\n", sizeof(arr));
printf("arr是%p,arr + 1是%p\n", arr, arr + 1);
return 0;
}
練習:
1.在二維陣列中填充如下內容
11111
22222
33333
44444
55555
/*
二維陣列練習
*/
#include <stdio.h>
int main() {
int num[5][5] = {};
int row = 0, col = 0;
for (row = 0;row <= 4;row++) {
for (col = 0;col <= 4;col++) {
num[row][col] = row + 1;
}
}
for (row = 0;row <= 4;row++) {
for (col = 0;col <= 4;col++) {
printf("%d", num[row][col]);
}
printf("\n");
}
return 0;
}
2.填充如下內容
11112
4 2
4 2
4 2
43333
/*
二維陣列練習
*/
#include <stdio.h>
int main() {
char map[5][5] = {};
int pos = 0, row = 0;
for (pos = 0;pos <= 3;pos++) {
map[0][pos] = '1';
}
for (pos = 0;pos <= 3;pos++) {
map[pos][4] = '2';
}
for (pos = 1;pos <= 4;pos++) {
map[4][pos] = '3';
}
for (pos = 1;pos <= 4;pos++) {
map[pos][0] = '4';
}
for (row = 0; row <= 4; row++) {
for (pos = 0;pos <= 4;pos++) {
if (map[row][pos]) {
printf("%c", map[row][pos]);
}
else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
3.圈叉棋
/*
圈叉棋練習
*/
#include <stdio.h>
int main() {
int map[3][3] = {};
int row = 0, col = 0, loop = 0, player = 1;
for (row = 0;row <= 2;row++) {
for (col = 0;col <= 2;col++) {
if (!map[row][col]) {
printf(".");
}
else if (map[row][col] == 1) {
printf("O");
}
else {
printf("X");
}
}
printf("\n");
}
for (loop = 0;loop < 9;loop++) {
do {
printf("請輸入行號和列號(1到3之間):");
scanf("%d %d", &row, &col);
if (row < 1 || row > 3) {
continue;
}
if (col < 1 || col > 3) {
continue;
}
if (map[row - 1][col - 1]) {
continue;
}
break;
} while (1);
map[row - 1][col - 1] = player;
player = 3 - player;
for (row = 0;row <= 2;row++) {
for (col = 0;col <= 2;col++) {
if (!map[row][col]) {
printf(".");
}
else if (map[row][col] == 1) {
printf("O");
}
else {
printf("X");
}
}
printf("\n");
}
}
return 0;
}
作業:
1.圈叉棋遊戲增加判斷輸贏的程式碼
/*
圈叉棋練習
*/
#include <stdio.h>
int main() {
int map[3][3] = {};
int row = 0, col = 0, loop = 0, player = 1;
for (row = 0;row <= 2;row++) {
for (col = 0;col <= 2;col++) {
if (!map[row][col]) {
printf(".");
}
else if (map[row][col] == 1) {
printf("O");
}
else {
printf("X");
}
}
printf("\n");
}
for (loop = 0;loop < 9;loop++) {
do {
printf("請輸入行號和列號(1到3之間):");
scanf("%d %d", &row, &col);
if (row < 1 || row > 3) {
continue;
}
if (col < 1 || col > 3) {
continue;
}
if (map[row - 1][col - 1]) {
continue;
}
break;
} while (1);
map[row - 1][col - 1] = player;
if (map[0][0] && map[0][0] == map[0][1] && map[0][0] == map[0][2]) {
if (map[0][0] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[1][0] && map[1][0] == map[1][1] && map[1][0] == map[1][2]) {
if (map[1][0] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[2][0] && map[2][0] == map[2][1] && map[2][0] == map[2][2]) {
if (map[2][0] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[0][0] && map[0][0] == map[1][0] && map[0][0] == map[2][0]) {
if (map[2][0] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[0][1] && map[0][1] == map[1][1] && map[0][1] == map[2][1]) {
if (map[2][1] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[0][2] && map[0][2] == map[1][2] && map[0][2] == map[2][2]) {
if (map[2][2] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[0][0] && map[0][0] == map[1][1] && map[0][0] == map[2][2]) {
if (map[2][2] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
if (map[0][2] && map[0][2] == map[1][1] && map[0][2] == map[2][0]) {
if (map[0][2] == 1) {
printf("O贏了\n");
}
else {
printf("X贏了\n");
}
break;
}
player = 3 - player;
for (row = 0;row <= 2;row++) {
for (col = 0;col <= 2;col++) {
if (!map[row][col]) {
printf(".");
}
else if (map[row][col] == 1) {
printf("O");
}
else {
printf("X");
}
}
printf("\n");
}
}
return 0;
}