PLF(Chapter1)

8
UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE PROGRAM LOGIC FORMULATION using C++ 1 RLD CHAPTER 1: An Overview of Computers and Logic In this chapter you will: (a) Understand computer components and operations (b) Learn about the steps involved in the programming process (c) Learn about the data hierarchy and file input (d) Use flowchart symbols and pseudo code statements (e) Use and name variables UNDERSTANDING COMPUTER COMPONENTS AND OPERATIONS Hardware and Software are the two major components of any computer system. Hardware is the equipment, or the devices, associated with a computer. For a computer to be useful, however, it needs more than equipment; a computer needs to be given instructions. The instructions that tell the computer what to do are called software, or programs, and are written by programmers. Together, computer hardware and software accomplish four major operations: 1. Input 2. Processing 3. Output 4. Storage You write computer instructions in a computer programming language, such as Visual Basic, C#, C++, Java, COBOL, etc. No matter which programming language a computer programmer uses, the language has rules governing its word usage and punctuation. These rules are called the language’s syntax. Every computer operates on circuitry that consists of millions of on/off switches. Each programming language uses a piece of software to translate the specific programming language into the computers on/off circuitry language, or machine language. Machine language is represented as a series of 0s and 1s, also called binary form. The language translation software that converts a programmer’s statements to binary form is called a compiler or interpreter, and it tells you if you have used a programming language incorrectly. NOTE: Although there are differences in how compilers and interpreters work, their basic function is the same – to translate your programming statements into code the computer can use. When you use a compiler, an entire program is translated before it can execute; when you use interpreter, each instruction just prior to execution.

Transcript of PLF(Chapter1)

Page 1: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

1 RLD

CHAPTER 1: An Overview of Computers and Logic

In this chapter you will:

(a) Understand computer components and operations

(b) Learn about the steps involved in the programming process

(c) Learn about the data hierarchy and file input

(d) Use flowchart symbols and pseudo code statements

(e) Use and name variables

UNDERSTANDING COMPUTER COMPONENTS AND OPERATIONS

Hardware and Software are the two major components of any computer

system. Hardware is the equipment, or the devices, associated with a computer.

For a computer to be useful, however, it needs more than equipment; a

computer needs to be given instructions. The instructions that tell the computer

what to do are called software, or programs, and are written by programmers.

Together, computer hardware and software accomplish four major operations:

1. Input

2. Processing

3. Output

4. Storage

You write computer instructions in a computer programming language, such as

Visual Basic, C#, C++, Java, COBOL, etc.

No matter which programming language a computer programmer uses, the

language has rules governing its word usage and punctuation. These rules are

called the language’s syntax.

Every computer operates on circuitry that consists of millions of on/off switches.

Each programming language uses a piece of software to translate the specific

programming language into the computers on/off circuitry language, or

machine language. Machine language is represented as a series of 0s and 1s,

also called binary form. The language translation software that converts a

programmer’s statements to binary form is called a compiler or interpreter, and

it tells you if you have used a programming language incorrectly.

NOTE: Although there are differences in how compilers and interpreters work, their basic

function is the same – to translate your programming statements into code the computer can

use. When you use a compiler, an entire program is translated before it can execute; when

you use interpreter, each instruction just prior to execution.

Page 2: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

2 RLD

Suppose you instruct someone to make a cake as follows:

Stir

Add two eggs

Add a gallon of gasoline – Don’t do it!

Bake at 350 degrees fro 45 minutes

Add three cups of flour

Once instructions have been inputted to the computer and translated into

machine language, a program can be run, or executed.

Computer storage comes in two broad categories. All computers have internal

storage, often referred to as memory, main memory, primary memory, or

random access memory (RAM).

UNDERSTANDING THE PROGRAMMING PROCESS

The programmer’s job can be broken down into six programming steps:

1. Understanding the problem

2. Planning the logic

3. Coding the program

4. Using software to translate the program into machine language

