CS 135: Computer Science I
Spring 2008
Design, code, and test a complete C/C++ program that computes
trajectory information about a golf ball hit by a good golfer on
Earth, Mars, and the Moon. You are to read the initial velocity
(
) and initial throw angle (
), in degrees, from standard
input. Both of these values will be of type double. Your program
will compute and write the following values to standard output.
Note that you will have to convert from degrees to radians because the C/C++ math library functions expect angles in radians and you will be reading them in degrees. You can google "degrees to radians" to find out how to convert from degrees to radians.
On Earth
, on the Moon
, and on
Mars
.
There are several constraints on this part of the assignment. First, you must use the following main function:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
const double EARTH_GRAVITY = 9.8;
const double MOON_GRAVITY = 1.67;
const double MARS_GRAVITY = 3.71;
void printOutput(string where, double h, double t, double d);
double hitDistance(double v, double a, double t);
double airTime(double v, double a, double g);
double getMaxHeight(double v, double g);
double degreesToRadians(double degrees);
int main(){
cout << "Enter initial velocity" << endl;
double v0;
cin >> v0;
cout << "Enter initial hit angle" << endl;
double a0;
cin >> a0;
a0 = degreesToRadians(a0);
double height, time, distance;
height = getMaxHeight(v0, EARTH_GRAVITY);
time = airTime(v0, a0, EARTH_GRAVITY);
distance = hitDistance(v0, a0, time); // time was just computed above
printOutput("On Earth", height, time, distance);
height = getMaxHeight(v0, MARS_GRAVITY);
time = airTime(v0, a0, MARS_GRAVITY);
distance = hitDistance(v0, a0, time);
printOutput("On Mars", height, time, distance);
height = getMaxHeight(v0, MOON_GRAVITY);
time = airTime(v0, a0, MOON_GRAVITY);
distance = hitDistance(v0, a0, time);
printOutput("On the Moon", height, time, distance);
return 0;
}
You may use the system("PAUSE") function call before the return 0.
Second, run your program for a Tiger Woods hit golf ball which starts traveling at about 270 kilometers per hour at an angle of 12 degrees. This will tell you how far his golf ball travels in the air before it hits the ground for the first time on Earth, Mars, and the Moon. Note that you have to convert from kilometers per hour to meters per second. Google will tell you how.
Here is sample interaction with your program.
sushil@xpc:~/samba/classes/135/assignments/as3/code$ ./golf
Enter initial velocity
40
Enter initial hit angle
20
On Earth
Max Height : 81.6327
Travel time: 5.584
Distance : 209.89
On Mars
Max Height : 215.633
Travel time: 14.7502
Distance : 554.426
On the Moon
Max Height : 479.042
Travel time: 32.7684
Distance : 1231.69
Design, code, and test a complete C/C++ program that generates information about game-players and computes their game scores for a game like "Dance, Dance, Revolution (DDR) for "Clark" whose game handle is "spr" and "Lois" whose game handle is "dpr." In this assignment, you will use a random number generator (rand()) to generate the number of correct dance steps and the number of missed dance steps. The number of correct or incorrect dance steps must be between 1 and 1000.
The program should then print names, handles, score information, and
scores. We compute the score using the same formula used in assignment
2.
Here is sample interaction with your program.
sushil@xpc:~/samba/classes/135/assignments/as3/code$ ./ddrScore
Clark spr 504 54 90.32
Lois dpr 748 855 46.66
Together Lois and Clark scored: 136.985
sushil@xpc:~/samba/classes/135/assignments/as3/code$ ./ddrScore
Clark spr 384 791 32.68
Lois dpr 641 282 69.45
Together Lois and Clark scored: 102.128
This document was generated using the LaTeX2HTML translator Version 2002-2-1 (1.71)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -split 1 as3S08
The translation was initiated by Sushil Louis on 2008-02-11