next_inactive up previous


Assignment 3

CS 135: Computer Science I

Spring 2008

Objectives

  1. Learn and demonstrate use of C/C++ data types
  2. Learn and demonstrate use of the C/C++ math library
  3. Learn and demonstrate use of the rand() function
  4. Learn and demonstrate prototyping, defining, and calling simple value returning functions.

Golf (10 points)

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 ($v_0$) and initial throw angle ($a_0$), 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 $g == 9.8m/sec^2$, on the Moon $g == 1.67m/sec^2$, and on Mars $g == 3.71m/sec^2$.

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

Clark and Lois play DDR (10 points)

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.

\begin{displaymath}
100 - 100 \frac{i}{i + c}
\end{displaymath}

where $c$ is the number of correct dance steps, and $i$ is the number of incorrect dance steps. You must use the same value returning function from the last assignment to compute the score and return this to main. Note that your program does not read anything from standard input. Finally, in addition to individual scores, your program must print the sum of Clark and Lois' scores on standard input. Run your program at least twice and show that your random number generator results in different scores being generated.

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

Handing it in

Turn in a Folder (Binder) containing:
  1. Cover sheet with
    1. Assignment Number
    2. Section Number
    3. Your name
    4. Your email
    5. Your TA's name
  2. Source code (.cpp) file (s) on a CD/USB stick with your name and section number written on your CD/USB stick.
  3. Hardcopy of your source code (printout)
  4. A printout of sample runs of your program on (at least) the test cases listed above.
Ask an instructor or TA if you have questions.

About this document ...

Assignment 3

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


next_inactive up previous
Sushil Louis 2008-02-11