-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathArrayForm.pas
More file actions
143 lines (111 loc) · 2.77 KB
/
ArrayForm.pas
File metadata and controls
143 lines (111 loc) · 2.77 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
136
137
138
139
140
141
142
143
unit ArrayForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FNumArr: array[0..9] of Integer;
procedure InitArray;
function GetArraySum: Integer;
function GetArrayMaxNum: Integer;
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.FormCreate(Sender: TObject);
begin
InitArray;
end;
// 배열에 초기값을 설정
procedure TForm3.InitArray;
var
I: Integer;
begin
// 배열(FNumArr)의 길이만큼 반복하며 임의의 값(1~100)을 설정
for I := 0 to Length(FNumArr) - 1 do
FNumArr[I] := Random(100);
end;
procedure TForm3.Button1Click(Sender: TObject);
var
I: Integer;
Sum, MaxNum: Integer;
begin
// 배열의 합
Sum := GetArraySum;
// 배열 중 최고값
MaxNum := GetArrayMaxNum;
Memo1.Lines.Clear;
Memo1.Lines.Add('배열 내용');
{ TODO :
(1) for 문을 이용해 배열의 내용을 출력하세요.
배열의 크기 변경되도 동작하도록 반복의 끝은 Length(FNumArr) - 1로 설정
예> for I := 0 to Length(FNumArr) - 1 do }
for I := 0 to Length(FNumArr) - 1 do
begin
Memo1.Lines.Add('-'+ inttostr(Fnumarr[i]));
end;
Memo1.Lines.Add('배열의 합은 ' + IntToStr(Sum) + ' 입니다.');
Memo1.Lines.Add('배열의 최대값은 ' + IntToStr(MaxNum) + ' 입니다.');
end;
function TForm3.GetArraySum: Integer;
var
I, Sum: Integer;
begin
Sum := 0;
{ TODO : (2) for 문을 이용해 배열의 값을 모두 더해 반환하도록 구현 }
for I := 0 to Length(FNumArr) - 1 do
begin
Sum := sum+fnumarr[i];
end;
Result := Sum;
end;
function TForm3.GetArrayMaxNum: Integer;
var
I, MaxNum: Integer;
begin
MaxNum := 0;
{ TODO :
(3) for 문을 이용해 배열의 값 중 가장 큰 값을 반환하도록 구현
if 문을 이용해 숫자를 비교 }
for I := 0 to Length(FNumArr) - 1 do
begin
if FNumArr[i] >= maxnum then
maxnum := fnumarr[i];
end;
Result := MaxNum;
end;
procedure TForm3.Button2Click(Sender: TObject);
var
I,
CountOver, CountUnder: Integer;
begin
CountOver := 0;
CountUnder := 0;
{ TODO :
(4) for 문을 이용해 배열의 값이
50 이상(>=)인 경우 CountOver 1 증가
50 미만(<)인 경우 CountUnder 1 증가 하도록 구현
}
for I := 0 to Length(FNumArr) - 1 do
begin
if FNumArr[i] >= 50 then
begin
countover := countover+1;
end
else countunder := countunder+1;
end;
Memo1.Lines.Add('50 이상인 수의 갯수: ' + IntToStr(CountOver));
Memo1.Lines.Add('50 미만인 수의 갯수: ' + IntToStr(CountUnder));
end;
end.