3 methods
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 |
ints are type cast as doubles when
used
with other doubles.char and bool can be typed cast as int.
Similarly, int can be type cast as char or int.
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 |
50% midterm +
50% final and 40% midterm + 60% final. 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. |
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. |
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 |
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.

powpowdouble double<cmath>. That is why we have #include<cmath>
at the beginning of the program.pow(x,y) return x to the power y. The
order of the arguments are important!#include to include the header,
e.g. #include<cmath>
sqrt, pow, log, log10, abs, exp, sin, cos, tan, , acos, asin, atan, cosh, sinh, tanh, labs, fabs, ceil, floorlabs is the absolute value function of a long fabs is the absolute value function of a floatfloor: the largest integer integer smaller than or equal to the parameter. ceil: the smallest integer bigger than or equal to the parameter. 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. |