(Print this page as a cover sheet for your printouts)
LAB 2
Section: ______
Due: 11:59 P.M. Tuesday, February 4, 2014
"On my honor, as an Aggie, I have neither given nor received anyunauthorized aid on any portion of the academic work included in thisassignment."
______
Typed or printed name of studentSignature of student
NOTE: Please follow your lab instructor's directions for submittingyour assignment through CSNET. ONLY ASSIGNMENTS SUBMITTED TO CSNET WILLBE GRADED! Make a printout of each source file and staple it behindthis cover sheet, unless your lab instructor directs otherwise. Sign itand give it to your TA in lab or put it in your TA's mailbox in thecorner of the 3rd floor of HRBB, near room 312. IF YOU DO NOT TURN IN ASIGNED COVER SHEET YOUR WORK WILL NOT BE GRADED!
NOTE: Homework will be graded on linux2.cse.tamu.edu using g++-4.7. You are free to develop your programs on Visual C++ or any other platform, but it is your responsibility to make sure your programs also compile andexecute correctly on linux2.cse.tamu.edu using g++-4.7.
The grade for this lab will be based on style (formatting, variablenames, comments, etc.), syntax (no compilation or link errors), andcorrectness (passes all test cases). Your grade for this lab is:
Problem #1 2 3 4
Style /4 /2 /4 /2
Syntax /6 /3 /6 /3
Correctness /10 /5 /10 /5
------
Total /20 /10 /20 /10
Grand total _____/50
- (20 points) Write your own cube root function named double my_cbrt_1(double n) using the following pseudocode:
x = 1
repeat 10 times: x = (2x + n / x2) / 3
returnx
and then write a main which prints n, cbrt(n), and my_cbrt_1(n) for n = 3.14159 times 10 to the kthpower for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which only works on linux2):
for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
n = 3.14159 * pow(10.0, k);
//cout goes here
}
Note: my_cbrt_1(n) is based on the Newton-Raphson algorithm. Name your program hw2pr1.cpp.
2. (10 points) Modify problem 1 to also print the relative error as a per cent, by adding a column relative_error_per_cent = 100 * ((my_cbrt_1(n) –cbrt(n)) / cbrt(n). Line up the columns using setw(), etc., and print a title over each column. Name your program hw2pr2.cpp.
3. (20 points) Write a C++ program to count the number of times the words “Whoop” or “whoop” or “Howdy” or “howdy” occur in the keyboard input. You may assume the input will contain only words separated by whitespace (i.e., no punctuation appears), and that the input will be terminated by an end-of-file character.Name your program hw2pr3.cpp. Hint: Consider the program in section 4.6.3 of the textbook.
OPTIONAL EXTRA CREDIT
======
4. (10 points) Modify problem 2 to scale n so the cube root will possibly be more accurate, as follows:
Name a function my_cbrt_2(n), which does thispseudocode:
Set result to 1.
If n is greater than 16/3, divide n by 8 and multiply result by 2;
repeat until n is not greater than 16/3.
If n is less than 2/3, multiply n by 8 and divide result by 2; repeat
untiln is not less than 2/3.
Return result times my_cbrt_1(n).
The main should print n, cbrt(n), my_cbrt_2(n), and the relative error of my_cbrt_2(n) as a per cent, in columns with headers. Name your programhw2pr4.cpp.