Writing a Complete Program

Slides:



Advertisements
Similar presentations
Chapter 3: Modules, Hierarchy Charts, and Documentation
Advertisements

Chapter 2: Understanding Structure
Chapter 3: Modularization
Chapter 2: Modularization
Programming Logic and Design Eighth Edition
Chapter 9: Advanced Array Manipulation
Repetition Control Structures
Modules, Hierarchy Charts, and Documentation
Programming Logic and Design Sixth Edition
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design, Third Edition Comprehensive
Programming Logic and Design Fourth Edition, Introductory
ITEC113 Algorithms and Programming Techniques
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
Objectives In this chapter, you will learn about:
Writing a Complete Program
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Modules, Hierarchy Charts, and Documentation
Understanding the Mainline Logical Flow Through a Program (continued)
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Chapter 1 Program Design
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Programming Logic and Design, Third Edition Comprehensive
Pseudocode.
Chapter 3 Planning Your Solution
Programming Logic and Design Fourth Edition, Introductory
- Meeting 4 – Writing a Complete Program
ALGORITHMS AND FLOWCHARTS
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
1 Chapter 4. To familiarize you with methods used to 1. Access input and output files 2. Read data from an input file 3. Perform simple move operations.
Simple Program Design Third Edition A Step-by-Step Approach
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design, Second Edition, Comprehensive
Programming Logic and Design Fifth Edition, Comprehensive
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 10: Using Menus and Validating Input Programming Logic and Design, Third Edition Comprehensive.
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
Chapter 4 PROCEDURE DIVISION. Paragraphs PROCEDURE DIVISION divided into paragraphs Each is independent module or routine Made up of series of instructions.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
ALGORITHMS AND FLOWCHARTS
Programming Logic and Design Fourth Edition, Comprehensive
ALGORITHMS AND FLOWCHARTS
Designing and Debugging Batch and Interactive COBOL Programs
Topics Introduction to File Input and Output
Programming Logic and Design Fourth Edition, Comprehensive
Iteration: Beyond the Basic PERFORM
ALGORITHMS AND FLOWCHARTS
Topics Introduction to Functions Defining and Calling a Function
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 10: Using Menus and Validating Input
Topics Introduction to File Input and Output
Chapter 4: Writing and Designing a Complete Program
Programming Logic and Design Eighth Edition
Presentation transcript:

Writing a Complete Program 4 Writing a Complete Program Programming Logic and Design, Second Edition, Comprehensive Chapter 4

Objectives After studying Chapter 4, you should be able to: Plan the mainline logic for a complete program Describe typical housekeeping tasks Describe tasks typically performed in the main loop of a program Describe tasks performed in the end-of-job module Chapter 4

Understanding the Mainline Logical Flow Through a Program You’re ready to plan the logic for your first complete computer program The output is an inventory report The report lists inventory items along with the price, cost, and profit of each item You can write a program that reads from an input file and produces a printed report as a procedural program—that is, a program in which one procedure follows another from the beginning until the end Chapter 4

Print Chart for Inventory Report Chapter 4

Understanding the Mainline Logical Flow Through a Program You write the entire set of instructions for a procedural program, and when the program executes, each instruction takes place one at a time following your program’s logic Chapter 4

Understanding the Mainline Logical Flow Through a Program The overall or mainline logic of almost every procedural computer program can follow a general structure that consists of three distinct parts: Performing housekeeping, or initialization tasks. Housekeeping includes steps you must perform at the beginning of a program to get ready for the rest of the program Performing the main loop within the program. The main loop contains the steps that are repeated for every record Performing the end-of-job routine. The end-of-job routine holds the steps you take at the end of the program to finish the application Chapter 4

Flowchart and Pseudocode of Mainline Logic Chapter 4

Understanding the Mainline Logical Flow Through a Program You can write any procedural program as one long series of program language statements, but most programmers prefer to break their programs into at least three parts The main program can call the three major modules as shown in the flowchart and pseudocode in Figure 4-3 The module or subroutine names, of course, are entirely up to the programmer Chapter 4

Understanding the Mainline Logical Flow Through a Program Chapter 4

Housekeeping Tasks Housekeeping tasks include all the steps that must take place at the beginning of a program Very often, this includes four major tasks: You declare variables You open files You perform any one-time-only tasks, such as printing headings at the beginning of a report You read the first input record Chapter 4

Declaring Variables Your first task in writing any program is to declare variables When you declare variables, you assign reasonable names to memory locations so you can store and retrieve data there Declaring a variable involves selecting a name and a type Chapter 4

