CS 111 Assignment 3
- Files to print out and bring to lecture and recitation
- Preparations before you begin your homework
- Practice problems
- Assigned programming problems
- Preparing to hand in your homework
This assignment is due Wednesday, March 15.
- Files to print out and bring to lecture and recitation
Below are tutorials and example programs and data files. Please make printouts of these and bring them with you to both lecture and recitation. However, please do NOT print them out in an on-campus lab. (On-campus printers are to be used only for your homework, i.e. for files YOU wrote.) If you do not have a computer at home, with a printer, ask a friend or classmate to print out copies of the following files for you.
- Tutorials:
- Examples to accompany the Tutorial on if, if/else, program design, testing, debugging, and garbage values
- busses.cpp
- oddOrEven1.cpp
- oddOrEven2.cpp
- passOrFail1.cpp
- passOrFail2.cpp
- compare3.cpp.txt, a line-numbered copy of compare3.cpp
- Code traces of compare3.cpp
- multibranchIfDemo.cpp
- nestedIfElseDemo1.cpp
- nestedIfElseDemo2.cpp
- nestedIfElseDemo3.cpp
- nestedIfElseDemo4.cpp
- Examples to accompany the Tutorial on characters, strings, and debugging
- Debugging exercises:
- age.cpp.txt, a line-numbered copy of age.cpp
- gradePoints.cpp
- Preparations before you begin your homework
On venus, create a directory named hw03 inside your homework directory. Change your present working directory to hw03 and then copy into it the example files for this homework assignment, by typing, at the prompt:
cp ~nixon/cs111/hw03/* .Please do NOT copy these files into your home directory, to avoid cluttering your home directory. If you inadvertantly copied them into your home directory, move them out using the mv command. Be very careful about deleting anything in your home directory, to avoid inadvertantly deleting your hidden files (.login, .cshrc, .profile, etc.).
Play with the example programs when reading the tutorials. Then, when you are ready to begin work on the homework assignment itself, it is recommended that you delete the example files, or at least most of them, in order to save disk space.
In both the practice problems and the assigned problems, pay careful attention to readability issues, such as indentation. You may lose up to 15 points (out of 100) for bad indentation, depending on how bad it is.
ANSWER NO MORE THAN ONE PROBLEM PER SHEET OF PAPER. At the top of each sheet, put (1) your for name (as known to the Registrar's office), (2) the last 4 digits of your student ID number, (3) "CS 111 Spring 2002 Assignment 3," and (4) your lab section..
- Practice problems
Answers to most of the practice problems are provided here. However, so that you will be adequately prepared for the next quiz, it is strongly recommended that you work on the problems yourself before looking at the answers. It is also recommended that you work on at least some of these practice problems before doing the assigned homework.
- Debugging exercises: Find and fix the errors in the following programs Test them to make sure they run properly.
- age.cpp.txt, a line-numbered copy of age.cpp
- gradePoints.cpp
- Write a program which promts the user to enter a total number of pennies and then outputs a money amount rounded to the nearest dollar. For example, 150 pennies would round up to $2, whereas 149 pennies would round down to $1. Use integers only; do not use floating-point numbers.
For this program, you should also submit handwritten code traces for some well-chosen boundary cases. (Code traces and boundary cases are discussed in the Tutorial on if, if/else, program design, testing, debugging, and garbage values.) Your program printout, therefore, should be line-numbered. If you are typing up your program on forbin, see the instructions in the tutorial on How to generate line-numbered copies of files.
- Write a program which prompts the user to enter four numbers and then outputs the largest of the four numbers. Use only the techniques we have covered so far (if and if/else but not loops).
- A quadratic equation, such as:
2 a x + b x + c = 0has two solutions (roots) given by the following formula:
+ /------------- -b - / discriminant \/ x = ---------------------- 2 a 2 where discriminant = b - 4 a c(Note: the above is algebra, NOT C++ code!)
Write a C++ program which prompts the user to enter the three coefficients a, b, and c, and then outputs the roots, as follows shown below. (In the following sample outputs, the output itself is in plain type, whereas the inputs typed by the user are in boldface type.)
- If the discriminant is positive, the program outputs the two real roots. For example, if a is 1, b is 0, and c is -1, the output should be:
This program finds roots of a quadratic equation. Enter the coefficients a, b, and c, separated by spaces:>1 0 -1 Two real roots: 1 and -1.- If the discriminant is zero, the program outputs the two identical real roots as a single number. For example, if a is 1, b is 2, and c is 1, the output should be:
This program finds roots of a quadratic equation. Enter the coefficients a, b, and c, separated by spaces:>1 2 1 Two identical real roots, both -1.- If the discriminant is negative, the program outputs the real and imaginary parts of the two complex roots. For example, if a is 1, b is 0, and c is 1, the output should be:
This program finds roots of a quadratic equation. Enter the coefficients a, b, and c, separated by spaces:>1 0 1 Two complex roots, where the real part is 0 and the imaginary part is plus or minus 1.Assume that the coefficients entered by the user will be real numbers (no imaginary part). Use type double for all quantities, and use the sqrt and abs functions declared in <cmath>. Use sqrt to find the square root of the discriminant and use abs to find the absolute value of the imaginary part of a complex root, to avoid outputs like:
and the imaginary part is plus or minus -2.
- The following program segment has very poor indentation:
if ( number1 < 0 ) if ( number2 < 0 ) cout << "+++++" << endl; else cout << "@@@@@" << endl; cout << "#####" << endl; cout << "$$$$$" << endl;Re-write it so that it has an acceptable form of indentation with the else indented by the same amount as the if to which it corresponds. Make no other changes to the program segment. Then, underneath your re-written program segment, write its output for each of the following four cases:
- a) number1 = -1 and number2 = -1
- b) number1 = -1 and number2 = 0
- c) number1 = 0 and number2 = -1
- d) number1 = 0 and number2 = 0
If you do not actually put the program segment in a program and compile and run it to see what its output actually looks like, write its output as you think it should appear. Write the output of the program segment only, not any larger program that might contain it.
- Modify the program segment in the previous problem so that it has the outputs given below, in the indicated cases. Make NO changes to the program segment other than (1) adding curly braces and (2) fixing the indentation to correspond with how the program is now organized.
- a) number1 = -1 and number2 = -1
+++++ $$$$$- b) number1 = -1 and number2 = 0
$$$$$- c) number1 = 0 and number2 = -1
@@@@@ ##### $$$$$- d) number1 = 0 and number2 = 0
@@@@@ ##### $$$$$
- Assigned programming problems
- (50 points) Write a program leapyear.cpp which prompts the user to enter a year number and outputs a statement indicating whether or not that year is a leap year. If the year number is a multiple of 4 but not a multiple of 100, then the year number is a leap year. If the year number is a multiple of 100 but not a multiple of 400, then the year number is not a leap year. If the year number is a multiple of 400, then the year number is a leap year. All other years are not leap years.
- (50 points) Write a program order4strings.cpp which prompts the user to enter four words and then prints out a listing of the four words in lexicographical order. (For an explanation of "lexicographical order," read the Tutorial on characters, strings, and debugging.) The words may have been entered by the user in any order, and must be rearranged by the program into lexicographical order.
- Preparing to hand in your homework
The following files should be emailed to your instructor (all attached, together, to one email message):
- leapyear.cpp,
- order4strings.cpp,
To recitation/lab on the due date, bring a stapled-together bundle of printouts of the above-listed source code files. Your printouts must be stapled together in exactly the order listed above.
Back to: