1 / 25

Problem Solving with the Sequential Logic Structure

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

fai
Download Presentation

Problem Solving with the Sequential Logic Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving with the Sequential Logic Structure Lesson 5 COP1006

  2. Overview • Algorithm Instructions • Sequential Logic Structure • Solution Development COP1006

  3. Flowchart Symbols • Terminal • Starts, Stops, Ends • Input/Output • Input data, Output information • Assign • Apply values to variables • Process • Execute instructions Terminal Input/Output Assign Process COP1006

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

  5. Examples Print Name Print Address Print City Print State Print Zip Print Pay • Enter Name • Enter Address • Enter City • Enter State • Enter Zip • Enter Hours • Enter Wage • Pay = Hours * Wage COP1006

  6. Let’s work a problem… COP1006

  7. The Problem… • Sadie 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? COP1006

  8. The Solution • 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 COP1006

  9. Structure Chart of Solution Calculate Savings Get input Print Calculate Calculate Amount CalculateInterest PrintInterest PrintAmount COP1006

  10. 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 COP1006

  11. Coding the Problem In Visual Basic Note: Up to this point it didn’t matter what language we use… COP1006

  12. Data Dictionary COP1006

  13. VB Solution – Declare Variables Internal Documentation taking the form of Comments ' Input Values by User Dim Principal As Decimal Dim InterestRate As Double Dim Time As Short ' Output Values Dim Amount As Single Dim Interest As Single COP1006

  14. VB Solution – Get Input 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 COP1006

  15. VB Solution – Calculate Result Private Sub 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 COP1006

  16. VB Solution – Produce Output 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 COP1006

  17. VB Solution – the Form COP1006

  18. Getting the Input 1st step 2nd step 3rd step COP1006

  19. Calculating the Solution COP1006

  20. VB Solution – the Output COP1006

  21. Documentation • 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 COP1006

  22. 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. COP1006

  23. 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? COP1006

  24. 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 COP1006

  25. Next? Problem Solving with Decisions COP1006

More Related