-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek09.cpp
More file actions
135 lines (108 loc) · 1.8 KB
/
week09.cpp
File metadata and controls
135 lines (108 loc) · 1.8 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 예제 6-2
#include <iostream>
using namespace std;
int sum(int a, int b) {
int sum = 0;
for (int i = a; i <= b; i++) {
sum += i;
}
return sum;
}
int sum(int a) {
int sum = 0;
for (int i = 0; i <= a; i++) {
sum += i;
}
return sum;
}
int main() {
cout << sum(3,5) << endl;
cout << sum(3) << endl;
cout << sum(100) << endl;
}
// 실행 결과
12
6
5050
// 예제 6-3
#include <iostream>
#include <string>
using namespace std;
void star(int a=5);
void msg(int id, string text="");
void star(int a) {
for(int i=0; i<a; i++)
cout << '*';
cout << endl;
}
void msg(int id, string text) {
cout << id << ' ' << text << endl;
}
int main() {
star();
star(10);
msg(10);
msg(10, "Hello");
}
// 실행 결과
*****
**********
10
10 Hello
// 예제 6-4
#include <iostream>
using namespace std;
void f(char c, int line=1);
void f(char c=' ', int line) {
for(int i = 0; i < line; i++) {
for(int j = 0; j < 10; j++)
cout << c;
cout << endl;
}
}
int main() {
f();
f('%');
f('@', 5);
}
// 실행 결과
%%%%%%%%%%
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
// 예제 6-5
#include <iostream>
using namespace std;
void fillLine(int n = 25, char c = '*') {
for(int i=0; i<n; i++) cout << c;
cout << endl;
}
int main() {
fillLine();
fillLine(10, '%');
}
// 실행 결과
*************************
%%%%%%%%%%
// 예제 6-6
#include <iostream>
using namespace std;
class MyVector{
int *p;
int size;
public:
MyVector(int n = 100) {
p = new int[n];
size = n;
}
~MyVector() { delete [] p; }
};
int main() {
MyVector *v1, *v2;
v1 = new MyVector();
v2 = new MyVector(1024);
delete v1;
delete v2;
}