week6_slides_W04

advertisement
ENGR 112
Introduction to Programming
Outline





Software and Programming
Program Structure
Tools for Designing Software
Programming Languages
Introduction to Visual Basic (VBA)
Software


Written in a programming language
Every language has a syntax



Language rules dictate writing program code
Syntax must be perfect for the program to work
What kinds of language rules?



Spelling
Punctuation
Placement of words in an instruction
Properly Working Program



Specific sequence of instructions
Don’t leave anything out
Don’t add in things that are not needed
Importance of Structure
Making a cake
 Stir
 Add two eggs
 Add a gallon of
gasoline
 Bake at 350 degrees
for 45 minutes
 Add three cups of
flour
BUT Instructions are
 Out of order
 Missing
 Belong to another
procedure
Running a Program

The program can ONLY be run or
executed


once syntax errors are removed
if the program appears to be written in
a logical order
Program Elements




Input- data, facts
Process – organize, check
for accuracy, or do some
calculations
Output – send results to
printer, monitor, or speaker
Storage – send results to
file to be used for input
later
Program Structure



Large programs are never written as one huge
series of steps.
Programs are broken down into units called
modules.
Modules are also referred to as:
 subroutines
 functions
 procedures
 methods
Why Modularize?




Allows multiple people to work on a
problem
Enables reuse
Makes it easier to identify structures
Makes it easier to debug
ENGR 112
Tools for Program Design
Flowcharts




Used to structure logical flow of
program
Create BEFORE you start creating code
Can help prevent errors in logic
Can speed up coding process since flow
of program has been developed
Flowchart Symbols
Processing
Start/End
Decision
Flow of logic
I/O
Connector
ENGR 112
Flowchart Examples
Flow Chart for
Determining
the best way
home
Flow Chart for
Running a Bath
Payroll Program
Flow Chart for
Determining Payroll
Amount without
Modules
Payroll Program with Modules
Flow Chart for
Determining
Payroll
Amount with
Modules
Creating Flowcharts #1

Read records from a file containing the
name and batting average of all players
in the league. Write out the name and
average of every player with an average
of .200 or greater.
Creating Flowcharts #2

Read a file containing the high
temperature of each day in
December. Write out the average high
temperature for the month. You can
assume the input file includes only the
31 temperatures for December.
Creating Flowcharts #3

Design a program to create a graduation list
given an input file of graduates including
name and GPA for each graduate. Any
graduate with a GPA that is greater or equal
to 3.8 should have "Dean's List" written next
to his/her name. All others should have “
Graduate” written next to his/her name.
Finally print “End of Graduation List -2002”.
Hierarchy Charts


You can use a hierarchy chart to
illustrate modules’ relationships
A hierarchy chart does not tell you
what tasks are to be performed
within a module; it does not tell you
when or how a module executes
Hierarchy Charts



Used to break down “tasks” associated
with a program into sub-tasks
Illustrates relationship between
tasks/subtasks
Useful in guiding structure of events in
Visual Basic
ENGR 112
Hierarchy Chart Examples
Hierarchy Chart Example
Value-Averaging Program
Hierarchy Chart Exercises




Allow a user to enter 5 numbers in any order,
return and ordered list of numbers.
Allow a user to enter a word of any length,
and return a count of the number of vowels.
Allow a user to enter any number, tell the
user if a number is a prime number and if the
number is odd or even.
Allow a user to enter a shape type and
dimensions, tell the user which of two shapes
has the largest area.
Pseudocode Definition

An outline of a program, written in a
form that can easily be converted into
real programming statements.
Definition from http://www.webopaedia.com/
Writing Pseudocode

Before you start typing your program,
sit down and think about what you
want to do. You might just run through
in your head the steps you think you
need, or you might jot some things
down. THIS is what your pseudocode is.
Writing Pseudocode

When you decide how you are going to
tackle the program, your ideas go
through several levels of refinement.
Similarly, the pseudocode can be more
or less detailed depending on exactly
what you want to put down.
ENGR 112
Pseudocode Examples
General Pseudocode Example
Build a matrix
– Put 1’s on main
diagonal,
– Put 0.5’s on off
diagonals
– Put numbers in corners
Detail Pseudocode Example
Build a matrix
Do “loop from 1 to size”
A(i,i) = 1
A(i+1,i) = -0.5
A(i,i+1) = -0.5
end Do
A(1,1) = ...–
Guess the Number Program
Get random number
Do
! loop until number is guessed
Prompt for user's guess
Get user guess
Compare against actual number:
If guess is higher,
print "too high!"
ElseIf guess is lower,
print "too low!"
Else
print "you guessed it!" and exit the program
Endif
End Loop
! go back for another guess
Bubble Sort Pseudocode
While not at end of list
compare adjacent elements
if second is greater than first
switch them
Else
get next two elements
if elements were switched
repeat for entire list
Important Points




Pseudocode does use loops or If/Then/Else
statements, where needed.
You should not use exact syntax in your
pseudocode, because someone should be able to
look at your pseudocode and write a program in
ANY language.
Pseudocode should be specific enough to give an
idea of what type of program-flow you are
thinking of.
Pseudocode shouldn't need comments but
sometimes it helps to tell what the loops are for.
Generate Pseudocode
Given a classroom full of people:
• find the average height
• the number of people
• the height of the tallest person
• the height of the smallest person
What Processes do we have?
• getting a height
• determining how many people are in the
class
• calculating an average
• determining the tallest height
• determining the smallest height
Decisions to be Made?
• Is this the last person?
• Is the current height we have measured greater
than the current tallest height?
• Is the current height we have measured smaller
than the current smallest height?
What Variables are Needed?
• Current_Height (the height we have just
measured)
• Tallest
• Smallest
• Number_Of_Persons
• Average_Height
Number_Of_Persons = 0
Average_Height = 0
Sum_Of_Heights = 0
Tallest = 0
Smallest = 10
DOWHILE NOT Last_Person
INPUT Current_Height
Number_Of_Persons = Number_Of_Persons + 1
Sum_Of_Heights = Sum_Of_Heights + Current_Height
IF Current_Height > Tallest
THEN Tallest = Current_Height
IF Current_Height < Smallest
THEN Smallest = Current_Height
END DOWHILE
Average_Height = Sum_Of_Heights / Number_Of_Persons
Pseudocode Examples
Example #1.
Read records from a file containing the name and batting average of
all players in the league. Write out the name and average of every
player with an average of .200 or greater.
Example#2
Read a file containing the high temperature of each day in
December. Write out the average high temperature for the
month. You can assume the input file includes only the 31
temperatures for December.
Example #3
Design a program to create a graduation list given an input file of
graduates including name and GPA for each graduate. Any graduate
with a GPA that is greater or equal to 3.8 should have "Dean's List"
written next to his/her name. All others should have “
Graduate” written next to his/her name. Finally print “End of
Graduation List -2002”.
Pseudocode Exercises



Determine the average age of patrons
during an evening at Clodfelters
Determine the greatest amount of
money spent by a single customer
during a day at Dari Mart
Determine the average amount of
money spent on text books at the OSU
bookstore on the first day of the term
Download