Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name,

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Programming Languages and Paradigms
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 10 Introduction to Arrays
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Going Round and Round: the Repetition Structure Chapter 4.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
CPS120: Introduction to Computer Science Decision Making in Programs.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Higher Grade Computing Studies 3. High Level Language Constructs Higher Computing Software Development S. McCrossan 1 Simple Data Types Integer: An integer.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
JavaScript, Fourth Edition
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
So now we’re programming What do programs do? Manipulate (process) data Math Read files Write to files Create files.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Language Find the latest version of this document at
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Scion Macros How to make macros for Scion The Fast and Easy Guide.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Definition of the Programming Language CPRL
Test 2 Review Outline.
User-Written Functions
Chapter 7: User-Defined Functions II
Function There are two types of Function User Defined Function
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Lecture 3 Functions Simple functions
6 Chapter Functions.
Coding Concepts (Basics)
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Procedures Brent M. Dingle Texas A&M University
Tonga Institute of Higher Education
Chapter 9: Value-Returning Functions
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Flow of Control.
The structure of programming
COP 3330 Object-oriented Programming in C++
Standard Version of Starting Out with C++, 4th Edition
ITM 352 Functions.
Presentation transcript:

Files Input and Output

Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name, Last_Name : String[20]; Paygrade : String[1]; Data_Line : String; Gross_Pay, Net_Pay, Withholding_Amount, SS_WithHolding_Amount,Deductions : Real; Gross_Pay_Accumulator : real; Personnel : Text; Found : Boolean; Blank_Location,error : Integer;

Begin clrscr; Assign(Personnel, 'A:\Personel.txt'); Reset(Personnel);

Reset opens a file to Read Rewrite opens a file to Write Rewrite erases any previous contents

Close(Personnel); End. (previous code)

Reading Input from a File Readln(Personnel, Data_line); File Variable variable

Reading Input that is Integer and/or Real Space is a delimitor Readln(Personnel, hours1, hours2, hours3)

String Input Size can be a delimitor Use String Functions to Separate

Writing to a File Writeln(Personnel,SS,blank,First_Name); File Variable Variables }

Writeln(lst,SS,blank,First_Name); Sends output to printer Variables } Don’t forget to include the ‘Uses Printer’ statement

Branching

Option A Option B Option C Evaluation of a condition

Conditions Equality Grade = A Greater Than A > B Lesser Than B < C

Truth Boolean Operators AND OR XOR NOT

(Grade = ‘A’) and (Class = ‘COP1000’) True

(Grade = ‘A’) or (Class = ‘COP1000’) With ‘or’ at least one must be true Expression or Condition

(Grade = ‘A’) XOR (Class = ‘COP1000’) With ‘XOR’ only one can be true -- not both Expression or Condition

(Not Grade = ‘A’) and (Class = ‘COP1000’) Not evaluates the expression and then gives it the opposite value. Expression or Condition

The IF Statement

IF THEN ELSE

IF Score > 90 THEN Letter_Grade := ‘A’; If the student’s grade is 91 or better, then the variable Letter_Grade will have a value of ‘A’. If the score is less than 91 then no value will be assigned to Letter_Grade

IF Score > 90 THEN Letter_Grade := ‘A’ ELSE Letter_Grade := ‘B’; This code means everyone will get an A or a B

IF Score > 90 THEN Letter_Grade := ‘A’ ELSE IF Score > 80 THEN Letter_Grade := ‘B’ ELSE IF Score > 70 THEN Letter_Grade := ‘C’ ELSE Letter_Grade := ‘D’;

No Punctuation before the ‘ELSE’

If Paygrade = 'A' then Begin (* Paygrade=A *) Gross_Pay := Hours_Worked * A_Payrate; Withholding_amount := Gross_Pay * A_withholding_Rate; End (* Paygrade=A *) else Begin (* Paygrade=B *) Gross_Pay := Hours_Worked * B_Payrate; Withholding_Amount := Gross_Pay * B_Withholding_Rate; end; (*Paygrade=B*) SS_Withholding_Amount := Gross_Pay * SS_Withholding_Rate; Deductions := SS_withholding_Amount + withholding_Amount; Net_pay := Gross_Pay - Deductions; Gross_Pay_Accumulator := Gross_Pay_Accumulator + Gross_Pay; Net_Pay_Accumulator := Net_Pay_Accumulator + Net_Pay; WH_Accumulator := WH_Accumulator + Withholding_Amount; SS_Accumulator := SS_Accumulator + SS_Withholding_Amount;

