# Assignment 1

Solution

## Important dates

 Assigned 18 Jan 2017 @ 01:17h Due 23 Jan 2017 @ 23:59h

## Description

Implement a function that has been declared for you but not defined.

Given the following declaration:

/**
* Calculates the time required for an accelerating object to reach a
* particular distance.
*
* @param    a     the (constant) acceleration of the object [m/(s*s)]
* @param    v     initial velocity [m/s]
* @param    d     final displacement [m]
*
* @returns  the (positive) time required to reach displacement d
*/
double accelTime(double a, double v, double d);


define the accelTime function in a file called assign1.cpp. You may find it helpful to know that there is a function called sqrt declared in the cmath header file that calculates a square root when passed a real number as an argument.

## Results

The top-level result for this assignment was that over half of the submissions I received earned full marks on the functional evaluation (the TAs will also grade for style, but that will take a little longer to complete). As you can see from the graphs above, several submissions would not compile due to syntactic errors, and a few people submitted files that were not C++ source files (instead they were, e.g., zip files). I’ve detailed some of the common errors that I saw below.

### Common semantic errors

Not returning a value
A few submissions calculated the time correctly, but instead of returning that value, printed it out and returned 0. When a function’s contract says that it retuns a value, that value should be returned with a return statement.
Changing the order of parameters
If my test code calls accelTime(-9.8, 10, 1), it depends on the contract established on the assignment page. This includes the order of parameters for the accelTime function: first acceleration, then initial velocity, then displacement. If you change the order of these parameters then the wrong values will end up in the wrong place.
Using incorrect mathematics
It was necessary in this assignment to use the quadratic formula, as there were both velocity and acceleration at play. If you didn’t recall this from Physics, I did mention it in the lecture notes / exercises. The correct expression for time is thus: $$\frac{-v + \sqrt{v^2 + 2ad}}{a}$$ in math terms or (-v + sqrt(v*v + 2*a*d)) / a in C++, where a is acceleration, v is initial velocity and d displacement. An incorrect expression could lead to simply incorrect values or something more dramatic like calculating the square root of a negative number, which will yield a result of nan (“not a number”).

### Common build errors

“Undefined symbols for […] accelTime(double, double, double)”
This often means that the accelTime function was not defined in the submitted source file. For example, if you didn’t implement accelTime but instead just did some calculations in main and printed the result, then my test code, looking for an accelTime function, would fail to build. If the missing function was due to a more trivial error like a misspelling of the function name (acceltime or acelTime instead of accelTime), please come see me and we can talk about possible resolutions.
“error: use of undeclared identifier ‘cout’; did you mean ‘std::cout’?”
This means that you were missing either #include <iostream> or else using namespace std at the top of your source file. This omission should’ve been caught when you tried to compile the code on your own computer… if it wasn’t, perhaps you could come see me to talk about it.
“error: use of undeclared identifier ‘sqrt’”
This means that you probably didn’t #include <cmath> in your source file before you called the sqrt function. As I mentioned on the assignment page, the sqrt function is declared in the cmath header file, so you must #include <cmath> before you can use it.
“error: expected unqualified-id”
If you saw this error on the line beginning containing the opening brace of your function’s body, you may have had a semicolon at the end of the function prototype, which should only be there for declarations, not definitions. Again, this should’ve been observable when you compiled the code on your own computer, but if not, perhaps you could come talk to me about it.