CS 1500-5, Spring 2010
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 online 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 -- 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."

For more information about Pythagorean Tripples:
MathWorld
Wikipedia

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
Lab02.cpp contains the shell of an interactive program that reads values for M and N, with M > N, and calculates a Pythagorean triple. Below is an example interactive session. The characters entered by the user of the program are shown underlined and in boldface.


mmartin@vega: g++ lab02.cpp
mmartin@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

mmartin@vega:

File Lab02.cpp is as follows:

//
// 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;
}

Individual steps:

  1. Save a copy of the lab02.cpp shown above as a file. 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.

  2. Below the ***a*** comment 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.

  3. 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 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.

  4. 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.

  5. 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 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!)

  6. 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?

  7. 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?
  8. Veryify that the indentation and spacing of your output is EXACTLY as shown in the example (of course, the underlining will not be present).

  9. Make a script (call it lab02.script) testing your program on the following input:  enter 3 and 7 for N and M; enter 125 and 207 for N and M.

  10. Upload your source code (lab02.cpp), your notes (lab02.notes),  and your script (lab02.script) to the CSHomework system.

  11. Turn in a hard copy of your source code (lab02.cpp).


For more advanced students:
Look at the sections in Chapter 2 on Boolean Expressions and if-else Statements. Consider ways to prevent the program from running if the user enters bad input (e.g. M<N).


Based on an assignment created by Dr. Sarraille.