121: = Computer Science 1 --- February 10

Topics:
Functions
Value and Reference Parameters
Top-down Design 

Recall the Problem.
Write a program for determining the type of triangle given its three = side lengths.

1. Defining the problem

Input: Float numbers A, B and C representing the three sides.

Output: One or more messages = indicating the type of triangle input.

Processing: Employ functions that use conditional expressions and selection to determine the corresponding triangle type.
 
 

2. Planning the solution using functions and top-down = design

Initial Algorithm 

1.     Get Data using reference parameters

            Get_Data(SideA, SideB, SideC) 

2.     Test the data to determine triangle type

a.      Test whether triangle is equilateral using value parameters.

EquilatTest(SideA, SideB,  SideC)

b.     Test whether triangle is right triangle using constant reference  parameters

RightTriTest(SideA, SideB, SideC)

3.     Output results.

 
 

//*************************************************************************

//Programmer: Fred Annexstein
//Last update: 02/10/2001
//Program purpose: This program inputs the threes side lengths of a
//triangle and determines the type of triangle from the input.
//*************************************************************************
 

//PREPROCESSOR DIRECTIVES

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void Get_Data(float&, float&, float&);

int EquilatTest(float, float, float);

int RightTriTest(const float&, const float&, const float&);

int main()

{
  //VARIABLE DECLARATIONS
  float SideA,SideB,SideC;       // Three side lengths
 

  //DESCRIPTION OF PROGRAM

 
  cout << "\nThis program determines the type of triangle from the side lengths.\n";
 

  Get_Data(SideA, SideB, SideC);

 
  if (EquilatTest(SideA, SideB, SideC) == 1)
    cout<< "The triangle is an equilateral triangle.\n";
  if (RightTriTest(SideA, SideB, SideC) == 1)
    cout<< "The triangle is a right triangle.\n";
  return 0;

} //END main()

 

//Example of using Reference Parameters

void Get_Data(float& A, float& B, float& C)
{
  cout << "\n Enter three sides of a triangle \n";
  cin >> A >> B >> C;
}

//Example of using Value Parameters

int EquilatTest(float A, float B, float C)
{
  if ((A==B) && (B==C))
    return 1;
  else return 0;
}

//Example of using Constant Reference Parameters

int RightTriTest(const float& A,  const float& B, const float& C)
{
  if ((A*A + B*B) == (C*C))
    return 1;
  else return 0;
}