Latest Revision: September, 2009
Directions for Lab #2
Read and study these directions before the day of the lab. Read this document
all the way through at least once before trying to perform any of the steps.
After that, rehearse some of the steps, mentally and/or by getting online and
doing them.
The goal of this exercise is to develop an interactive program
that uses integer arithmetic operations.
A Pythagorean triple is a set of three positive integers that represent the
sides of a right triangle. Such triples -- X, Y, Z -- satisfy the relation:
X*X + Y*Y = Z*Z
-- which means: "The square of X plus the square of Y equals the square of Z."
You can generate ("make") Pythagorean triples by using the following algorithm
(recipe):
First pick any two positive integers M and N with M > N, then determine the
lengths of the sides of a right triangle like this:
Side One = M*M - N*N
Side Two = 2*M*N
Hypotenuse = M*M + N*N
Below is a "shell" or "skeleton" of a C++ program. It is not finished. It has
only comments where most of the executable statements need to be. Its purpose
is to read values for M and N from the user of the program (M > N), and to
calculate a Pythagorean triple.
/* start of file lab02.cpp */
/*
lab02.cpp. This program reads two positive integers
M and N with M > N and displays a Pythagorean triple,
which consists of three positive integers that represent
the sides of a right triangle.
*/
#include <iostream>
using namespace std ;
int main(void)
{
/* ***a*** Declare M and N and comment on their meanings */
/* ***b*** Display: Program to Calculate Pythagorean Triples */
/* ***c*** Prompt and read a positive integer N */
/* ***c*** Prompt and read a positive integer M > N */
/* ***b*** Display: Pythagorean Triple */
/* ***d*** Calculate and display Side one */
/* ***d*** Calculate and display Side two */
/* ***d*** Calculate and display Hypotenuse */
return 0;
}
/* end of file lab02.cpp */
Below is an example of an interactive session. It is what should be seen on
the screen when you run the program. To make it easy for you to figure out
which characters the user of the program entered, and which characters the
program printed on the screen, the characters entered by the user are shown
underlined and in boldface.
john@vega: a.out
Program to Calculate Pythagorean Triples
Enter an integer N: 2
Enter an integer M > N: 5
Pythagorean Triple
Side One = 21
Side Two = 20
Hypotenuse = 29
john@vega:
Individual steps:
- Save a copy of the program shell lab02.cpp (see above). Give the
file the same name: lab02.cpp. To make the file you can just
select the program text, copy it, paste it into a window running an
editor, and save it.
- Below the comment that begins with ***a*** insert a declaration of
integer variables M and N and comment on their meanings. (See your C++
textbook for examples of this kind of declaration.) Save, compile and
execute the program. Correct any errors.
- Below each comment that begins with ***b*** insert the indicated output
statement -- one to display "Program to Calculate Pythagorean Triples"
and one to display "Pythagorean Triple". Construct the statements so
that the program output will be just as in the example interactive
session shown above. Make sure that your code creates blank lines just
as in the example. Re-run the program (i.e. save, compile and execute
it again). Correct any errors. Notice the output you get.
- Under the comments that begin with ***c*** insert the indicated code to
request and read the integer N or M. Construct the code so that the
program will behave just as in the example interactive session. (Notice
that each prompt is indented in that example.) Re-run the program.
Correct any errors. Notice the output you get.
- Under each comment that begins with ***d*** put the code for the
appropriate output statement. Construct the code, so that the program
will display the Pythagorean triple corresponding to the values of N and
M that were input. The numbers output must be M*M-N*N, 2*M*N,
and M*M+N*N.
Get the indentation (spacing) right, as in the
example.
Don't use any new variables for this part of the code.
Perform the computations in the output statements. (As an
example of the general idea: cout << 3*A+6*4 <<
endl; is an output statement that computes 3A+24 and
outputs it without using any new variables.)
Re-run the program. Correct any errors. Notice the output you get.
Verify that the numbers in the output do form a Pythagorean
triple. For example, if the output contains the numbers 21, 20, and 29,
then verify using a calculator (or pencil and paper) that 21*21 + 20*20 =
29*29. (This is a critical step. The whole point of learning to program
is to make the computer give us what we want. The output has to be
correct.)
- Run the program and enter numbers that violate the directions in the
following way: Enter one negative integer and one positive one. Does
the program produce a Pythagorean triple? Refer to the program code.
Can you point out what made the program succeed or fail?
- Run the program and enter numbers that violate the directions in a
different way: Enter two positive numbers but have M < N. Does the
program produce a Pythagorean triple? Refer to the program code. Can
you point out what made the program succeed or fail?
- Make a script (you could call it lab02.script) where you run the program
and enter 3 and 7 for N and M. Refer to the directions of the
"Hello World! assignment"
if you need help with the directions for making a script.
- E-mail me a copy of the program source code (the file lab02.cpp) with
subject line:
CS1500Lab02Source
E-mail me a copy of the script with subject line:
CS1500Lab02Script
(A word about the subject lines - it is important that you use the exact
subject lines I specify. It assures I will be able to find your
message in my inbox. There may be hundreds of messages there. You will
lose a significant amount of credit for this assignment if you don't use
the correct subject line. The easiest way to
make sure you are using the correct subject line is to just select, copy,
and paste it into the appropriate location when you are composing the
e-mail.)
You may refer to the directions of the
"Hello World!" assignment if you need help with sending the
e-mail. If you need help, please ask.