cs1ch3sec3.htm
 

CHAPTER THREE

Analysis and Design : First Steps

 


Section III: Patterns Section I : Problem Analysis Section II: Problem Analysis Section IV: The While Loop With Priming Read Pattern Section V: The Counter Pattern
Section VI: The Sum Pattern Section VII: The Average Pattern Section VIII: The Salary Problem Revisited Section IX: Taking Advantage of Patterns

  


Table of Contents

Learning C++:
An Index of Entry Points


2. The

of C++

A reference document on the basic elements of C++.



3. The Patterns



Index!



Back a few paragraphs we said that there were three parts to developing a correct algorithm. The first two were

  • knowing how to solve the problem yourself;
  • relating the effects of an instruction on the state of the computer to your goal(s) for an instruction.

Now we discuss the third part. It turns out that computer-based solutions to certain problems don't always take the same form as human oriented solutions. And, even when they do, there are often ways of phrasing the solution in algorithmic form that are easier to translate into code. A simplistic example: Suppose we had written the 'if' part of the algorithm above as:


 When the Hours Worked is greater than 40
{	Regular Salary = 40 * Rate of Pay
	Overtime Rate of Pay  = Rate of Pay * 1.5
	Overtime Salary = Overtime Rate of Pay * (Hours Worked - 40)
	Full Salary = Regular Salary + Overtime Salary
}
Otherwise
{	Full Salary = Hours Worked * Rate of Pay
} 

To an English speaker, this means the same thing as the above algorithm but it may not be clear to a novice programmer that "when....otherwise" is translated as 'if...else'.

More important there are times when a computer algorithm must include details that would never be included in a set of instructions meant for a human. Consider the program from chapter 2 to calculate the sum of 10 numbers (ch2prg4.cpp) . Instructions given to a human for this problem would be something like:

Get all 10 numbers
Add them up.
Output the Result.

However, this is not at all how a computer solves this problem - as you can see from looking at the code in ch2prg4.cpp Therefore, translating these instructions into C++ code would be a difficult task. The challenge is to find an algorithm that is correct AND easy to translate. Consider the following more 'computer friendly' algorithm for this problem:


Set the Sum to 0
Set the Count to 0
While the Count is less than 10
{	Get a Number from the user
	Add that Number to Sum
	Add 1 to Count 
}
Output the Sum 
A number of elements make this algorithm different. Humans would never be told to 'initialize' some memory locations to 0 - which is what the first two steps do. A computer must be explicitly told this. Humans implicitly repeat a set of steps, (execute a loop) while computers need explicit information about how many times to loop (or when to stop looping) and what instructions are part of the loop. No wonder then that programming can be a difficult process for the novice:
  1. The novice is used to expressing instructions or algorithms in forms that are appropriate for human interpretation ;

  2. Such algorithms are hard to translate into code;

  3. To write algorithms that are easier to translate into code, one needs to have some understanding of what good code looks like but this is exactly what the novice is still learning.

You will learn how to write easily translatable algorithms as you learn how to code but in the meantime you need an aid in writing good algorithms. It turns out that there are a number of general patterns or templates that underlie many common coding problems. Expert programmers simply 'know' these and use them in developing their algorithms. These patterns represent the general form your algorithms should take when faced with certain classes of problems. The rest of this chapter discusses a number of these patterns and provides coding examples based on these. You are strongly advised to study these carefully and use them when you begin a new programming problem. They are meant to guide you in the design process and to help you avoid having to 're-invent the wheel' every time you start a new program. (For those simply wanting a summary of the patterns themselves, a separate document is provided.)

Top of Section Main Menu Next Section