0% found this document useful (0 votes)
31 views

Design&Pseudocode V2

The document introduces algorithms and pseudocode. It defines an algorithm as a detailed, step-by-step sequence of actions to solve a problem. Algorithms have inputs, outputs, definiteness, correctness, finiteness and generality. Program design develops the logical details of an algorithm using structured programming, top-down design, stepwise refinement and pseudocode. Pseudocode is an informal language that describes program algorithms without syntax. It focuses on logical problem details. Several example problems are provided with their required variables and pseudocode solutions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Design&Pseudocode V2

The document introduces algorithms and pseudocode. It defines an algorithm as a detailed, step-by-step sequence of actions to solve a problem. Algorithms have inputs, outputs, definiteness, correctness, finiteness and generality. Program design develops the logical details of an algorithm using structured programming, top-down design, stepwise refinement and pseudocode. Pseudocode is an informal language that describes program algorithms without syntax. It focuses on logical problem details. Several example problems are provided with their required variables and pseudocode solutions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction to Program Design and Pseudocode

Computing problems are solved by executing a series of actions in


some specific order. To design a computing problem solution, the
actions that are needed must be specified in addition to the order of
the actions. Such a specification is called an algorithm.

Algorithm - a detailed, step-by-step sequence of actions that


describes how to solve a particular problem.

General Properties Shared by Algorithms:


 Input. An algorithm will have input values typically obtained from
(a) an external source (e.g., the user of a program
implementation of the algorithm), or (b) from another component
of the larger problem within which the algorithm is operating, or
(c) generated within the algorithm.
 Output. For each of the input values, an algorithm generates
output values, which are typically either given to an external
source (e.g., displayed to the user of a program) or sent to
another component of the larger problem within which the
algorithm is operating. The output values represent the solution
to the algorithm’s problem.
 Definiteness. The steps of an algorithm must be defined
precisely and given in the proper order.
 Correctness. An algorithm should produce the correct output
values for the given input values.
 Finiteness. An algorithm should generate the desired output
values using a finite number of steps for the given input values.
 Generality. An algorithm should generate the correct output
values for all possible input values, not just for a particular set of
input values.
Program Design – developing the logical details of an algorithm; try
to use structured programming methodology.
Structured programming - methodology that results in
readable, reliable, and maintainable programs.
 uses top-down program design and stepwise refinement
 uses a limited number of control structures
 uses modularity
Top-Down Design and Stepwise Refinement
 break up a problem into a series of precisely stated sub-
problems
 repeat until a collection of easily solved sub-problems exist
 proceed from general to particular - look at big picture first,
details later
 specify the details of your algorithms using pseudocode
or flowcharts
Pseudocode
 An informal, high-level, detailed, English-like method of
describing and outlining program algorithms. It is a design
language that closely resembles a high-level programming
language, but does not require the use of all the precise
syntax of the programming language.
 Primary advantage: Pseudocode allows you to focus on the
logical details of your program solution without getting bogged
down with the syntax rules of the target programming
language.
 Primary disadvantage: Pseudocode has no single form - its
form varies depending on the author. For team programming
development projects, this means that the pseudocode reader
must become used to other styles.
Modular Programming
 Program Module - an independent block of computer code
that has a single entry and a single exit point and that
implements a single, logically self-contained process to
perform a specific task. Note: Initially, the programs that you
will design and write will consist of only one module – we will
learn about designing and writing other program modules later
in the class.
Writing Pseudocode
For each module in your programs, detailed pseudocode should
be written describing exactly what the module will do when
invoked. This includes specifying all of the steps of the algorithm,
in the correct order, and including references to all variables that
the algorithm will require to complete its task. (Note: Variables
are symbolic names used in programs to represent memory
locations in which your program’s data will be stored.) The
important aspect of well-written pseudocode is to express
sufficient detail of a problem solution. The detail of your
pseudocode should be sufficient to allow another
programmer to write a high-level language program directly
from your pseudocode.
Design details that should be included with your pseudocode:

1. Identification of all of the steps required to be performed to fulfill a


problem solution. Each step should specify one action (e.g.,
reading in a data value, performing an arithmetic operation, or
displaying a data value). The steps should be listed, one step per
line, and given in the correct order.

2. Identification of all the variables and other data structures (e.g.,


vectors, arrays, or structures) required by each program module for
storage of the data that is being manipulated by the module. You
should specify suggested variable names, identify the data type
(e.g., integer or floating point), and give a brief description of what
each variable will be used for.

Additional pseudocode detail that should be included for designs


that include several program modules:

3. A description of all program modules used in your design. You