5. Testing the program

6. Putting the program into production

1-Understanding the Problem

� A good programmer is often part counselor, part detective!

2-Planning the Logic

� The heart of the programming process lies in planning the program’s logic.

You may hear programmers refer to planning a program as “developing

an algorithm.” An algorithm is the sequence of steps necessary to solve

any problem.

NOTE: Programmers call some code errors semantic errors. For example, if you misspell a

programming language word, you commit a syntax error, but if you use an otherwise

correct word that does not make any sense in the current context, you commit semantic

errors.

NOTE: Even though hard disk drive is located inside your computer, the hard disk is not

main, internal memory. Internal memory is temporary and volatile; a hard drive is

permanent, nonvolatile storage.

Page 3: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

3 RLD

� The process of walking through a program’s logic on paper before you

actually write the program is called desk-checking.

3-Coding the Program

� Programmers choose a particular language because some languages

have built-in capabilities that make them more efficient than others at

handling certain types of operations.

4- Using Software to translate the Program into Machine Language

5-Testing the Program

� A program that is free of syntax errors is not necessarily free of logical

errors.

6-Putting the Program into Production

UNDERSTANDING THE DATA AND HEIRARCHY AND FILE INPUT

When data items are stored for use on computer systems, they are often stored

in what is known as a data hierarchy, which describes the relationships between

data components.

In the data hierarchy, the smallest usable unit of data is the character.

Characters are letters, numbers, and special symbols. It also includes space and

tab. Characters are made up of smaller elements called bits.

Write and correct

the program code

Compile the

program

Compile the

program

Data that the

program uses

If there are no

syntax errors

List of

syntax

error

messages

Program

output

If there are

syntax errors

Page 4: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

4 RLD

Characters are grouped together form a field. Field is a single data item, such as

lastName, streetAddress.

Related fields are often grouped together to form a record. Records are groups

of fields that go together for some logical reason. E.g. individual student record

Related records, in turn, are grouped together to form a file. File are groups of

records that go together for some logical reason. E.g. Student File

Finally, many organizations use database software to organize many files. A

database holds groups of files, often called tables that together serve the

information needs of an organization.

A database contains many files. A file contains many records. Each record in a

file has the same fields. Each record’s fields contain different data items that

consist of one or more stored characters in each field.

Whether the data has been stored in a flat file or database, when a program

needs all the fields in a record, you can write programming statements to get or

input each field in one of several ways.

Get name

Get address

Get salary

Most languages also allow you write a single statement in the following format:

Get name, address, salary

Database

File

Record

Field

Character

Name: Emily Sicat

Address: Hagonoy, Taguig

Salary: 375.00

Name: Alexander Pahayahay

Address: West Rembo, Makati

Salary: 536.77

Name: Era Gannaban

Address: McKinley, Taguig

Salary: 890.00

Name: Abel Bucad

Address: Greenwoods, Pasig

Salary: 722.23

Page 5: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

5 RLD

USING FLOWCHART SYMBOLS AND PSEUDOCODE STATEMENTS

When programmers plan the logic for a solution to a programming problem,

they often use one of two tools, flowcharts or pseudo code. A flowchart is a

pictorial representation of the logical steps it takes to solve a problem.

Pseudocode is an English-like representation of the same thing. Pseudo is a prefix

that means “false”, and to code a program means to put it in a programming

language; therefore, pseudocode simply means “false code”.

The following five statements constitute a pseudocode representation of a

number-doubling problem: start

get inputNumber

compute calculatedAnswer as inputNumber times 2

print calculatedAnswer

stop

Flowcharts are the most widely used general method for describing infromation

and data processing systems.

Any information or data processing system has been defined as consisting of

three phases – input, processing and output. In other words, data inputs are

transformed in processing phase into desired outputs.

Definition of Terms:

� Data are raw facts from which information is derived or created.

� Information is communicated knowledge expressed in a form that makes

