## 3.7 End of Chapter Exercise

### Conditional

1. Write a function f(x) that returns absolute value of x.
• If x is positive, then f(x) = x.
• If x is negative, then f(x) = -x.
2. Write a function f(x) that returns grade point average (GPA) given exam score x.
• If x=90, then GPA is 5.
• If x is between 80 and 90, then GPA is 4.
• If x is between 70 and 80, then GPA is 3.
• If x is less than 70, then GPA is 2.
3. Write a function f(x,y) that returns grade using test score x and exam score y.
• If x+y is no less than 90 and y is no less than 40, then grade is A+.
• If x+y is no less than 80 but less than 90, then grade is A.
• If x+y is no less than 70 but less than 80, then grade is A-.
• If x+y is less than 70, then grade is B+.

### While loop

1. Use while loop to calculate the sum of all numbers from 50 to 100.

2. Use while loop to calculate the product of all numbers from 50 to 100.

### For Loop

1. Use for loop to calculate the sum of all numbers from 50 to 100.
2. Use for loop to calculate the product of all numbers from 50 to 100.

### Application

1. Write a MACD function that returns both macd and signal line using four inputs:
• price: price vector
• S: number of periods for fast moving average
• L: number of periods for slow moving average
• K: number of periods for signal moving average