Title: 05-530-111 Computer Programming 1
1 05-530-111 Computer Programming 1
????? 3???????????????(Control Structures)
2???????????
- 3.1 ?????????? (Algorithm)
- 3.2 ????????? (Pseudocode)
- 3.3 ??????????????? (Control structure)
- 3.4 ????????????????? (Sequence structure)
- 3.5 ???????????????????? if
- 3.6 ???????????????????????? switch
3???????????
- 3.8 ???????????????????? while
- 3.7 ???????????????????? for
- 3.9 ???????????????????? do/while
- 3.10 ?????? break ??? continue
4???????????
- 3.11 ?????????????????? (Formulating Algorithm)
- ????????? 1, ???????????????????????
(Case Study 1,
Counter-Controlled Repetition) - 3.12 ????????????????????????????????????????????
?? (Formulating Algorithm with Top-Down, Stepwise
Refinement) - ????????? 2, ???????????????????????????
(Case Study 2, Sentinel-Controlled
Repetition) - ????????? 3, ??????????????????????
(Case Study 3, Nested
Control Structure)
53.1 ?????????? (Algorithm)
- ?????????????????? ??? ???????????????????????????
???????????????????? - ????????
- ????????????????
63.2 ????????? (Pseudocode)
- ????????? ??? ??????????????????????
????????????????????????????????????????? - ??????????????????????????????????????
?????????????????????????????? ???????????????????
?????????????????????? ? - ????????????????????? ???????????????????????????
??? ??????????????????????????
??????????????????????????????????????????????????
???????????????????????????????? (???? C ????
Java) ???????????? ???????????????????????????????
?????????????????? ?
73.3 ??????????????? (Control structure)
- ??????????????????????????????????????????????????
?????????????????? 3 ???????????? ??? - ????????????????? (Sequence structure)
- ????????????????? (Selection structure)
- ????????????????? (Repetition structure)
83.4 ????????????????? (Sequence structure)
- ??????????????????????????????????????????????????
?????????????? ???????????????????????????????????
???????????????????
9???????? Flowchart ?????????????????????????????
103.5 ???????????????????? if
- ???????????????????? if ??? ??????????????????????
????????? (???????????????) ??????????????????????
???? ?????? 3 ????????? - 1. ????????????? (One Alternative)
- 2. ??????????? (Two Alternative)
- 3. ???????????? (Multiple-Alternative)
113.5.1 ???????????????????? if ?????????????
- ??????????????? ??????????????? (???????????????)
?????????? ??????????????????????????? - ???????? Flowchart ??????????????????????? if
?????????????
12????????????????????????????? if ?????????????
- ??????????? 1 ??????
- if (condition)
Statement T
??????????????????????
if (condition)
True task
13??????????????????????????????? if ?????????????
- ????????1
- if (x gt 0)
- total total x
????????2 if (xgty)
tempx xy ytemp
143.5.2 ???????????????????? if ???????????
- ???????????????????????????? ????????
????????????? (???????????????)
???????????????????????????????? ???? ????????
(???????????????) ????????????????????????????????
- ???????? Flowchart ??????????????????????? if
???????????
15????????????????????????????? if ???????????
- ??????????? 1 ??????
- if (condition)
- statement T
- else
- statement F
??????????????????????
if (condition) true
task else
false task
16??????????????????????????????? if ???????????
- ????????1 if (x gt 0)
- cout ltlt "positive\n
- else
- cout ltlt negative\n
????????2 if (xgty) temp x xy
ytemp else countcount 1
cout ltlt "smaller\n
173.5.3 ???????????????????? if ????????????
- ????????????????????????????? ????????
???????????????????????????? ?????????????????????
????????????????? 2 ???????? ?????????????????????
???????? if ??????????????????????????????????????
? MultipleAlternative
18????????????????????????????? if ????????????
- ??????
- if (condition 1)
- statement 1
- else if (condition 2)
- statement 2
-
-
- else if (condition n)
- statement n
- else
- statement
19??????????????????????????????? if ????????????
- /increment mum-pos, num_neg, or num_zero
depending on x/ - if (x gt 0)
- num_pos num_pos 1
- else if (x lt 0)
- num_neg num_neg 1
- else
- num_zero num_zero 1
203.6 ???????????????????????? switch
- ???????????????????????? switch
????????????????????? ????????????????????????????
???????? ??????????????????? ???????????????????
(int) ???? ??????????????????(char)
???????????????????????????????(double)
??????????????????????????, ??????
?????????????????
21????????????????????????????????? switch
- ?????? switch (controlling expression)
-
- case lable_set1
- statements1
- break
- case label_set2
- statements2
- break
-
-
- case lable_setn
- statementsn
- break
- default
-
-
22??????????????????????????????????? switch
- ???????? switch (class)
-
- case B
- case b
- cout ltlt "Bat\n
- break
- case C
- case c
- cout ltlt "Cat\n
- break
- default
- cout ltlt "Unknown class \n ltlt class
-
233.7 ???????????????????? for
- ????????????????????????????????????
?????????????????????? ???????????????????????????
???? 3 ??????? - 1. ??????????????????????????????????
- 2. ???????????????????
- 3. ?????????????????????????????????????????
24?????????????????????????? for
- ?????? for (initialization expression
loop repetition condition
update expression)
25???????????????????????????? for
- ???????? /Display N asterisks/
- for (count 0
- count lt 10
- count 1)
- cout ltlt ""
- ???? for (count 0 count lt10 count
1) - cout ltlt ""
- ???????
-
26???????????????????????????? for
- ???????? /Display Round No./
-
- for (i 1 i lt5 i 1)
- cout ltlt Round ltlt i ltlt endl
- ???????
- Round 1
- Round 2
- Round 3
- Round 4
273.8 ???????????????????? while
- ??????????????????????????????????????? ?
????????????????????????? ???? - ???????????????????????????????????????? (????)
??????????????????????????????????? - ????????????????????????????????????????? (????)
??????????????????????????????????
???????????????????????????????????????? while ???
28?????????????????????????? while
- ?????? while (loop repetition condition)
- statement
29???????????????????????????? while
- ???????? /Display N asterisks./
- count 0
- while (count lt 10)
- cout ltlt "
- count count 1
- ???????
-
30???????????????????????????? while
- ???????? /Display Round No./
- i 1
- while (i lt 5)
- cout ltlt Round ltlt i ltlt endl
- i i 1
- ???????
- Round 1
- Round 2
- Round 3
- Round 4
313.9 ???????????????????? do/while
- ???????????????????? while ??? for
??????????????????????????????????????????????????
????? ????????????????? (???????????) ????????
??????????????????????????????????????????????????
????? 1 ????? ???????????????????????????????????
??????????? do-while ???
32?????????????????????????? do/while
- ?????? do statement
- while (loop repetition condition)
33???????????????????????????? do/while
- ???????? / Display Round No./
- i1
- do
- cout ltlt Round ltlt i ltlt endl
- i i 1
- while ( i lt 5)
- ???????
- Round 1
- Round 2
- Round 3
- Round 4
343.10 ?????? break ??? continue
- ?????? break ?????????????????????????????????????
?????????????????????????????? ??????????????
- break
- ?????? continue ??????????????????????????????????
?????????????????????? ???????????????????????????
???? ?????????????? - continue
353.11 Formulating Algorithm
- ????????? 1, ???????????????????????
- ????????????????????????????????? while ????
do/while ????????????????????????????????? ??????
(Counter) ????????????????????????????????????????
? ???????????????????????????????????
??????????????????????????????????????????????????
??
36????????????????? 1, ???????????????????????
- ?????
- ??????????????????????????????? 10
??????????????????????????????????????????????????
????????????????? 0 ??? 100 - ????????????????????????
- ????????????????????, ????????????????
???????????????????? ????????????????????????????
????????????????? ????????????????????????????
37????????????????? 1, ???????????????????????
- / Class average program with counter-controlled
repetition / - include ltiostream.hgt
- int main()
-
- int counter, grade, total, average
- / initialization phase /
- total 0
- counter 1
- / processing phase /
- while (counter lt 10)
- cout ltlt "Enter grade "
- cin gtgt grade
- total total grade
- counter counter 1
-
-
38????????????????? 1, ???????????????????????(???)
-
- / termination phase /
- average total / 10
- cout "Class average is " ltlt average ltlt endl)
- return 0 / indicate program ended
successfully /
39???????????????? 1, ???????????????????????
- Enter grade 98
- Enter grade 76
- Enter grade 71
- Enter grade 87
- Enter grade 83
- Enter grade 90
- Enter grade 57
- Enter grade 79
- Enter grade 82
- Enter grade 94
- Class average is 81
403.12 Formulating Algorithm with Top-Down Stepwise
Refinement
- Top-Down Stepwise Refinement ?????????????????????
??????????????????????????????????????? - ???????? pseudocode ??????????????????????
????????????????????????? - ??????????????? ??????? pseudocode
??????????????????????????????????????????????????
???????????????????????????????????
41????????? 2 ???????????????????????????
- ????????????????????????????????? while
???? do/while ?????????????????????????????????
?????????? (sentinel value) ??????????????????????
????????????????????
42????????????????? 2, ???????????????????????????
- ?????
- ?????????????????????????????????????????????????
?????????????? 1 ?????????????????????????????????
??????? - ????????????????????????
- ????????????? ???????????????????????????????????
??????? ????????????????????????????????
?????????????????????????? ???????????????????????
????????????????????????
43?????????????????????????? 2
?????????????????? total ???????
0 ?????????????????? counter ???????
0 ???????????????????? While ?????????????????????
?????????????? ???????????????
total ??????????? counter ????????? ????????????
???? (????????????????????????????) If counter
?????????? 0 ???????? average ??????? total
??????? counter ????? average Else ????? No
grades were entered (??????????????????????????)
44????????????????? 2, ???????????????????????????
- / Class average program with sentinel-controlled
repetition / - include ltiostream.hgt
- int main()
-
- float average / new data type /
- int counter, grade, total
- / initialization phase /
- total 0
- counter 0
- / processing phase /
- cout ltlt "Enter grade, -1 to end "
- cin gtgt grade
- while (grade ! -1)
- total total grade
- counter counter 1
- cout ltlt "Enter grade, -1 to end "
- cin gtgt grade
-
-
45????????????????? 2, ???????????????????????????(?
??)
- / termination phase /
- if (counter ! 0)
-
- average (float) total / counter
- cout ltlt Class average is ltlt average
-
- else
- cout ltlt "No grades were entered\n"
- return 0 / indicate program ended successfully
/ -
46???????????????? 2, ???????????????????????????
- Enter grade 75
- Enter grade 94
- Enter grade 97
- Enter grade 88
- Enter grade 70
- Enter grade 64
- Enter grade 83
- Enter grade 89
- Enter grade -1
- Class average is 8250
47????????? 3 ??????????????????????
- ????????????????????????????????????????????
?????????????????????????? ? ???
??????????????????????????????????????????????????
?????????????????????????????????????
48????????????????? 3, ??????????????????????
- ?????
- ????????????????????????????????????????????????
???????????????????????????????
??????????????????????????? 10 ?? - ???????????????????????? ???????????????????????
1 ????????? - ?????????????????????? ???????????????????????
2 ????????? - ?????????????????????????? 8 ?????????
??????????????? Raise tuition - ????????????????????????
- ??????????? (??????? ??? 1 ???? 2) ??????
??????????? Enter result (???????????)
??????????????????????????????????????????????????
?? - ????????????????????????? (?????????)
- ???????????????? ????????????????????????????????
????????????????????? - ?????????????????? 8 ????????? ???????????????
Raise tuition (????????????????)
49?????????????????????????? 3
?????????????????? passes (???????????????)
??????? 0 ?????????????????? failures
(?????????????) ??????? 0 ??????????????????
student counter ??????? 1 While student counter
??????????????????? 10 ?????????????????????? I
f ??????????????? ????????????????
passes Else ????????????????
failures ???????????????? student
counter ????????????? passes ?????????????
failures If ??????????????? 8 ????????? ?????
Raise tuition
50????????????????? 3, ??????????????????????
- / Analysis of examination results /
- include ltiostream.hgt
- int main()
-
- / initializing variables in declarations /
- int passes 0, failures 0, student 1,
result - / process 10 students counter-controlled loop
/ - while (student lt 10)
-
- cout ltlt "Enter result (1pass, 2fail) "
- cin gtgt result
- if (result 1) / if/else nested in while
/ - passes passes 1
- else
- failures failures 1
- student student 1
-
51????????????????? 3, ??????????????????????
- cout ltlt Passed ltlt passes
- cout ltlt "Failed " ltlt failures)
- if (passes gt 8)
- cout ltlt "Raise tuition\n"
- return 0 / successfully termination /
-
52???????????????? 3, ??????????????????????
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 2
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Enter Result (1pass, 2fail) 1
- Passed 9
- Failed 1
- Raise tuition