Latest Revision: 09/16/01
Directions for Lab #2
Read and study all these directions before the day of the lab.
Try to rehearse mentally what you will be doing. Better still,
if you have time, get on line and actually rehearse some of the
steps.
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 -- the square of X plus
the square of Y equals the square of Z. We can generate triples
from two positive integers M and N with M > N, as follows:
Side One = M*M - N*N
Side Two = 2*M*N
Hypotenuse = M*M + N*N
Lab023.cpp contains the shell of an interactive program that
reads values for M and N, with M > N, and calculates a
Pythagorean triple. The following is an example interactive
session, with input 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:
File Lab023.cpp is as follows:
//
// lab023.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.h>
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;
}
Individual steps:
- Save a copy of the lab023.cpp shown above as a
file. Give the file the same name: lab023.cpp. To
make the file you can just select the program text, copy it,
and paste it into an editor window.
- Below the ***a*** comment insert a declaration of integer
variables M and N and comment on their meanings. (See the
following examples of this in Shiflet: example 2.2 on page
62 and example 2.3 on page 66). Save, compile and execute
the program. Correct any errors.
- Below each ***b*** comment insert an 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 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 ***c*** comments insert code to request and read
the integers N and 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 ***d*** comment 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 show 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 the correct one! )
- Run the program and enter numbers that violate the
directions by having one number be negative. 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 by having 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 lab023.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
lab023.cpp) with subject line "cs1500,lab02,source". E-mail
me a copy of the script with subject line
"cs1500,lab02,script". Refer to the directions of the
"Hello World!" assignment if you need help with
the directions for sending the e-mail.