Declaring Variables Some languages require that you provide storage size in addition to a type and name for each variable You can provide any names you choose for your variables You can choose any one-word names for the variables, but a typical practice involves beginning similar variables with a common prefix, for example, inv Chapter 4

Representation of Typical Data for INVENTORY File Chapter 4

Declaring Variables When you ask the program to read in an inventory record, four “chunks” of data will be transferred from the input device to the computer’s main memory: name, price, cost, and quantity In most programming languages you can give a group of associated variables a group name In addition to declaring variables, sometimes you want to provide a variable with an initial value Providing a variable with a value when you create it is known as initialization or defining the variable Chapter 4

Declaring Variables Declaring a variable provides it with a name and type Defining a variable provides it with a value In most programming languages, if you do not provide an initial value when declaring a variable, then the value is unknown or garbage Chapter 4

Declaring Variables Some programming languages do provide you with an automatic starting value; for example in BASIC or RPG, all numeric variables automatically begin with the value zero Be especially careful to make sure all variables you use in calculations have initial values When you declare the variables invItemName, invPrice, invCost, and invQuantity, you do not provide them with any initial value Chapter 4

Declaring Variables The report illustrated in Figure 4-1 contains three individual heading lines You are not required to create variables for your headings Using variable names is usually more convenient than spelling out the heading’s contents, especially if you will use the headings in multiple locations within your program Notice that the three heading variables defined in Figure 4-8 are not indented under invRecord like the invRecord fields are Chapter 4

Beginning of Flowchart for Housekeeping() Module for the Inventory Report Program Chapter 4

Opening Files If a program will use input files, you must tell the computer where the input is coming from This process is known as opening a file The program also needs to know the name of the file being opened In many languages if no input file is opened, input is accepted from a default or standard input device, most often the keyboard Again, if no file is opened, a default or standard output device, usually the monitor is used Chapter 4

Specifying Files That You Open Chapter 4

Printing Headings A common housekeeping task involves printing headings at the top of a report In the inventory report example, three lines of headings appear at the beginning of the report In this example, printing the heading lines is straightforward: print mainHeading print columnHead1 print columnHead2 Chapter 4

Reading the First Input Record The last task you execute in the housekeeping() module of most computer programs is to read the first data record in memory When you read the four data fields for the inventory file data, you can write read invItemName, invPrice, invCost, invQuantity, but if you have declared a group name such as invRecord, it is simpler to write read invRecord Chapter 4

Reading the First Input Record When the last task within housekeeping() reads the first invRecord, the first task following housekeeping() is to check for eof on the file that contains the inventory records Immediately after reading from a file, the next step always should determine whether eof was encountered Not reading the first record within the housekeeping() module is a mistake Chapter 4

Comparing Faulty and Correct Record-Reading Logic Chapter 4

Flowchart and Pseudocode for Housekeeping() Routine in Inventory Report Program Chapter 4

Flowchart and Pseudocode for Housekeeping() with Headings() Module Chapter 4

Flowchart and Pseudocode for Headings() Module Chapter 4

Writing the Main Loop The main loop of a program, controlled by the eof decision, is the program’s “workhorse” Each data record will pass once through the main loop where calculations are performed with the data and the results printed For the inventory report program to work, the mainLoop() module must include three steps: Calculate the profit for an item Print the item information on the report Read the next inventory record Chapter 4

Writing the Main Loop Although you can give a variable any legal name, you probably do not want to begin the name for the variable that holds the profit value with the inv prefix, because profit is not part of the INVENTORY input file Chapter 4

Writing the Main Loop The last step in the mainLoop() module of the inventory report program involves reading in the next invRecord Figure 4-15 shows the flowchart and pseudocode for mainLoop() Using a separate work variable or work field such as profit to temporarily hold a calculation is never wrong, and often it’s the clearest course of action Chapter 4

Flowchart and Pseudocode for mainLoop() of Inventory Report Program Chapter 4

Performing End-Of-Job Tasks Within any program, the end-of-job routine holds the steps you must take at the end of the program after all input records are processed Some end-of-job modules print summaries or grand totals at the end of a report The end-of-job module for the inventory report program is very simple Chapter 4

Flowchart and Pseudocode of finishUp() Module Chapter 4

Summary When you write a complete program, you first determine whether you have all the necessary data to produce the report Housekeeping tasks include all steps that must take place at the beginning of a program The main loop of a program is controlled by the eof decision Within any program, the end-of-job module holds the steps you must take at the end of a program after all the input records have been processed Chapter 4