CS-211 Homework 02 - graphBall.c
Write a C program called "graphBall.c" which prints out a primitive graph of the path of a ball thrown in the air. The horizontal axis of the graph is a time axis. The vertical axis should show the height of the ball (in meters) as it is thrown in the air with some initial velocity. The intent of this homework is to get some experience coding and invoking functions in C.
To start with, define the following constants that you will use in your program:
#define RMIN 0
#define RMAX 20
#define CMIN 0
#define CMAX 75
#define GRAVITY 9.81
#define TSCALEFACT 20.0
#define HSCALEFACT 4.0
These constants should appear after the #includes for your program, but before the function declarations.
Next, you should declare the lower level functions you will need for this program. For this assignment, you must write "right-side up" code, so these functions must be declared at the top of the file. I suggest you include at least the following lower level functions. (You may add more if you want to, but these are required.)
void printRow(float v0,int r);
void printScaleRow();
float tscale(int c);
float hscale(int r);
float height(float v0, float t);
There is more detail about what each function should do below.
- int main(int argc,char **argv)
- The main function should perform the following tasks:
- Check to make sure there are is at least one command line argument, other than the command itself. If not, print an error message and return a non-zero return code.
- Convert the first command line argument to a floating point value using the atof() library function. (You will need to include stdlib.h to use this function.) The result should be kept as the initial velocity.
- Loop using an integer control variable for the row, starting at RMAX, and decrementing by 1, as long as the row is greater than or equal to RMIN. In the body of this loop, invoke the printRow function, passing in the initial velocity, and the row as arguments. This will print a single row of your graph.
- Return 0 to indicate that everything worked as planned.
- void printRow(float v0,int r)
- This function prints a single row of the graph, where you can think of the parameter, r, as the row number. If this is row zero, print the horizontal axis by invoking the printScaleRow() function. That's all you need to do for row zero. If it's not row zero loop through each column of the row and deciding what character to print at that column. To do this, loop through columns from CMIN to CMAX, incrementing by 1. Within the loop body, perform the following:
- Determine the height of the ball at this time by invoking the height() function. The first argument of the height function is the initial velocity, which was passed as a parameter to printRow. The second argument to height is the floating point time. You can determine the floating point time by scaling the column number using the tscale function. Save the result of the height() function in a variable.
- If the height of the ball is greater than the height of this row (and you can get the height of the row by invoking hscale(h)) then you should print a single asterisk (*) to the screen.
- If the height of the ball is less that the height of this row, and if the column is zero, you need to print the vertical axis of the graph. This can be done as follows:
- Calculate the current height of this row by invoking the hscale(h) function. Save the result in a variable.
- Get the integer part of that variable by assigning the floating point variable to an integer variable.
- If the absolute value of the difference between the floating point height value and the integer part of that floating point is less that 1e-4, then print the integer part of the floating point height value. You may use the fabs() library function to compute the floating point absolute value, but if you do, you will need to include math.h.
- If you are not close to an even integer height, you should print a single vertical bar (|) for the vertical axis.
- If the height of the ball is less than the height of this row, and the time is not zero, you should print a single blank.
After the loop, the printRow function should print a newline (\n).
- void printScaleRow()
- The printScaleRow prints the horizontal scale for your graph. It does this using a very similar loop through the columns as you used in the printRow function. However, we need to print different values. Implement the body of the loop as follows:
- If the column is zero, print a single plus sign (+). That's all you need to do. If not...
- Calculate and save the floating point time value associated with this column by invoking the tscale() function.
- Assign the result to an integer variable to get the integer part of the floating point time value.
- If the absolute value of the difference between the floating point time value and the integer part of that value is less than 1e-4, then print the integer part of the time value.
- If not, just print a dash (-).
Don't forget to print a newline after the loop is done.
- float tscale(int t)
- This just scales the column number into a floating point time value by dividing the parameter by TSCALEFACT and returning the result.
- float hscale(int h)
- This just scales the row number into a floating point height value by dividing the paramter by HSCALEFACT and returning the result.
- float height(float v0, float t)
- This function determines the height of the ball at floating point time, t, given the initial velocity, v0. If you remember the in-class demo about ball height, we calculate this with the simple formula:
v0 * t - ( GRAVITY * t * t / 2)
We also made the assumption that we caught the ball once it returned, so there was no negative height. If the formula returns a negative number, you should just return a zero.
Once you have coded and tested, here's an example of what your output should look like:
>./graphBall 8
5
|
|
|
4
|
|
| **
3 *********
| *************
| ****************
| ******************
2 ********************
| **********************
| ************************
| **************************
1 ***************************
| *****************************
| ******************************
|*******************************
+-------------------1-------------------2-------------------3--------------
Submission
In myCourses, got to the CS-211 A0 class, open "Contents", then "Homework Submissions", then "Homework 02 Submission". Upload your "graphBall.c" file in the "Attach Files" are of the submission page. Don't forget to click on the "Submit" button at the bottom of the page.
You may submit as many times as you want before the due date, but only the last submission will be graded. Any previous submissions will be ignored.
Grading Criteria
This homework is worth 10 points. You will get the full 10 points unless:
- -7 If your program does not compile.
- -2 If your program causes warning messages when it compiles.
- -2 For not submitting correctly (incorrect program name, etc.)
- -2 For not handling command line parameters correctly.
- -2 For changing the constant values (and not changing them back before submitting) because this causes extra work when grading.
- -6 Maximum deduction for not printing a correct graph (partial credit will be given)
- -3 for every 24 hours your program is late.
- -10 If your program matches another student's program. Everyone's interests should be unique.