PIC10A Lecture 7

Reading

p92-103

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 << static_cast<double>(a)/b << endl;
return 0;
}

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

Inplicit type casting

#include<iostream>
using namespace std;

int main() {
bool b = 3;
cout << b << endl;
char ch = 72;
cout << ch << endl;
return 0;
}

Output
1
H
The ascii value of H is 72. Refer to Appendix 3 of Savitch.

Function

Question

#include<iostream>
using namespace std;

int 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";
}

return 0;
}

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

A better approach

Break down the problem into subtasks:
#include<iostream>
using namespace std;

double finalScore(double midterm, double exam);
char grade(double score);

int 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";

return 0;
}


// 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';
}
}
The program will be explained in more details during the class.

Predefined functions

Question

1) Find the square root of 2.
#include<iostream>
#include<cmath>
using namespace std;

int main() {
cout << "The square root of 2 is " << sqrt(2) << endl;
return 0;
}
Output
The square root of 2 is 1.41421

2) Find 2.25 to the power 3. (i.e. the cube of 2.25)
#include<iostream>
#include<cmath>
using namespace std;

int main() {
cout << "2.25 to the power 3 is " << pow(2.25,3) << endl;
return 0;
}
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

sqrt, pow, abs,labs, fabs, ceil, floor, exit, rand, srand.
Refer to the textbook, p96.