Latest Revision: February 12, 2014
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
performs arithmetic on integers, using the C++ int data
type.
A Pythagorean triple is a set of three positive integers that
represent the sides of a right triangle. Such triples --
a, b, c -- satisfy the
relation:
a*a + b*b = c*c
-- which means: "The square of a plus the square of
b equals the square of c." The common
notation for expressing this idea in math books is:
(a2 + b2 = c2)
You can make Pythagorean triples by using the following recipe:
First pick any two positive integer seeds M and N, with M > N, then
determine the lengths of the sides of a right triangle like this:
Side One of the Triangle Equals: M*M - N*N
Side Two of the Triangle Equals: 2*M*N
Hypotenuse of the Triangle Equals: M*M + N*N
For example if we choose seeds M=2 and N=1, then M is larger than N,
as required, and
Side One = M*M - N*N = 2*2 - 1*1 = 3,
Side Two = 2*M*N = 2*2*1 = 4, and
the Hypotenuse = M*M + N*N = 2*2 + 1*1 = 5.
So using M=2 and N=1 as seeds, the recipe gives us the Pythagorean Triple:
(3, 4, 5).
Please notice that the seeds and the sides are different
things. In our example the seeds are 1 and 2, and the sides
calculated from the seeds are 3, 4, and 5.
Below is a "shell" or "skeleton" of a C++ program. It is not finished. It
has only comments where the C++ statements need to be. The
intended purpose of the program is to read values for M and N from the user
(M is required to be larger than N), and to then calculate and display a
Pythagorean triple.
/* start of file lab02.cpp */
/*
lab02.cpp. This program reads two positive integer seeds
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 int variables M and N */
/* ***b*** Display this text:
"A Program to Calculate Pythagorean Triples" */
/* ***c*** Prompt for a positive integer seed N, and then read it */
/* ***c*** Prompt for a positive integer seed M > N, and then read it */
/* ***b*** Display this text: "The 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 illustrates what you should see
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
A Program to Calculate Pythagorean Triples
Enter an integer N: 2
Enter an integer M > N: 5
The Pythagorean Triple
Side One = 21
Side Two = 20
Hypotenuse = 29
john@vega:
Individual steps:
- Make the indicated additions to the skeleton program. Don't delete
anything. Just add what the directions say to add.
- 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.
- Immediately below the comment in file lab02.cpp that begins with
***a***, insert a declaration of integer variables M and N. (See
section 2.1 of your C++ textbook for examples of this kind of
declaration.) Save, compile and execute the program. Correct any
errors.
- Just under each comment that begins with ***b***, insert the indicated
output statement - the "cout statement" that is needed. Under one of
the ***b*** comments you must make a cout statement that displays the
string "A Program to Calculate Pythagorean Triples" and under the
other ***b*** comment you'll make a cout statement that displays "The
Pythagorean Triple". You've made cout statements in the previous two
labs. There are many examples in the text, on page 20, for instance.
Construct the cout statements so that the
program output will be exactly 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.
- Directly under each comment that begins with ***c***, insert the
indicated C++ statements, to prompt for and read the integer N, or to
prompt for and read the integer M. This will require two statements
under each comment, first a cout statement that puts the prompt on the
screen for the user to read, and then a cin statement that reads the
input from the user and copies it to the desired variable. Refer to
lines 7-8 and 9-10 of Display 1.8 on page 20 of your text for examples
of such pairs of statements.
Construct your C++ statements
so that the program will behave exactly as in
the example interactive session shown above. (Notice
that each prompt is indented in that example, and each has a long run
of blanks in a specific area) 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 (cout) statement. Construct the code, so that the
program will display the correct Pythagorean triple - the triple
corresponding to the values of N and M that were input by the user.
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. Write cout statements that contain the
expressions to be calculated. (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. There's another
example at the bottom of page 50 of your text.)
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 you might 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. Before the lab starts,
have a look at the program called triple_check.cpp that is included in
the assignment directory. It will probably help you do the lab
assignment better if you read and understand it. Also you may use
triple_check.cpp to check the correctness of the output of the program
you write for this lab. )
- Make a script showing a run
of the program in which
you enter 2 and 7 for N and M.
Filter the script. Refer to the directions of the
"Hello World! assignment"
if you need help with the directions for making and filtering 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
(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
just to select, copy, and paste it into the appropriate location when
you are composing the command to send 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 additional help, please ask.