Vocabulary Iteration Procedures Procedure Calls Encapsulation

What do procedures do? Organize code by tasks Allows reusable code Protects values

Where do they fit? Program name; CONST VAR Procedure name Begin ____ End; Begin (*Program Body *) ____ _____ End;

Begins with the word Procedure Give the pocedure a name Procedure Open_Files; Begin Assign(Income, Data_File); End;

Begin Open_Files; Initialize_Count; While Not EOF(Income) DO Begin If Sex = ‘F’ Then Add_Females Else Add_Males; End; Compute_Averages; Output_Results; Close_Files; End; ‘Calling’ the Procedure

Naming a Procedure Usually, procedures have a verb indicating purpose of procedure Example: Calculate_Scores

Local Variables vs Global Variables

Global Variables Declared at the beginning of the program All procedures, all code recognizes global variables

Local Variables Declared within the procedure Known only within the procedure Protects against unintended consequences

Procedure Compare_Values ( VAR Val1 : integer; VAR Val2 : Real) ; Var I : Integer; Begin For I := 1 to Val1 DO Val2 := val2 + val1 End;

Parameters values that are passed to functions, procedures or other subprograms

Passing Parameters

What’s the difference ??? Reference, Formal or VAR parameters pass the address of the variable to the procedure. Thus any changes the procedure makes to the variable is passed back to the calling program. Value parameters pass a copy of the variable. Any changes the procedure makes to the value of the variable is not retained in the calling program.

Begin ________; ________: Compare_Values (Value1, Value2); ________; End. Parameters

Procedure Compare_Values ( VAR Val1 : integer; VAR Val2 : Real) ; Var I : Integer; Begin For I := 1 to Val1 DO Val2 := val2 + val1 End; Order is important. The parameters in the code which calls the procedure must be in the same order as the procedure

Value1 Val1 Val2 Value2 Procedure Call Make sure your data types are consistent

Begin Open_files; Initialize_Accumulators(WH_Total, SS_Total, Gross_Total, Net_Total); While not eof(Payroll) Do Begin (* payroll outer loop *) Readln(Payroll, Payroll_SS, Hours_Worked); Initialize_Inner_Loop; While (not eof(personnel)) and not found do Begin Readln(Personnel, dataline); Personnel_SS := dataline; If personnel_ss = payroll_SS then Begin Parse_Dataline (personnel_SS, First_Name, last_name, paygrade); ________ Formal Parameter

Procedure Parse_Data(DATA : STRING; VAR VAR FNAME, LNAME : String); CONST BLANK = ‘ ‘; VAR Blank Location : Integer; BEGIN Blank_Location := POS(Data,Blank); FNAME := COPY(DATA,1,Blank_Location -1); Delete(Data, 1, Blank_Location); LNAME := DATA; END; Value Actual Local

Scope

Definition Scope concerns the visability of variables

Program Example; VAR X : Integer; Procedure A; VAR y : Integer; Procedure B Var z : Integer; Begin (*Procedure B *) ____ End; (*Procedure B *) Begin (* Procedure A *) End; (* Procedure A *) Begin (*Program Code *) X Y Z Scope of Each Variable

Functions

Differences Between Functions and Procedures Procedures capable of returning to the calling program more than one value. Functions return only one value.

Function Parse_Name(Data : String) : String Keyword Function Name Parameters Data type of value the function returns

Important! A function does not automatically assign a value to a variable. You must program it that way

FIRST_NAME := COPY(DATA, 1, POS(DATA,BLANK) -1) Blank_Location := POS(DATA,BLANK); FIRST_NAME := COPY(DATA,1,Blank_Location-1) note: the second method does not store the location of the blank in a variable. It can not be used later in the program.

Recursion A function which calls itself

That’s Enough

The End