programming examples to accompany structure topic please use speaker notes for additional...

16
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Upload: sophia-chase

Post on 12-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Programming Examples to Accompany Structure Topic

Please use speaker notes for additional information!

Page 2: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Start

Get input

number

notend of user

input

Calculate the doubleof the input by

multiplying by 2

Display/printanswer

Get input

number

Y

N

Stop Pseudocode:startget input numberwhile not end of user input calculate the answer (multiply input by 2) display/print the answer get input numberend loopstop

Page 3: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

This shows the program written from the logic in the flowchart and pseudocode on the previous slide. It also shows the output that was generated when the program was run.

COBOLCOBOL

Page 4: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

This shows the definition of the variables I am going to use in the program.

INPUT-NUM is where I will have the user key in a number to be doubled.

DOUBLE-NUM will hold the result of doubling the number.

ANS-WS is where the enter key the user presses when they want to move on to the next number is stored.

COBOLCOBOL

Page 5: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

I am using the DISPLAY AND ACCEPT commands to take in user input. The DISPLAY asks the user to key in information and the ACCEPT takes the information and stores it in the place of that name.

DISPLAY “ENTER INPUT NUMBER”.

This command prompts the user to enter a number.

ACCEPT INPUT-NUM.

This command takes the data the user enters (note the data cannot be more than two characters) and stores it in INPUT-NUM.

Note that when the user entered 123456, the computer took only the 12 and stored it as INPUT-NUM. This means that DOUBLE-NUM will be 024. It is 024 because DOUBLE-NUM has a PIC of 999 and can therefore hold three numbers.

COBOLCOBOL

Page 6: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

PERFORM UNTIL INPUT-NUM=99 means that processing will stop when the user enters 99.

While the user enters other numbers the processing between the PERFORM and the END-PERFORM will be executed. Note that one of the things I added to the pseudocode was the end loop.

The processing that is done is to use the COMPUTE statement to do the math and then DISPLAY the original number and the double number. The ACCEPT ANS-WS waits for the user to press the enter key or take some other keyboard action. Then the processing takes in a new number.Pseudocode:

startget input numberwhile not end of user input calculate the answer (multiply input by 2) display/print the answer get input numberend loopstop

COBOLCOBOL

Page 7: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

IDENTIFICATION DIVISION. PROGRAM-ID. DOUBLE. DATA DIVISION. FILE SECTION. WORKING-STORAGE SECTION. 01 INPUT-AREA. 05 INPUT-NUM PIC 99. 01 WORK-AREAS. 05 DOUBLE-NUM PIC 999. 01 USER-RESPONSE. 05 ANS-WS PIC X. PROCEDURE DIVISION. PROCESS. DISPLAY "ENTER INPUT NUMBER". ACCEPT INPUT-NUM. PERFORM UNTIL INPUT-NUM = 99 COMPUTE DOUBLE-NUM = INPUT-NUM * 2 DISPLAY INPUT-NUM " " DOUBLE-NUM ACCEPT ANS-WS DISPLAY "ENTER INPUT NUMBER" ACCEPT INPUT-NUM END-PERFORM. STOP RUN.

Pseudocode:startget input numberwhile not end of user input calculate the answer (multiply input by 2) display/print the answer get input numberend loopstop

COBOLCOBOL

Page 8: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

IDENTIFICATION DIVISION. PROGRAM-ID. DOUBLE. ENVIRONMENT DIVISION. SELECT NUMBER-FILE ASSIGN TO "E:\CIS17\COBOL\DOUBLE.TXT". DATA DIVISION. FILE SECTION. FD NUMBER-FILE DATA RECORD IS NUMBER-RECORD. 01 NUMBER-RECORD. 05 INPUT-NUM PIC 99. WORKING-STORAGE SECTION. 01 WORK-AREAS. 05 DOUBLE-NUM PIC 999. 01 EOF-CHECK-AREA. 05 EOF-IND PIC X VALUE "N". 01 USER-RESPONSE. 05 ANS-WS PIC X. PROCEDURE DIVISION. PROCESS. OPEN INPUT NUMBER-FILE. READ NUMBER-FILE AT END MOVE "Y" TO EOF-IND. PERFORM UNTIL EOF-IND = "Y" COMPUTE DOUBLE-NUM = INPUT-NUM * 2 DISPLAY INPUT-NUM " " DOUBLE-NUM ACCEPT ANS-WS READ NUMBER-FILE AT END MOVE "Y" TO EOF-IND END-READ END-PERFORM. STOP RUN.

Pseudocode:startget input numberwhile not end of user input calculate the answer (multiply input by 2) display/print the answer get input numberend loopstop

COBOLCOBOL

Page 9: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

This is the file that is being read. You can see on the output that the first line is for 02 and the second line is for 12 and the third line is for 25 etc.

COBOLCOBOL

Page 10: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Using Visual Basic, I am not exactly following the logic flowchart that was laid out.

Visual Basic is event driven. This means that when the user clicks on something, it causes certain code to happen. In this case, I am having the user enter a number and then click on Start Program. The calculation is done and appears in the box on the left.

There is no loop in this program because each time the user enters a number, they click on the Start Program button and that one calculation gets done.

VBVB

Page 11: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

VBVB

Page 12: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Option Explicit Dim wkDoubleNum As Integer

Private Sub cmdDoubleFile_Click() wkDoubleNum = txtInputNum.Text * 2 picDoubleNum.Print "#: "; txtInputNum.Text; " Double: "; wkDoubleNumEnd Sub

Private Sub cmdEnd_Click() EndEnd Sub

When the End button is clicked this subroutine is executed and the program Ends.

When the Start Program button is clicked it executed the cmdDoubleFile subroutine which does the math and calculates the double amount which it stores in the variable wkDoubleNum and then prints the results in the picDoubleNum box.

The variable wkDoubleNum is declared as an Integer.

VBVB

Page 13: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Option Explicit Dim wkDoubleNum As Integer

Private Sub cmdDoubleFile_Click() wkDoubleNum = txtInputNum.Text * 2 picDoubleNum.Print "#: "; txtInputNum.Text; " Double: "; wkDoubleNumEnd Sub

Private Sub cmdEnd_Click() EndEnd Sub

Pseudocode:startget input numberwhile not end of user input calculate the answer (multiply input by 2) display/print the answer get input numberend loopstop

The get input number is handled by providing a box on the form for the user to enter the number.

The loop is handled by the user who enters a number and then presses the key to Start Program which does the routine called cmdDoubleFile.

The user controls the loop with the click event so no loop is coded into the program.

VBVB

Page 14: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

VBVB

Page 15: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

VBVB

Page 16: Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!

Option Explicit Dim wkDoubleNum As Integer Dim wkInputNum As Integer

Private Sub cmdDoubleFile_Click() Do While Not EOF(1) Input #1, wkInputNum wkDoubleNum = wkInputNum * 2 picDoubleNum.Print "#: "; wkInputNum; "Double: "; wkDoubleNum Loop MsgBox "Processing Complete" Close #1 EndEnd Sub

Private Sub Form_Load() Open App.Path & "\double.txt" For Input As #1End Sub

Pseudocode:startwhile not end of user input get input number calculate the answer (multiply input by 2) display/print the answerend loopstop

VBVB

Pseudocode:startget input numberwhile not end of user input calculate the answer (multiply input by 2) display/print the answer get input numberend loopstop