should specify suggested module names and give a brief
description of what task the module will perform when invoked.

4. A description of all input parameters (if any) for each program


module used in your design. You should specify suggested
parameter names, identify the data type, and give a brief
description of what the role is for each parameter. Later, you should
also specify the parameter type – value or reference.

5. Identification of the value returned (if any) from each program


module used in your program. You should specify the returned
value type and give a brief description of the returned value.
Problem-Solving and Pseudocode Development
To give you an idea of what I will be looking for in pseudocode, the
following is a collection of introductory programming problems along
with pseudocode algorithms that solve the problems. For each, both
variable specifications and the design steps are specified.

Problem 1: Enter three numbers and find and display their


average.

Variables required (names and types):


x, y, z: (floating point) to store the three numbers
sum: (floating point) to store the sum of the numbers
average: (floating point) to store the average of the
numbers

Pseudocode solution:
Display “Enter the three numbers:”
Read the three numbers into variables x, y, and z
Let the variable sum = x + y + z
Let the variable average = sum/3.0
Display “The average value = ” and average
Problem 2: Prompt for and enter the price of an item being
purchased at a retail store and calculate and display the final
price including sales tax. Assume the sales tax is 6.5%.

Variables required (names and types):


price, tax, total: (floating point) to store the price, sales tax,
and total price

Pseudocode solution:
Display “Enter the price:”
Read the price into the variable price
Let the variable tax = 0.065 * price
Let the variable total = price + tax
Display “The final price with sales tax = $” and total
Problem 3: A cubic foot of water weighs 62.4 pounds and
contains 7.48 gallons. Prompt for and enter a number of cubic
feet of water and then calculate and display the water’s weight in
pounds and its amount in gallons.

Variables required (names and types):


volume, weight, gallons: (floating point) to store the volume,
weight, & gallons of water

Pseudocode solution:
Display “Enter the number of cubic feet of water:”
Read the number of cubic feet of water into the variable
volume
Let the variable weight = volume * 62.4
Let the variable gallons = volume * 7.48
Display “Water weight in pounds =” and weight
Display “Water amount in gallons =” and gallons
Problem 4: Prompt for and read a test score (integer values in
the range 0-100) and then determine and display if the score is
passing (60 or more). Note: This solution requires an if selection
structure – we will learn more about this structure later in the class.

Variables required (names and types):


score: (integer) to store the score

Pseudocode solution:
Display “Enter the score:”
Read the score into the variable score
If score > 60
display “The score is passing”
Problem 5: Prompt for and read two integers and then determine
and display the smaller of the two integers (assume the numbers
are different). Note: This solution requires an if-else selection
structure – we will learn more about this structure later in the class.

Variables required (names and types):


x, y, smaller: (integer) to store the two integers and the
smaller value

Pseudocode solution:
Display “Enter two integers:”
Read the numbers into the variables x and y.
If x < y
let the variable smaller = x
else
let the variable smaller = y
Display “The smaller number =” and smaller
Problem 6: Prompt for and enter 30 numbers and find and
display their average. Note: This problem is similar to Problem 1.
But, it is not practical to use 30 different variables here for the
numbers, so the solution given here uses a looping structure – we will
learn more about this structure later in the class.

Variables required (names and types):


x, average: (floating point) to store the current number and
the average value
sum: (floating point) an accumulator to accumulate the sum
of the 30 numbers
counter: (integer) a counter to count the number of numbers
entered

Pseudocode solution:
Initialize the variable sum = 0.
Initialize the variable counter = 1.
While counter < 31
Display “Enter a number:”
Read the number into the variable x
Let sum = sum + x
Let counter = counter + 1
End of while loop
Let the variable average = sum/30.0
Display “The average value =” and average
Problem 7: Prompt for and enter 40 test scores (integer values in
the range 0-100), and count and display how many were passing
scores (60 or more) and how many failing scores (59 or less).
Note: This problem will require using an if-else selection structure
inside of a looping structure. We will cover these topics in more detail
later in the class.

Variables required (names and types):


pass, fail: (integer) counters to count the number of passing
and failing scores
counter: (integer) a counter to count the number of scores
entered
score: (integer) to store the current score

Pseudocode solution:
Initialize the pass variable for counting the number of
passes to 0
Initialize the fail variable for counting the number failures to
0
Initialize the variable counter = 1
While counter < 41
Display “Enter a test score:”
Read the test score into the variable score.
If score > 59
pass = pass + 1
else
fail = fail + 1
Let counter = counter + 1
End of while loop
Display “Number of passing grades is” and pass
Display “Number of failing grades is” and fail

You might also like