it immediatley useful for decision making.

Three Phases of Data Processing System

Phase 1: Input → Recording or Capturing

Phase 2: Processing → Classification and sorting, Storage and Retrieval,

Summarization, Analysis, Communication (reporting),

Calculation

Phase 3: Output → Displaying of Results, Printing Results

PROCESSING (Phase 2)

INPUT

(Phase 1) OUTPUT

(Phase 3)

Page 6: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

6 RLD

FLOWCHARTING SYMBOLS

The American National Standard Institute (ANSI) has adopted a standard set of

symbols to be used in flowcharting. The most commonly used symbols are

presented below.

SYMBOLS USED IN FLOWCHARTING

SYMBOL NAME FUNCTION

Terminal

Represents the beginning and the end

of the program of the program. It

contains the words “Begin” or “Start”

and “End” or “Stop

Process or

Action

Represents a step or an instruction

such as arithmetic operations and

comparisons.

Ex. Gather all materials in a box; Add 5

and 7

Input or

Output

Represent the step wherein

information such as letters and

numbers are entered by the users or

produced by the process.

Ex. Input 3 numbers; Get A, B, C; Print A

Arrow or Flow

Direction

Indicator

Indicates the flow or direction or the

next activity to be done.

Decision

Denotes a decision to be done. It

involves 2 courses of action that is

answerable by either yes/true or

no/false wherein one has to be

chosen. The symbol represents the

If…Then and For…Next Statements.

Preparation

Represents the process that will

change the direction of initialization of

any value or the execution of a

variable.

Page 7: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

7 RLD

On-Page

Connectors

Indicates that the process is continued

where the matching off-page

connector is placed on the same

page.

Example:

Off-Page

Connectors

Indicates that the process is continued

where the matching on-page

connector is placed on the different

page.

Example:

ADVANTAGES OF FLOWCHART

1. The flow of the program is easily understood.

2. It determines the validity of the processes involved in the program.

3. Debugging or correcting of errors is less complicated.

4. It produces effective program documentation.

GUIDELINES FOR PREPARING A FLOWCHART

1. Determine the following:

a. Data to be entered

b. Information to produced

c. The way the data will be entered

d. The point when data will be given

e. The point when information must produced

2. There should only be one Start/Begin and Stop/End processes.

3. Concentrate in the logic of the program.

4. Choose only the important steps, No need to represent all the steps in

the flowchart.

5. Another programmer should easily understand your flowchart.

In programming, storing of data is very important and this becomes possible with

the use of variables and constants.

A A

A A

Page 8: PLF(Chapter1)

UNIVERSITY OF MAKATI – COLLEGE OF COMPUTER SCIENCE

PROGRAM LOGIC FORMULATION using C++

8 RLD

USING AND NAMING VARIABLES

Programmers commonly refer to the location in memory called inputNumber and

calculatedAnswer as variables. Variables are named memory locations, whose

contents can vary or differ over time.

A variable name is also called an identifier.

The variable names used in our discussion follow only three rules:

1. Variable names must be one word. The name can contain letter, digits,

hyphens, underscores, or any other characters you choose, with the

exception of spaces.

2. Variable names should have some appropriate meaning.

3. Variable names may not begin with a digit, although usually they may

contain digits.

Note: As a convention, the variable names are in lowercase.

Suggested Variable Names for

Employee’s Last Name Comments

Best suggestions employeeLastName Good-descriptive identifier

employeeLast Good-most people would interpret Last as

meaning Last Name

empLast Good

Inferior and Illegal suggestions

emlstnam Legal-but cryptic lastNameOfTheEmployee Legal-but awkward last name Not legal – embedded space

employeelastname Legal-but hard to read without camel

casing

Camel casing – is the format for naming variables in which the initial letter is

lowercase, multiple-word variable names are run together, and each new word

within the variable name begins with an uppercase letter.

-End of Chapter 1-