# Math2220 Lecture 7

## Type Casting

A type cast is a way of changing a value of one type to the value of another type.

### Explicit type casting

 3 methods type(expression) (type)expression `static_cast`(expression) Examples: `char(75);` `(double)3;` `static_cast(3);`

Example

 ```#include using namespace std; int main() { int a, b; cin >> a >> b; cout << double(a)/b << endl; char line[100]; cin.getline(line,100); cin.getline(line,100); } ```

Output

 ```4 7 0.571429 ```
Question: What happens if you don't use type casting?

### Inplicit type casting

• `int`s are type cast as `double`s when used with other `double`s.
• `char` and `bool` can be typed cast as `int`. Similarly, `int` can be type cast as `char` or `int`.

Example

 ```#include using namespace std; int main() { bool b = 3; cout << b << endl; // 1 char ch = 72; cout << ch << endl; // H ch='A'; cout << int(ch) << endl; // 65, check ascii table int num = 99; cout << char(num) << endl; // c, check ascii table char line[100]; cin.getline(line,100); } ```

Sample output

 ``` 1 H 65 c ```
The ascii value of H is 72. Refer to ASCII table at wikipedia.

## Function

### Question

• Input : user's midterm and final score
• Final score is calculated by the maximum of ```50% midterm + 50% final``` and `40% midterm + 60% final`.
Grade is calculated by : A : 90-100, B : 80-90, C: 65-80, D: 50-65, F : under 50.

Example

 ```#include using namespace std; void main() { double midterm, exam; // score for midterm and final exam double final; // final result cout << "Please enter your midterm and final score: "; cin >> midterm >> exam; double result1 = 0.5*midterm + 0.5*exam; double result2 = 0.4*midterm + 0.6*exam; // the final score is the max of result1 and result2 if(result1 >= result2) final = result1; else final = result2; if (final >= 90) { cout << "Your grade is A.\n"; } else if (final >= 80) { cout << "Your grade is B.\n"; } else if (final >= 65) { cout << "Your grade is C.\n"; } else if(final >= 50) { cout << "Your grade is D.\n"; } else { cout << "Your grade is F.\n"; } char line[100]; cin.getline(line,100); cin.getline(line,100); } ```

Sample output

 ``` Please enter your midterm and final score: 89.5 99.2 Your grade is A. ```

#### A better approach

Break down the problem into subtasks:
• calculate the final score

Example

 ```#include using namespace std; double finalScore(double midterm, double exam); char grade(double score); void main() { double midterm, exam; // score for midterm and final exam double final; // final result cout << "Please enter your midterm and final score: "; cin >> midterm >> exam; final = finalScore(midterm, exam); cout << "Your grade is " << grade(final) << ".\n"; char line[100]; cin.getline(line,100); cin.getline(line,100); } // input: scores for midterm, exam // return: final score double finalScore(double midterm, double exam) { double result1 = 0.5*midterm + 0.5*exam; double result2 = 0.4*midterm + 0.6*exam; // the final score is the max of result1 and result2 if(result1 >= result2) return result1; else return result2; } // input: score // output: grade char grade(double score) { if (score >= 90) { return 'A'; } else if (score >= 80) { return 'B'; } else if (score >= 65) { return 'C'; } else if(score >= 50) { return 'D'; } else { return 'F'; } } ```

Sample output

 ``` Please enter your midterm and final score: 75.3 88.1 Your grade is B. ```
The program will be explained in more details during the class.

## Predefined functions

### Question

1) Find the square root of 2.

Example

 ```#include #include using namespace std; void main() { double x=2; cout << "The square root of " << x << " is " << sqrt(x) << endl; char line[100]; cin.getline(line,100); } ```

Output

 ```The square root of 2 is 1.41421 ```

2) Find 2.25 to the power 3. (i.e. the cube of 2.25)

Example

 ```#include #include using namespace std; void main() { cout << "2.25 to the power 3 is " << pow(2.25,3)<< endl; char line[100]; cin.getline(line,100); } ```

Output

 ```2.25 to the power 3 is 11.3906 ```
Pay attention to `sqrt(2)` and `pow(2.25,3)`. They are all functions. `sqrt(2)`returns the square root of 2. `pow(2.25,3)` returns the cube of 2.25.

### Parts of a function

In order to use a function, you should know
• Name
• Arguments: what gets plugged in
• Value returned
• Where does it locate: which library(cmath? iostream?).
• Definition: algorithm to determine value returned.

### Example :`pow`

• Name: `pow`
• Arguments: two `double`
• Value returned: `double`
• library : `<cmath>`. That is why we have `#include<cmath>` at the beginning of the program.
• Definition: `pow(x,y)` return x to the power y. The order of the arguments are important!

### How to use the function?

• Use the directive `#include` to include the header, e.g. `#include<cmath>`
• Know the number and types of argument
• Know what to expect as value returned
• Function call.

### List of math functions

• `sqrt, pow, log, log10, abs, exp, sin, cos, tan, , acos, asin, atan, cosh, sinh, tanh, labs, fabs, ceil, floor`
• `labs` is the absolute value function of a `long`
• `fabs` is the absolute value function of a `float`
• `floor`: the largest integer integer smaller than or equal to the parameter.
• `ceil`: the smallest integer bigger than or equal to the parameter.
• Check cmath (math.h) - C++ Reference

Example

 ```#include #include using namespace std; void main() { const double PI=3.14159265358; cout << "PI is " << PI << ".\n"; cout << "log(7.2) is " << log(7.2) << ".\n"; cout << "cos(PI/6) is " << cos(PI/6) << ".\n"; cout << "sin(10.5) is " << sin(10.5) << ".\n"; cout << "e to the power 5 " << exp(double(5)) << ".\n"; // don't forget type casting cout << "Cubic root of 10 is " << pow(10, 1./3.) << ".\n"; cout << "The integer part of 3.2 is " << floor(3.2) << ".\n"; cout << "The integer part of -3.2 is " << floor(-3.2) << ".\n"; cout << "The absolute value of -3.8 is " << abs(-3.8) << ".\n"; char line[100]; cin.getline(line,100); } ```

Output

 ``` PI is 3.14159. log(7.2) is 1.97408. cos(PI/6) is 0.866025. sin(10.5) is -0.879696. e to the power 5 148.413. Cubic root of 10 is 2.15443. The integer part of 3.2 is 3. The integer part of -3.2 is -4. The absolute value of -3.8 is 3.8. ```