MultiDimensional Array
2022. 3. 4. 00:43
1
2
3
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
int main(void){
// 다차원 배열 Multidimensional Array
int i; // ㅁ
int arr[5]; // ㅁㅁㅁㅁㅁ
// [0][1][2][3][4]
int arr2[2][5];
// ㅁㅁㅁㅁㅁ
// ㅁㅁㅁㅁㅁ
// [0,0][0,1][0,2][0,3][0,4] -> arr2[0][0]
// [1,0][1,1][1,2][1,3][1,4] -> arr2[1][4]
int arr3[4][2];
// ㅁㅁ
// ㅁㅁ
// ㅁㅁ
// ㅁㅁ
// [0,0][0,1] -> arr3[0][1]
// [1,0][1,1]
// [2,0][2,1]
// [3,0][3,1]
int arr4[3][3][3];
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
// ㅁㅁㅁ
int arr[5] = {1,2,3,4,5};
int arr2[2][5] =
{{1,2,3,4,5},
{1,2,3,4,5}};
int arr3[4][2] =
{ {1,2},
{3,4},
{5,6},
{7,8}
};
for (int i = 0; i<4; i++){
for (int j = 0; j < 2;j++){
printf("2차원 배열 (%d, %d)의 값 : %d\n", i ,j, arr3[i][j]);
}
}
int arr4[3][3][3] = {
{
{1,2,3},
{4,5,6},
{7,8,9}
},
{
{11,12,13},
{14,15,16},
{17,18,19}
},
{
{21,22,23},
{24,25,26},
{27,28,29}
}
};
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++){
for (int k = 0; k<3; k++){
printf("3차원 배열 (%d, %d, %d)의 값 : %d\n", i ,j, k,arr4[i][j][k]);
}
}
}
|
cs |
다차원 배열의 기본 형태
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
// 10 마리의 서로 다른 동물 (각 카드 2장씩)
// 사용자로부터 2개의 입력값을 받아서 -> 같은 동물 찾으면 카드 뒤집기
// 모든 동물 쌍을 찾으면 게임 종료
// 총 실패 횟수 알려주기
srand(time(NULL));
initAnimalArray();
initAnimalName();
shuffleAnimal();
int failCount = 0; //실패 횟수
while(1){
int select1 = 0; // 사용자가 선택한 첫 번째 수
int select2 = 0; // 사용자가 선택한 두 번째 수
printAnimals(); // 동물 위치 출력
printQuestion(); // 문제 출력
printf("뒤집을 카드를 2개 고르세요 : ");
scanf("%d %d", &select1, &select2);
if (select1 == select2) // 같은 카드 선택 시 무효
continue;
// 좌표에 해당하는 카드를 뒤집어 보고 같은지 같지 않은지 확인.
// 정수 좌표 (x,y)로 변환.
int firstSelect_x = conv_pos_x(select1);
int firstSelect_y = conv_pos_y(select2);
int secondSelect_x = conv_pos_x(select1);
int secondSelect_y = conv_pos_y(select2);
// 같은 동물인 경우 -> 카드가 뒤집히지 않았는지 && 두 동물이 같은지
if (checkAnimal[firstSelect_x][firstSelect_y] == 0 && checkAnimal[secondSelect_x][secondSelect_y] == 0 && arrayAnimal[firstSelect_x][firstSelect_y] == arrayAnimal[secondSelect_x][secondSelect_y])
{
printf("\n\n빙고 ! : %s 발견 \n\n", strAnimal[arrayAnimal[firstSelect_x][firstSelect_y]]);
checkAnimal[firstSelect_x][firstSelect_y] = 1;
checkAnimal[secondSelect_x][secondSelect_y] = 1;
}
//다른 동물인 경우
else
{
printf("\n\n땡! 틀렸거나 이미 뒤집힌 카드입니다 !\n");
printf("%d : %s\n", select1, strAnimal[arrayAnimal[firstSelect_x][firstSelect_y]]);
printf("%d : %s\n", select1, strAnimal[arrayAnimal[secondSelect_x][secondSelect_y]]);
printf("\n\n");
failCount++;
}
// 모든 동물을 찾았는지 여부, 1: 참, 0: 거짓
if (foundAllAnimals() == 1){
printf("\n\n 축하합니다 ! 모든 동물을 다 찾았네요 \n");
printf("지금까지 총 %d번 실수 하였습니다.\n", failCount);
break;
}
}
return 0;
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
int arrayAnimal[4][5]; // 카드 지도 20장
int checkAnimal[4][5]; // 뒤집혔는지 여부 확인
char * strAnimal[10];
void initAnimalArray();
void initAnimalName();
void shuffleAnimal();
int getEmptyPosition();
int conv_pos_x(int x);
int conv_pos_y(int y);
void printAnimals();
void printQuestion();
int foundAllAnimals();
|
cs |
위 함수 선언
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
void initAnimalArray(){
for (int i= 0; i < 4; i++){
for (int j = 0; j < 5; j++){
arrayAnimal[i][j] = -1;
}
}
}
void initAnimalName(){
strAnimal[0] = "원숭이 ";
strAnimal[1] = "하마 ";
strAnimal[2] = "강아지 ";
strAnimal[3] = "고양이 ";
strAnimal[4] = "돼지 ";
strAnimal[5] = "코끼리 ";
strAnimal[6] = "기린 ";
strAnimal[7] = "낙타 ";
strAnimal[8] = "타조 ";
strAnimal[9] = "호랑이 ";
}
void shuffleAnimal(){
for (int i =0; i<10; i++){
for (int j =0 ; j < 2; j++){
int position = getEmptyPosition();
int x = conv_pos_x(position);
int y = conv_pos_y(position);
arrayAnimal[x][y] = i;
}
}
}
int getEmptyPosition(){
// ㅁㅁㅁㅁㅁ 0 1 2 3 4 -> 0 0 0 0 0
// ㅁㅁㅁㅁㅁ 5 6 7 8 9 -> 1 1 1 1 1
// ㅁㅁㅁㅁㅁ 10 11 12 13 14 -> 2 2 2 2 2
// ㅁㅁㅁㅁㅁ 15 16 17 18 19 -> 3 3 3 3 3
while(1){
int randPos = rand() % 20; // 0 ~ 19 사이 숫자 반환
// 19가 나왔다면 (3,4)의 값임.
int x = conv_pos_x(randPos);
int y = conv_pos_y(randPos);
if(arrayAnimal[x][y] == -1){
return randPos;
}
else {
}
}
return 0;
}
int conv_pos_x(int x){
// 19 -> (3,4)로 반환
return x / 5;
}
int conv_pos_y(int y){
return y % 5; // y를 5로 나눈 나머지 값 // 19의 경우 몫은 3, 나머지는 4,,
}
void printAnimals(){
printf("\n=====이건 비밀인데 .. 몰래 보여줍니다 ===\n\n");
for (int i = 0; i < 4 ; i++){
for (int j = 0; j < 5; j++){
printf("%8s", strAnimal[arrayAnimal[i][j]]);
}
printf("\n");
}
printf("\n===========================\n\n");
}
void printQuestion(){
printf("\n\n(문제)\n\n");
int seq= 0;
// seq checkAnimal
// ㅁㅁㅁㅁㅁ 0 1 2 3 4 0 0 0 0 0
// ㅁㅁㅁㅁㅁ하마 6 7 8 9 1 0 0 0 0
// ㅁㅁㅁㅁㅁ 10하마12 13 14 0 1 0 0 0
// ㅁㅁㅁㅁㅁ 15 16 17 18 19 0 0 0 0 0
for (int i = 0; i < 4; i++){
for (int j = 0; j < 5; j++){
// 카드를 뒤집어 정답을 맞혔으면 '동물 이름'
if (checkAnimal[i][j]!= 0){
printf("%16s ", strAnimal[arrayAnimal[i][j]]);
}
// 아직 뒤집지 못했으면, (정답을 못맞혔으면) 뒷면 -> 위치를 나타내는 숫자
else {
printf("%4d", seq);
}
seq++;
}
}
}
int foundAllAnimals(){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 5; j++)
{
if(checkAnimal[i][j] == 0){
return 0;
}
}
}
return 1; //모두 다 찾음.
}
|
cs |
위 함수 정의.
'Programming > C\C++' 카테고리의 다른 글
파일 입출력 (0) | 2022.03.04 |
---|---|
struct 구조체 (0) | 2022.03.04 |
*포인터* (0) | 2022.03.04 |
Initial Array && ASCII (0) | 2022.02.25 |
functions setting 과 활용 (0) | 2022.02.25 |