50
Computer Science 1620 Math Library

Computer Science 1620

  • Upload
    haile

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

Computer Science 1620. Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years. - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Science 1620

Computer Science 1620

Math Library

Page 2: Computer Science 1620

Remember this program?suppose that I invest $25000 into a mutual

fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years

Page 3: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years

#include <iostream> using namespace std;

int main() {

cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 4: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

balance = balance * 1.08; cout << "Year 1: $" << balance << endl;

balance = balance * 1.08; cout << "Year 2: $" << balance << endl;

balance = balance * 1.08; cout << "Year 3: $" << balance << endl;

return 0;

}

Page 5: Computer Science 1620
Page 6: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years

#include <iostream>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 7: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years

#include <iostream>#include <iomanip> using namespace std;

int main() {

double balance = 25000.0; cout << fixed << showpoint << setprecision(2);

balance = balance * 1.08; cout << "Year 1: $" << balance << endl;

balance = balance * 1.08; cout << "Year 2: $" << balance << endl;

balance = balance * 1.08; cout << "Year 3: $" << balance << endl;

return 0;}

Page 8: Computer Science 1620
Page 9: Computer Science 1620

Suppose we alter the program?suppose that I invest $25000 into a mutual

fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

Page 10: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

#include <iostream>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 11: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

#include <iostream>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 12: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

#include <iostream>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 10: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 13: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

#include <iostream>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 10: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl; cout << "Year 20: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl; cout << "Year 30: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 14: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years#include <iostream>#include <iomanip> using namespace std;

int main() {

double balance = 25000.0; cout << fixed << showpoint << setprecision(2);

balance = balance * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08; cout << "Year 10: $" << balance << endl;

balance = balance * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08; cout << "Year 20: $" << balance << endl;

balance = balance * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08; cout << "Year 30: $" << balance << endl;

return 0;}

Page 15: Computer Science 1620
Page 16: Computer Science 1620

The previous example worked, but … to calculate 1.0830 required that we manually

type in thirty multiplicationssuppose we had wanted to know the

balance after 100 years?

Page 17: Computer Science 1620

pow(b,e) to compute b raised to the power of e, one can use

the pow function syntax:

the pow statement itself is an expression value: the first expression raised to the power of the

second expression

pow( , )numericexpression

numeric expression

Page 18: Computer Science 1620

Example:#include <iostream>#include <cmath> using namespace std; int main() {

cout << pow(3.0, 3.0) << endl; cout << pow(3, 4.0) << endl; cout << pow(4.0, 3) << endl; int x = 5; double y = 6.0; cout << pow(x, y) << endl; cout << pow(x, y) + pow(3.0, 3.0) << endl; cout << pow( pow(x, y), pow(3.0, 3.0) ) << endl; return 0;

}

Page 19: Computer Science 1620

A note about powyou cannot send it two integer expressions

cout << pow(3, 4) << endl; // compiler error

this has to do with function overloadingwe will examine functions later

for now, just know that one expression has to be a floating point type

Page 20: Computer Science 1620

The cmath libraryFunction Descriptionabs(x) Returns the absolute value of x

pow(x, y) Returns xy

sqrt(x) Returns the square root of x

sin(x) Returns the sine of x

cos(x) Returns the cosine of x

tan(x) Returns the tangent of x

log(x) Returns the natural logarithm of x ln x

log10(x) Returns log10 x

exp(x) Returns ex

floor(x) Returns the closest whole number < x

ceil(x) Returns the closest whole number > x

Page 21: Computer Science 1620

A Note about Types when using a math function (from previous slide), the type of

the expression is the same as the type of the input expression, with some exceptions:

an integer expression is converted to a double for the pow function, the type of the expression is the largest of

the base and exponent expressions for abs

integers are not converted to doubles shorts and chars are converted to ints

Page 22: Computer Science 1620

Expression Type

sqrt(2.0) double

sin(2.0f) float

cos(2) double

log(3 + 4.0f) float

exp(2) + exp(2.0) double

abs(75); int

floor(3.5); double

Page 23: Computer Science 1620

A Note about the Trig Functions the input expression is considered to be

radians

hence, sin(30.0) = -0.988032, not 0.5

