ES26 Lab Introduction to Programming
1
Objectives After completing this lesson, you should be able to do the following:
Explain the process of program development Identify the properties of good algorithms Able to represent algorithms using flowcharts
Introduction to Programming
Programs are developed to instruct computers to do specific tasks, or solve specific problems An algorithm is a finite set of instructions that if followed accomplishes a particular task Thus, programming is the activity of communicating algorithms to computers
Program Development Process Problem or Task check it
make list
Requirements List design algorithm
verify
Algorithm verify
code program
Program test and debug
Properties of Algorithm 1.
Finiteness
2.
the execution of an algorithm must be completed after a finite number of operations have been performed
Absence of ambiguity
every step of the algorithm should have a unique interpretation every time an algorithm is presented for execution with the same input data, the exact same results should be obtained
Properties of Algorithm (cont..) 3.
Sequence definition – the order in which the steps are carried out should be clearly specified
4.
Input and output definition – the algorithm can have zero or more inputs and one or more outputs
Properties of Algorithm (cont..) 5.
Effectiveness
6.
the algorithm can only give instructions that the computer can carry out cases wherein there is insufficient information to perform a task should not exist
Scope definition
the problem or class of problems where the algorithm can be applied should be defined
Representation of Algorithms
Flowchart
Pictorial representation of the logical steps it takes to solve a problem
Pseudocode
An English-like representation of the same thing
Flowcharts
A two-dimensional graphical representation of an algorithm Flowcharts don't deal with details making it convenient for algorithm design and presentation Allow the reader to follow the logic of the algorithm more easily than would a linear description
Flowcharting Basics
Almost every program involves the steps of input, processing and output as in the program Get number Answer = number * 2 Print answer Therefore, most flowcharts need some way to visually separate these three steps
Input Operations
Input operations are represented by the parallelogram A statement is written in English, inside the parallelogram Get number
Processing Statements
Arithmetic statements are examples of processing statements Processing statements are represented by rectangles, and statements are written inside of them Answer = Number * 2
Output Statements
Output statements use the same symbol as input statements – the parallelogram Print Answer
Flowlines
To show the correct sequence of statements, arrows (flowlines) are used to connect steps Whenever possible, most of a flowchart should read from top to bottom or from left to right on a page
Terminal
To be complete, a flowchart should include two more elements: a terminal or start/stop symbol at each end Usually, a word like START or BEGIN is placed in the first terminal symbol A word like END or STOP is placed in the other The standard terminal symbol is shaped like a race track START
STOP
Flowchart For the Program That Doubles A Number START
Get number Answer = Number * 2
Print Answer STOP
Program That Doubles A Number For Several Inputs START
Get number Answer = Number * 2
Print Answer
Problem: The program never ends!!!
Stopping A Program
The best way to end the program is to have predetermined value for NUMBER that means “Stop the program!” The programmer and the user could agree that the user will never need to know the double of 0, so the user could enter a 0 when he or she wanted to stop The computer could then test any incoming value for NUMBER and, if it is a 0, stop the program
Decisions
Decisions are represented in flowcharts by diamonds The diamond usually contains a question, the answer to which is either a yes or a no All good computer questions has two mutually exclusive answers like yes or no ?
Program That Doubles A Number START Get number Number=0? No Answer = Number * 2
Print Answer
Yes
STOP
The Connector
A connector will be used when the physical constraints of page size stop the programmer from completing the flowchart START
1
Step 1
Step 1
Step 2
Step 1
1
STOP
Variables
Programmers commonly refer to the locations in memory called NUMBER and ANSWER as variables Variables are memory locations, the contents of which can vary Every computer programming language has its own set of rules for naming variables
The Structures
Sequence Selection Iteration
Sequence Step 1 Step 2 Step 3
Selection or Decision
Iteration or Repetition
Some Problems for Flowcharting
Design a flowchart that computes the average of three numbers Design a flowchart that determines whether a number is positive, negative or zero
Some Problems for Flowcharting
Input an arbitrary number of positive integers and compute the sum. If the integer entered is negative, then terminate the algorithm. Design a flowchart that outputs the highest number among three numbers entered by the user