lesson 5 mcmanus cop1006 1. algorithm instructions sequential logic structure solution...

25
Lesson 5 McManus COP1006 1

Upload: cleopatra-moody

Post on 21-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Lesson 5

McManusCOP1006 1

Page 2: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Algorithm Instructions Sequential Logic Structure Solution Development

McManusCOP1006 2

Page 3: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Terminal◦ Starts, Stops, Ends

Input/Output◦ Input data, Output information

Assign◦ Apply values to variables

Process◦ Execute instructions

McManusCOP1006 3

Terminal

Input/Output

Assign

Process

Page 4: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

The oldest logic structure The most used logic structure The easiest structure to understand Is one statement after another statement,

after another statement, after another statement, etc.

McManusCOP1006 4

Page 5: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Enter Name Enter Address Enter City Enter State Enter Zip Enter Hours Enter Wage Pay = Hours * Wage

Print Name Print Address Print City Print State Print Zip Print Pay

McManusCOP1006 5

Page 6: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 6

Page 7: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Elliott invests $5,000 in a savings account that yields 5% (.05) annual interest.

Assuming that all interest is left on deposit along with the original deposited amount◦ What will be the equation to calculate the

amount that will be in the account at the end of 10 years?

◦ How much will the interest be at the end of the 10 years?

McManusCOP1006 7

Page 8: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

First: Research banking algorithms◦ We wind up with Two Equations:

First…the Tough one Amount = Principal * ((1 + Interest Rate) ^ Time)

Principal – amount to be invested Interest Rate – yearly percentage rate Time – investment period in years Amount – principal + interest earned

Next…the Easy one Interest = Amount – Principal

Interest – the actual amount of Interest

McManusCOP1006 8

Page 9: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 9

Calculate Savings

Get input Calculate

PrintAmount

PrintInterest

CalculateInterest

CalculateAmount

Print

Page 10: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Get Principal Amount Get Yearly Interest Rate Get Time Intervals Calculate Amount of Principal + Interest

Amount = Principal * ((1 + Rate) ^ Time) Calculate Amount of Interest

Interest = Amount - Principal Print Amount of Ending Principal + Interest Print Amount of Interest

McManusCOP1006 10

Page 11: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

In Visual Basic

Note: Up to this point it didn’t matter what language we use…

McManusCOP1006 11

Page 12: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Variable Name Data Type Module Defined Domain Scope

Principal Decimal GetInput

Calculate

0 < n < 1 million Global

InterestRate Double GetInput

Calculate

0 < n < 1 to 14+ digits

Global

Time Short GetInput

Calculate

0 < n < 32,767 Global

Amount Single Calculate

PrintResult

0 < n < 1 million Global

Interest Single Calculate

PrintResult

0 < n < 1 million Global

McManus COP1006 12

Page 13: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

' Input Values by UserDim Principal As DecimalDim InterestRate As DoubleDim Time As Short

' Output ValuesDim Amount As SingleDim Interest As Single

McManusCOP1006 13

Internal Documentation

taking the form of Comments

Page 14: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Private Sub cmdGetInput_Click()Principal = CDec(InputBox("In Dollar amount " & _

“ (5000)", "Please Enter Principal Amount"))

InterestRate = CDbl(InputBox(" Ex. 5% " & _ “ would be entered as .05", "Please " & _ “ Enter Annual Interest Rate"))

Time = CShort(InputBox("An Integer " & _ “ with no decimals", "Please Enter Length "

& _ “ of Time in Years"))

End Sub

McManusCOP1006 14

Page 15: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Private Sub cmdCalculate_Click()

' Calculates Principal + Interest over timeAmount = Principal * ((1 + InterestRate) ^ Time)

' Calculates amount of total Interest at end of' time period

Interest = Amount - Principal

End Sub

McManusCOP1006 15

Page 16: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Private Sub cmdPrintResult_Click() lstOutput.Items.Add(" The Amount of Principal + " & _

“Interest Paid on") lstOutput.Items.Add(" the Principal Amount of " & _ VB6.Format(Principal, "Currency") & " for ") lstOutput.Items.Add(Time & " years is") lstOutput.Items.Add(" " & _

VB6.Format(Amount, "Currency")) lstOutput.Items.Add(" and the Amount of Interest is") lstOutput.Items.Add(" " & _

VB6.Format(Interest, "Currency"))

End Sub

McManusCOP1006 16

Page 17: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 17

Page 18: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 18

1st step

2nd step

3rd step

Page 19: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 19

Page 20: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 20

Page 21: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Internal Documentation◦ Takes the form of comments within the program◦ Helped by using Mnemonic terms

Creates self-documenting code External Documentation

◦ Takes the form of User Manuals Help Screens System Documentation

McManusCOP1006 21

Page 22: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Input Variables to be tested◦ Principal

any numeric value greater than 0

◦ InterestRate any numeric value greater than 0 and less than 1

◦ Time any integer greater than 0

Note: We didn’t do any Error checking in this problem.

McManusCOP1006 22

Page 23: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Output Variables◦ Would expect to see the Amount a positive

number greater than the Principal amount◦ Would expect to see the Interest greater than

zero.

Why are these statements important?

McManusCOP1006 23

Page 24: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

Analyze the problem◦ Being able to restate the problem is one

indicator that you understand the problem Develop the structure chart Develop the algorithms Develop the data dictionary Develop the code Test the solution

McManusCOP1006 24

Page 25: Lesson 5 McManus COP1006 1.  Algorithm Instructions  Sequential Logic Structure  Solution Development McManusCOP10062

McManusCOP1006 25