Read the assignment and pick out the specific bits of code that you must write. Create pseudo-code for each one. I often use C++ comments. When you're done, read the assignment again and see if you've missed anything. Repeat the process until you're sure that have everything needed. Then you should be able to just fill in code for the comments. I'll get you started:
Prompt the user for the bounds between which to search for a root. |
1 2 3
|
double left, right;
cout << "enter the limits: " << flush;
cin >> left >> right;
| |
Write a function to handle this input from the user. |
Oops, so that's
1 2 3 4 5 6 7 8 9 10 11
|
void getBounds (double &left, double &right)
{
cout << "enter the limits: " << flush;
cin >> left >> right;
}
int main()
{
double left, right;
getBounds(left,right);
...
| |
Also write a function to find x, given t and name it fcn(). |
1 2 3 4
|
double fcn(double t)
{
// compute x(t) and return it.
}
| |
Hardcode as constants the values for m and x0 based on a 3000 lb car and a
10 inch pothole. Convert these measurements to the required units carefully. |
1 2
|
const double m = 3000; // TODO: change unilts
const double x0 = 10; // TODO: change units
| |
The following third function is provided. Study it until you understand exactly what is does. |
So we'll need this in the code exactly as-is:
1 2 3 4 5 6 7 8 9 10
|
bool bisect(double& left, double& right) {
if (fcn(left)*fcn(right) > 0)
return 1;
double mid = (left + right)/2;
if (fcn(left)*fcn(mid) < 0)
right = mid;
else
left = mid;
return 0;
}
| |
Editorial: why the heck is the prof returning 1/0 from a bool function?? It should be true/false!
TODO: study that code and figure out what it does. Add comments.
Follow the console output format shown below...: |
TODO: change the prompts in getBounds to match this:
Please enter the lower limit: 0.08
Please enter the upper limit: 0.13 |
So now maybe main is:
int main()
{
count << "HW7 Root finding\n";
getBounds(left, right);
cout << "The root is" << solve(left, right) << '\n';
cout << "Detailed results can be found in HW7Results.txt in the project folder.\n"
}
and solve is:
double solve(double left, double right);
Sweet! Put this together into a program. For now, just have solve() return some number. Get the program to compile and run. Now you have a program that reads the numbers and calls the solver.
Next, code up fcn() to compute the value of the function. Add some temporary code to evaluate it at a few points and check your results.
Then write code for solve(). Be sure to write the output file data. This will help you debug solve().
When you're done, re-read the assignment again. Carefully check to make sure you've done everything you're supposed to. Make sure that your output matches the requirements exactly.