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<type>(expression)
Examples:
char(75);
(double)3;
static_cast<double>(3);

Example

#include<iostream>
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

Example

#include<iostream>
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

Example

#include<iostream>
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:

Example

#include<iostream>
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<iostream>
#include<cmath>
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<iostream>
#include<cmath>

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.

ring

Parts of a function

In order to use a function, you should know

Example :pow

How to use the function?

List of math functions

Example

#include<iostream>
#include<cmath>

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.