problem solving with the sequential logic structure

28
Problem Solving with the Sequential Logic Structure Lesson 5 McManus COP1000 1

Upload: ursa

Post on 22-Jan-2016

43 views

Category:

Documents


7 download

DESCRIPTION

Problem Solving with the Sequential Logic Structure. Lesson 5. Overview. Algorithm Instructions Sequential Logic Structure Solution Development. Flowchart Symbols. Terminal Starts, Stops, Ends Input/Output Input data, Output information Assign Apply values to variables Process - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Problem Solving with the Sequential Logic Structure

COP1000 1

Problem Solving with the Sequential Logic Structure

Lesson 5

McManus

Page 2: Problem Solving with the Sequential Logic Structure

COP1000 2

Overview

• Algorithm Instructions• Sequential Logic Structure• Solution Development

McManus

Page 3: Problem Solving with the Sequential Logic Structure

COP1000 3

Flowchart Symbols

• Terminal– Starts, Stops, Ends

• Input/Output– Input data, Output information

• Assign– Apply values to variables

• Process– Execute instructions

McManus

Terminal

Input/Output

Assign

Process

Page 4: Problem Solving with the Sequential Logic Structure

COP1000 4

Sequential Logic Structure

• 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.

McManus

Page 5: Problem Solving with the Sequential Logic Structure

COP1000 5

An Example

• 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

McManus

Page 6: Problem Solving with the Sequential Logic Structure

COP1000 6

Another Example

• Input– Get Height– Get Width

• Processing– Area = Height * Width

• Output– Print Area

McManus

Sequential Statements in Pseudocode

What each

section is doing…

Page 7: Problem Solving with the Sequential Logic Structure

COP1000 7

Let’s work a problem…

McManus

Page 8: Problem Solving with the Sequential Logic Structure

COP1000 8

The Problem…

• 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?

McManus

Page 9: Problem Solving with the Sequential Logic Structure

COP1000 9

The Solution

• First: Research banking algorithms– We wind up with Two Equations:

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

Time)» Principal (Original Amount put into Savings)» Interest Rate (Yearly Percentage Rate)» Time (Investment Period in Years)» Amount (Principal + Interest Earned)

• Next…the Easy one– Interest = Amount – Principal

» Interest (Actual Amount of Interest)

McManus

Page 10: Problem Solving with the Sequential Logic Structure

COP1000 10

Structure Chart of Solution

McManus

Calculate Savings

Get Principal,Interest Rate

& Time

CalculateModule

PrintAmount

PrintInterest

CalculateInterest

CalculateAmount

PrintOutput

Page 11: Problem Solving with the Sequential Logic Structure

COP1000 11

Pseudocode Solution

• 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

McManus

Input

Process

Output

Page 12: Problem Solving with the Sequential Logic Structure

COP1000 12

Coding the Problem

In Visual Basic

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

use…McManus

Page 13: Problem Solving with the Sequential Logic Structure

COP1000 13

Data Dictionary

McManus

Name Variable Name

Data Type

Defined Domain Module

Principal Principal Decimal 0 < n < 1 million GetInputCalculate

Interest Rate

Rate Double 0 < n < 1 to 14+ digits GetInputCalculate

Time Time Short 0 < n < 32,767 GetInputCalculate

Amount Earned

Amount Single 0 < n < 1 million CalculatePrintResult

Interest Earned

Interest Single 0 < n < 1 million CalculatePrintResult

Page 14: Problem Solving with the Sequential Logic Structure

COP1000 14

VB Solution – Declare Variables

McManus

' Input Values by UserDim Principal As Decimal ‘Original Loan AmountDim InterestRate As Double ‘Yearly Interest RateDim Time As Short ‘Time in Years

' Output ValuesDim Amount As SingleDim Interest As Single

Internal Documentation

taking the form of Comments

Page 15: Problem Solving with the Sequential Logic Structure

COP1000 15

VB Solution – Get Input

Private Sub txtPrincipal_TextChanged( _ByVal sender As System.Object, _

ByVal e As System.EventArgs) _Handles txtPrincipal.TextChanged

'Accesses user input from Principal textbox 'converts user input (text) to numeric format

Principal = Val(txtPrincipal.Text)

End Sub

McManus

Page 16: Problem Solving with the Sequential Logic Structure

COP1000 16

VB Solution – Get Input

Private Sub txtInterest_TextChanged( _ByVal sender As System.Object, _

ByVal e As System.EventArgs) _Handles txtInterestRate.TextChanged

'Accesses user input from Interest textbox 'converts user input (text) to numeric format

InterestRate = Val(txtInterestRate.Text)

End Sub

McManus

Page 17: Problem Solving with the Sequential Logic Structure

COP1000 17

VB Solution – Get Input

Private Sub txtTime_TextChanged( _ByVal sender As System.Object, _ByVal e As System.EventArgs) _Handles txtTime.TextChanged

'Accesses user input from Time textbox 'converts user input (text) to numeric format

Time = Val(txtTime.Text)

End Sub

McManus

Page 18: Problem Solving with the Sequential Logic Structure

COP1000 18

VB Solution – Calculate Result

Private Sub cmdCalculate_Click( _ByVal eventSender As System.Object, _ByVal eventArgs As System.EventArgs) _Handles cmdCalculate.Click

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

'Calculates amount of total Interest at end of 'time period Interest = Amount - Principal

End Sub

McManus

Page 19: Problem Solving with the Sequential Logic Structure

COP1000 19

VB Solution – Produce Output

Private Sub cmdPrintResult_Click(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles cmdPrintResult.Click

'displays the output to the user 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

McManus

Page 20: Problem Solving with the Sequential Logic Structure

COP1000 20

VB Solution – the Form

McManus

Page 21: Problem Solving with the Sequential Logic Structure

COP1000 21

Getting the Input

McManus

Page 22: Problem Solving with the Sequential Logic Structure

COP1000 22

Calculating the Solution

McManus

Page 23: Problem Solving with the Sequential Logic Structure

COP1000 23

VB Solution – the Output

McManus

Page 24: Problem Solving with the Sequential Logic Structure

COP1000 24

Documentation

• Internal Documentation– For Programmers– Takes the form of comments within the

program– Helped by using Mnemonic terms

• Creates self-documenting code

• External Documentation– For Users and System Administrators– Takes the form of

• User Manuals• Help Screens• System Documentation

McManus

Page 25: Problem Solving with the Sequential Logic Structure

COP1000 25

Testing the Solution

• 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.

McManus

Page 26: Problem Solving with the Sequential Logic Structure

COP1000 26

Testing the Solution

• 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?

McManus

Page 27: Problem Solving with the Sequential Logic Structure

COP1000 27

Summary

• 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

McManus

Page 28: Problem Solving with the Sequential Logic Structure

COP1000 28

Next?

McManus