Practice Programs

Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.

  1. Using your text editor, enter (that is, type in) the C++ program shown in Display 1.8. Be certain to type the first line exactly as shown in Display 1.8. In particular, be sure that the first line begins at the left-hand end of the line with no space before or after the # symbol. Compile and run the program. If the compiler gives you an error message, correct the program and recompile the program. Do this until the compiler gives no error messages. Then run your program.

  2. Modify the C++ program you entered in Practice Program 1. Change the program so that it first writes the word Hello to the screen and then goes on to do the same things that the program in Display 1.8 does. You will only have to add one line to the program to make this happen. Recompile the changed program and run it. Then change the program even more. Add one more line that will make the program write the word Good-bye to the screen at the end of the program. Be certain to add the symbols \n to the last output statement so that it reads as follows:

    cout << "Good-bye\n";

    (Some systems require that final \n, and your system may be one of them.) Recompile and run the changed program.

  3. Further modify the C++ program that you already modified in Practice Program 2. Change the multiplication sign * in your C++ program to a division sign /. Recompile the changed program. Run the program. Enter a 0 input for “number of peas in a pod.” Notice the run-time error message due to division by zero.

  4. Modify the C++ program that you entered in Practice Program 1. Change the multiplication sign * in your C++ program to an addition sign +. Recompile and run the changed program. Notice that the program compiles and runs perfectly fine, but the output is incorrect. That is because this modification is a logic error.

  5. Modify the C++ program that you entered in Practice Program 1. In this version calculate the total length of fence you would need to enclose a rectangular area that is width feet long and height feet tall. The program should have variables for width and height with values entered by the user. Create another variable, totalLength, that stores the total length of fence needed (which your program should calculate). Output the total with an appropriate message.

  6. The purpose of this exercise is to produce a catalog of typical syntax errors and error messages that will be encountered by a beginner and to continue acquainting you with the programming environment. This exercise should leave you with a knowledge of what error to look for when given any of a number of common error messages.

    Your instructor may have a program for you to use for this exercise. If not, you should use a program from one of the previous Practice Programs.

    Deliberately introduce errors to the program, compile, record the error and the error message, fix the error, compile again (to be sure you have the program corrected), then introduce another error. Keep the catalog of errors and add program errors and messages to it as you continue through this course.

    The sequence of suggested errors to introduce is:

    1. Put an extra space between the < and the iostream file name.

    2. Omit one of the < or > symbols in the include directive.

    3. Omit the int from int main().

    4. Omit or misspell the word main.

    5. Omit one of the (); then omit both the ().

    6. Continue in this fashion, deliberately misspelling identifiers (cout, cin, and so on). Omit one or both of the << in the cout statement; leave off the ending curly brace }.