AN EXAMPLE OF A PROBLEM INVOLVING
THE SELECTION CONTROL STRUCTURE

The Situation

You want a program that can identify positive and negative numbers and then display the words "positive" or "negative" accordingly. Your program must also deal with zero separately, displaying the word "zero" if it is entered. After displaying the appropriate word, the program should skip a line and display the words "To try another number, run this program again.".

Analysis:

The analysis and documentation for this problem appears below followed by the C++ source code.

Constraints:

SOLUTION

The following documentation is intended to serve as an example of what would be expected from an analyst when designing a program related to the situation described above. All emphasized text below serves only to clarify the documentation for students, but would not actually appear in the real documentation.

PROBLEM STATEMENT:

Request and store a number and then display a word indicating its sign. If the number is positive, display the word "positive". If it is negative, display the word "negative". If it is zero, display the word "zero". After displaying the appropriate word, the program should skip a line and display the words "To try another number, run this program again.".

SAMPLE SOFTCOPY

Programs that involve selection of alternative actions will always have more than one (and often many) possible results. The output produced by a program can differ drastically depending on the decisions it makes. Because of this, samples of the results expected from each decision must be demonstrated separately by providing multiple sample outputs; one for every possible path through the flowchart. These sets of possible program reactions are sometimes referred to as "scenarios". Program designers are required to determine multiple sets of sample input data that will result in each path being demonstrated, and then document each of these in separate samples for each scenario as shown below.

Note: The first two lines are the "Introduction". Bracketed items indicate user input.

Scenario 1 - Entry of a value of zero:

1
2
3
4
5
6
Sign Notification Program

Enter a number to analyze: [0]
That number is zero.

To try another number, run this program again.

Scenario 2 - Entry of a positive number:

1
2
3
4
5
6
Sign Notification Program

Enter a number to analyze: [5]
That is a positive number.

To try another number, run this program again.

Scenario 3 - Entry of a negative number:

1
2
3
4
5
6
Sign Notification Program

Enter a number to analyze: [-2]
That is a negative number.

To try another number, run this program again.

SYMBOLIC CONSTANT LIST: (NONE)

VARIABLE LIST:

IDENTIFIER DESCRIPTION DATA TYPE SOURCE USAGE DEST.
N Number to be analyzed Integer Keyboard Tested ---

FLOWCHART (Graphic Algorithm) for the program "SIGN":

A flowchart is a graphic algorithm that helps to illustrate the flow of control of a program as the processor would advance from one step to another, often in simple sequential order, but sometimes following more complex structures. In this case the processor must use its ability to compare and recognize relationships between data such as greater, less than, or equal to, and make a decision about which step(s) to process next. As long as the order of execution of steps continues to proceed toward the end of the program without branching backwards to a previous step, then the structure being followed is known as selection. For more information about what each shape below represents, view the web page about Flowcharting Symbols & Guidelines.

Flowchart of a selection structure


ALGORITHM (AS AN OUTLINE)

(The outline below would be redundant if a flowchart is provided, but is added in this example to show an optional algorithm format)

a. Start
b. Display the Introduction (see Sample Softcopy).
    b1. Display "Sign Notification Program" message and return.
    b2. Display a blank line.
c. Request and store N
    c1. Prompt on screen with "Enter a number to analyze: ".
    c2. Store keyed-in response in N.
d. If N is zero:
    then: display "That number is zero." message and return
    otherwise:
        If N is greater than zero:
            then: display "That is a positive number." message and return
            otherwise: display "That is a negative number." message and return.
e. Display a blank line.
f. Display the words "To try another number, run this program again."
g. End

DESK CHECK:

Multiple Tests Required for Selection:

A program that involves selection of alternative branches will always have more than one (and often many) possible paths through its flowchart (algorithm). Because of this, each of these paths must be tested separately by performing one test for every possible path through the flowchart. Program designers have to define different sets of test input data to ensure that each scenario is tested, and must document each of these tests separately (as shown below).

Additional Columns Required to Trace Conditions:

Desk Check Tracing Charts for programs that select alternative branches based on test conditions must include columns to trace the true/false results of those conditions. One column should be added for each diamond shaped symbol in the flowchart using the condition as the column heading. The example above involves two tests, so two extra columns will be added to the Tracing Chart along with the one column necessary to trace the value of variable N.

Tracing Chart for Scenario 1 - an entry of zero:

N N = 0 N > 0
0    
  True  
Test Softcopy for Scenario 1 - an entry of zero:
1
2
3
4
5
6
Sign Notification Program

Enter a number to analyze: [0]
That number is zero.

To try another number, run this program again.

Separate pairs of documents (Tracing Chart and Test Output) would be provided to prove that the flowchart worked for each scenario.

Alternative (Blended) Method for Desk Check Documentation:

Another (but not as common) Desk Check documentation technique can be used that blends together the Tracing Chart and the Test Output for each scenario into one document. This alternative way of documenting your desk check activity is illustrated below for the other two scenarios defined in the original Sample Softcopies.

Blended Desk Check Document for Scenario 2 - An entry greater than zero:

N N = 0 N > 0  
Sign Notification Program
 
Enter a number to analyze: [5]
5      
  False    
    True  
That is a positive number.
 
To try another number, run this program again.

Blended Desk Check Document for Scenario 3 - An entry less than zero:

N N = 0 N > 0  
Sign Notification Program
 
Enter a number to analyze: [-2]
-2      
  False    
    False  
That is a negative number.
 
To try another number, run this program again.

C++ SOURCE CODE:

/* sign.cpp - an example of the selection structure */
/* Written by: Randolph Gibson - Date: Sept. 11, 2011 */

#include <iostream>
using namespace std;

int N; /* A number to be evaluated */

int main ()

{
  cout << "Sign Notification Program\n\n";
  cout << "Enter a number to analyze: ";
  cin >> N;

  if (N == 0) cout << "That number is zero.\n";
  else if (N > 0) cout << "That is a positive number.\n";
       else cout << "That is a negative number.\n";

  cout << "\nTo try another number, run this program again.\n";
  return 0;   /* Return zero error code to parent process */
}
PATH: Instructional Server> COP 2000> Examples>