# Integrated exercises III

(総合演習 III)

## Computing Practice I

### 14th lecture, July 17, 2017

http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture14.html

### Martin J. Dürst

© 2005-17 Martin J. Dürst 青山学院大学

# Today's Schedule

• Remaining Schedule
• Today's exercises

# テストなし、ログイン不要

ログインした学生は授業開始前にログオフすること

# Remaining Schedule

• July 20: Integrated exercises IV
• July 27, 14:50-16:15: Term final exam (85 minutes)

# 前回の演習結果

 1301 1302 1303 1304 100 points 68 39 1 9 60 points 24 51 44 42 errors 1 1 6 - not submitted 6 8 48 48

Total of problems solved: 4 problems: 32 students, 3 problems: 16 students, 2 problems: 45 students, 1 problems: 2 students, 0 problems: 1 student

Number of students who did not complete a single problem before 18:30: 22 students

# Important Points for Application Exercises

• DRY! DRY!! DRY!!!
(caution: DRY ≠ code golf)
• Prefer data over code
(Example: Use initialization instead of functions)
• Data structures matching problem structure
(arrays, structures, combinations of arrays and structures)
• Easy to understand variable names (not just `a, b, c,`...)
• If labels are needed (e.g. "beef", "shrimp",...), make them part of the data
• Check which data needs to be kept across which loops

# Function vs. Initialization

Using functions:

```int shrimp(int protein,int calcium)
{
int price;
price = protein+calcium;
if (price>=20) return price-20;
else           return 0;
}```

Using initialization:

```#define FOODS 8
#define NUTRIENTS 5
typedef struct { int nutrient[NUTRIENTS]; int price; } Food;
int main(void)
{
Food foods[FOODS] = { { { 1, 1, 0, 0, 0}, 22 },
{ { 0, 1, 1, 0, 0}, 20 },
...
};
...
}```

# Selection of Data Structures (Example: 1303)

Data across multiple trials: none

Data common to all investment plans: duration, interest rate ⇒ single variables

Number of investment plans: Maximum 20 ⇒ array

Data per investment plan: initial investment, sales profit, operating expenses, net revenue, net present value, plan number ⇒ `struct`

# 応用課題演習 III

• 1401: カードのゲーム (複数回)
• 1402: 暗号化 (バイナリファイル入出力の応用)
• 1403: 自動販売機: 他国の紙幣構造 (例: 25¢ 硬貨) にも簡単に対応可能で DRY な設計!
• 1404: 単語の統計

Q&A フォーラムの活用! 題名に注意!
(問題文の詳細や解き方についての質問も勿論可能)

# 次回への準備

• 足りない演習問題を完成し、提出
• 復習、期末試験の準備