to convert radians to degrees, multiply the value by (π / 180)

Page 24: Computer Science 1620

Example:#include <iostream>#include <cmath> using namespace std; int main() {

const double PI = 3.14159;

cout << sin(30) << endl;

cout << sin(30 * PI / 180) << endl; return 0;

}

Page 25: Computer Science 1620
Page 26: Computer Science 1620

Back to our previous examplesuppose that I invest $25000 into a mutual

fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

Page 27: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

#include <iostream>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 10: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl; cout << "Year 20: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl; cout << "Year 30: $" << 25000 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 << endl;

return 0;

}

Page 28: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years

#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

cout << fixed << showpoint << setprecision(2); cout << "Year 10: $" << 25000 * pow(1.08, 10) << endl; cout << "Year 10: $" << 25000 * pow(1.08, 20) << endl; cout << "Year 10: $" << 25000 * pow(1.08, 30) << endl;

return 0;

}

Page 29: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years#include <iostream>#include <iomanip> using namespace std;

int main() {

double balance = 25000.0; cout << fixed << showpoint << setprecision(2);

balance = balance * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08; cout << "Year 10: $" << balance << endl;

balance = balance * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08; cout << "Year 20: $" << balance << endl;

balance = balance * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08 * 1.08; cout << "Year 30: $" << balance << endl;

return 0;}

Page 30: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

double balance = 25000.0; cout << fixed << showpoint << setprecision(2);

balance = balance * pow(1.08, 10); cout << "Year 10: $" << balance << endl;

balance = balance * pow(1.08, 10); cout << "Year 20: $" << balance << endl;

balance = balance * pow(1.08, 10); cout << "Year 30: $" << balance << endl;

return 0;}

Page 31: Computer Science 1620

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) ten years, b) twenty years c) thirty years#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

double balance = 25000.0; double increase = pow(1.08, 10); cout << fixed << showpoint << setprecision(2);

balance = balance * increase; cout << "Year 10: $" << balance << endl;

balance = balance * increase; cout << "Year 20: $" << balance << endl;

balance = balance * increase; cout << "Year 30: $" << balance << endl; return 0;}

Page 32: Computer Science 1620

Example 2: Suppose you have a cannon. Given an initial velocity of a cannonball and the angle of the cannon θ, output (1) the time that the ball remains in the air and (2) the maximum height of its trajectory

Page 33: Computer Science 1620

Arithmetic Data and Operators

Given a value θ and initial velocity v0, we can calculate the time that the ball stays in the air:

and its maximum height:

secsin2 0

gvt θ−

=

metersg

vh2

)sin( 20 θ−

=

• g is approximately -9.81 m/s at sea level

Page 34: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 35: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 36: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 37: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 38: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 39: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

secsin2 0

gvt θ−

=

Page 40: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / -9.81; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

secsin2 0

gvt θ−

=

cmathcall

Page 41: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / -9.81; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

secsin2 0

gvt θ−

=

MagicNumber

Page 42: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

secsin2 0

gvt θ−

=

Expects radians

Page 43: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * 3.14159 / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

secsin2 0

gvt θ−

=

MagicNumber

Page 44: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

secsin2 0

gvt θ−

=

Page 45: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

metersg

vh2

)sin( 20 θ−

=

Page 46: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

metersg

vh2

)sin( 20 θ−

=

Page 47: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 48: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(2); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 49: Computer Science 1620

Write a program that accepts an initial velocity and the angle of trajectory, and computes the hangtime and the maximum height of the cannonball. Output to three decimal places.#include <iostream>#include <cmath>#include <iomanip> using namespace std;

int main() {

const double PI = 3.14159; const double G = -9.81;

double v0; cout << "Initial velocity (m/s): "; cin >> v0; double theta; cout << "Angle of trajectory (degrees): "; cin >> theta; theta = theta * PI / 180.0; double time = -2 * v0 * sin(theta) / G; double height = -pow(v0 * sin(theta), 2) / (2 * G); cout << fixed << showpoint << setprecision(3); cout << "Hangtime (s): " << time << endl; cout << "Height (m): " << height << endl; return 0;}

Page 50: Computer Science 1620