-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_practice.cpp
More file actions
85 lines (63 loc) · 1.68 KB
/
7_practice.cpp
File metadata and controls
85 lines (63 loc) · 1.68 KB
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
#include <iostream>
#include <cstdlib> // rand, srand
#include <ctime> // time
#include <string>
using namespace std;
void main()
{
// 6-1. 어떤 달의 날짜 수를 배열을 사용해서 출력하는 프로그램을 만드시오.
int month = 0;
int arr[12] = { 31,30,31,30,31,30,31,31,30,31,30,31 };
// int* p = arr; // 아래와 동일
int* p = &arr[0];
// int* p = &arr; // 오류나는 코드
do {
cout << "월을 입력해주세요. (1~12) : ";
cin >> month;
} while (month > 12 || month < 1);
cout << "해당 월에는 " << p[month-1] << "개의 일이 있습니다." << endl;
// 6-2. 1에서 50 사이의 랜덤한 정수 10개를 요소로 갖는 배열을 만들고, 이러한 배열에서 최솟값과 최댓값을 구해 출력하는 프로그램을 만드세요.
const int size = 10;
int arr[size];
srand(time(0));
for (int i = 0; i < size; i++)
{
arr[i] = rand() % (50) + 1; // 1~50 사이 랜덤한 정수 생성
}
int max = 0;
int min = 51;
cout << "배열 요소 출력 : " << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
cout << "\n최댓값은 " << max << "이고, 최솟값은 " << min << "입니다." << endl;
// 6-3. 주어진 2차원 배열에 대해 row 방향 혹은 col 방향으로 읽는 프로그램을 작성하시오
string w;
int arr[2][3] = {{1,4,6},{3,5,1}};
int* p = &arr[0][0];
cout << "어떤 방향으로 읽을 것인지 기입하십시오. (row or col):";
cin >> w;
if (w.compare("row") == 0) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
if (w.compare("col") == 0) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << *(p + j) << " ";
}
cout << endl;
p = p + 3;
}
}
}