FEU EAC ITES103 ITEI103 Flowcharting and Pseudocoding StudVersion

Preview:

DESCRIPTION

sfsfsfs

Citation preview

FEU – EAST ASIA COLLEGEInformation

Technology

Department

Module 2

Flowcharting and

Pseudo-coding

Engr. J. Cansino

Program Elements

&

Structure

Program Elements and Structure

1.) Constants – Literal Value that does not change during

program execution. It can be of the numeric or string type.

Numeric Constant - may be an integer (whole number) or a

real number (with a decimal place). It may be preceded by a

positive or negative sign.

Examples:

10 -28 +3.4 -0.413 0 201

Note: No other characters must or can appear together with the

constants. The following are examples of incorrect numeric

constants:

10,000 $14.3 10mi/hr 1/2

String Constant – A character or group of characters enclosed

in double quotation marks ( “ ). The numbers 0-9, letters a-z or

A-Z and all special characters like a space, % $ - _ & etc. may

be included.

Examples:

“J” “Jojo” “.” “1225” “J & J”

Note: The entire string constant must always be enclosed in

double quotation marks, one before the first character and one

after the last character (no more no less). The following are

examples of incorrect string constants:

“Earl Jerald Cansino” “SMART” “Avenger Assemble”

2.) Variables – Is a portion of the computer’s main memory

used to store a numeric or string constant. The value stored in

the variable must be given a unique variable name but it can

only hold one value at a given time.

Variable names must conform to the following

requirements:

a.) Must contain only letters, numbers and/or an underscore.

b.) Must begin with a letter.

c.) Must end either with a letter or a number.

d.) Must be unique that is, not a name of another variable.

e.) Must not be a keyword like ACCEPT, WHILE and the

like.

A variable may be one of the following types:

Numeric, String, Array

Note: Variable names are not case sensitive in

flowcharting and pseudo-coding, that is, upper and

lower case letters are not differentiated. For

example the variable TOTAL is the same as total.

The following are example of incorrect variable

name:

Year code 10ctr #5 Total=

3.) Expressions – is a group of program

elements consisting of operands and operators.

Operands can be a constant, variable, and or

an array element. Operators can be arithmetic,

logical, relational and/or string. Expressions

always yield a value of a certain type depending on

the type of expressions used.

Arithmetic Expressions

Hierarchy or Priority of Operators

1.) Expressions in parenthesis

2.) * / %

3.) + -

Examples:

1.) 10 + 4 * 8

2.) 50 % 6 – 5

3.) 20 / 4 * 9

32

42

2

-3

5

45

String Expressions

Examples:

1.) “3” + “4” = “34”

2.) “Ja ” + “red” = ”Jared”

Examples of incorrect usage of the +

operator:

“3” + 4 100 + “Peso”

Relational Expressions

Greater than >

Less than <

Greater than or equal to >=

Less than or equal to <=

Not equal !=

Is equal to ==

Examples:

Ctr1 > 10 3 <= 5 Temp1 != Temp2

Examples of incorrect Relational Expressions:

30 <= X <= 40 43 > < 50

Logical Expressions

Hierarchy or Priority of Operators

1.) Expressions in parenthesis

2.) Not (!)

3.) And (&&)

4.) Or (||)

TRUTH TABLE FOR NOT(!)

X !X

FALSE TRUE

TRUE FALSE

TRUTH TABLE FOR AND(&&)

X Y X && Y

FALSE FALSE FALSE

FALSE TRUE FALSE

TRUE FALSE FALSE

TRUE TRUE TRUE

TRUTH TABLE FOR OR(||)

X Y X || Y

FALSE FALSE FALSE

FALSE TRUE TRUE

TRUE FALSE TRUE

TRUE TRUE TRUE

Examples:

15 >= 10 && 15 <= 20

T && T

T

4.) Statements – are keywords, which perform

which perform a predefined procedure. The following

statements are available:

1.) DISPLAY Statement

2.) ACCEPT Statement

3.) IF Statement

4.) WHILE Statement

5.) FOR Statement

Assignment #1.1 Evaluate the

following expression.

1.) 11 % 2 + 3 * 7 –2

2.) 100 % 5 % 3 % 2

3.) – (45 / 9) * 6 + 4

4.) (5 + 7) % (10 * 2 – 5) / (15 – (8 – 6 % 4))

5.) (8 + 21 % (18 – 5 * 3)) % (17 – (6 + 13 % 5))

6.) (12 == 12 % 2 * 2) && !(( 5 > 3) && (8<3))

7.) (6 > 3) && (8 != 8) || !(10 <= 10)

8.) 15 / 3 == 15 % 39.) 5 != 11 / 2

10.) (100 * 24)/12 + 45

Assignment #1.2

Check if Valid (V) or Invalid (I)

1. Numeric Constant: “0.12345”

2. String Constant: HELLO_TRISHA

3. Arithmetic Expression: (45.9)%(5*2%3)

4. Variable Name: _J_A_R_E_D

5. Relational Expression: (5<=2*2) =10+1

6. Logical Expression: (!(!(!(F&&T))))

7. Variable Name: J5R3D

8. String Constant: “*&****hello##”

9. Numeric Constant: 1,000,000.98

10. Logical Expression: (!T&&!F)||(!F)

Evaluate the Following expressions:

1. 12 % 6 + (2 + 12 * 3) – ( 12 % 13)

2. (10 / 3 + (16 + 3) )– ((5 * 2 % 10) + 2)

3. (T || F) && (!(!F && (6 > 8) || (5< 10) || F) || !T)

4. (T || !F) || (!(!(!F || (3 < 15) || F) || !F) && T)

5. !(!T && !F) || (!T || (12 <= 12) || (T && F)|| F)

FLOWCHARTS

- A graphic form of an algorithm.

- A diagram of a sequence of steps that a program

must perform to produce the desired output.

- Traditional tool of programmers which allow flexibility

in terms of detail to be included.

TYPES OF FLOWCHARTS

1.) System Flowchart – present the total picture

without worrying about taking care of every detail.

2.) Program Flowcharts – depicts a main segment of

a complete computer program.

FLOWCHARTING GUIDELINES

1.) Always use the ANSI standard symbols.

2.) Construct your flowcharts to read from top to

bottom and left to right.

3.) Do not crisscross flowlines (used labeled

connectors and use arrowheads to indicate direction

of the flow.

4.) Make the messages and labels in the flowchart

symbol direct and descriptive.

5.) Strive for neatness, clarity and simplicity.

BASIC FLOWCHARTING SYMBOLS

Terminal Symbol

Decision Symbol

Preparation Symbol

Processing Symbol

Input/Output Symbol

Offpage Connector

Predefined Process Symbol

Onpage Connector

Flow Direction Lines

Sequential Program

Structure

For Flowchart, symbols used are the following:

Terminal symbol

I/O symbol

Preparation symbol

Direction lines

For Pseudocode, keywords used are the following:

1.) Accept

Syntax: Accept <variable>

Accept <variable1>, …, <variableN>

2.) Display

Syntax: Display “<string>”

Display <variable>

Display <variable1>, …, <variableN>

Sequential structure

#1] Input a temperature in Celsius and

output the corresponding temperature in

Fahrenheit. The formula is a follows:

Fahrenheit = 9 (Celsius) + 32

5

FlowchartStart

F = 0

Input C

F = (9.0/5.0) * C + 32

Output F

End

Pseudocode

Variables Used:

F, C are numeric

Begin

F = 0

Display ” Input temperature in Celsius: ”

Accept C

F = (9.0 / 5.0) * C + 32

Display “Equivalent temperature in

Fahrenheit:”, F

End

Sequential structure

#2] Pepperoni++ Pizza House charges 10%

service charge and 5% sales tax on the gross

bill of the customer. Create a flowchart and a

pseudocode that would input the gross bill of the

customer and the amount given by the customer

to the waiter. It must output the customer’s total

bill and change (if there’s any).

Flowchart

Start

TBill = 0

change = 0

Input GBill, Amt

TBill = Gbill + Gbill *0.1 + Gbill * 0.05

change = Amt - TBill

Output TBill, change

End

Pseudocode

Variables Used:

GBill, Amt, change, TBill are numeric

Begin

TBill = 0

change = 0

Display ” Input Gross Bill and Amount Given: ”

Accept GBill, Amt

TBill = GBill + GBill * 0.1 + GBill * 0.05

change = Amt - TBill

Display “Total Bill = ”, Tbill

Display “Change = ”, change

End

Assignment #1.2: Sequential structure

#5] Using Visual Logic, Draw and execute a FCD for

Workers at a particular company were given a 15.5%

salary increase. Moreover, the increase was

retroactive for 2 months, that is, effective two months

ago. Create a flowchart and a pseudocode that takes

the employee’s old salary as input and then output

the amount of retroactive pay (balance) due the

employee and his new salary as well.

SELECTION /

CONDITIONAL PROGRAM

STRUCTURE

For Flowchart, symbols used are the following:

Terminal symbol

I/O symbol

Preparation symbol

Flow Direction lines

Decision symbol

For Pseudocode, keywords used are the

following:

1.) Accept

Syntax: Accept <variable>

Accept <variable1>, …, <variableN>

2.) Display

Syntax: Display “<string>”

Display <variable>

Display <variable1>, …, <variableN>

3.) if-then-else

Syntax #1: if <condition> then

begin

statements

.

.

.

end

else

begin

statements

.

.

.

end

Syntax #2: if <condition1> then

begin

statements

end

else if <condition2> then

begin

statements

end

else if <conditionN> then

begin

statements

end

else

begin

statements

end

Selection/Conditional Structure

#1] Using Visual Logic, create a

flowchart and a pseudo-code that

will prompt the user to input two

number and output the highest

number entered.

FlowchartStart

Input N1, N2

End

If

N1 > N2

N

Output N2

Output N1

Y

A

A

Pseudocode

Variables Used:

N1, N2 are numeric

Begin

Display ” Input two numbers: ”

Accept N1, N2

if (N1 > N2) then

Display N1

else

Display N2

End

Assignment # 1 .3 Selection Structure#1] The fine for an over-speeding violation depends on the

speed of the erring driver, as follows:

60 to 75 mph = Php 2000.00

76 mph and above = Php 4000.00

Create a flowchart and a pseudocode to input the car’s

speed and then output the fine, if any.

#2] In the game of JACK N POY each of the two player

choose either a scissors (code X), paper (code P), or stone

(code S). If one chooses scissors and the other chooses

stone then stone wins. If one chooses paper and the other

stone then paper wins. If one chooses paper and the other

scissors then scissors wins. If they both choose the same

then the result is a tie. Create a flowchart or a pseudocode

that will input two character codes corresponding to the

object selected and then output either the message “PLAYER

1 WINS” or “PLAYER 2 WINS”

#3] Bongga Ka Day Inc. gives additional performance

bonuses to its employees every year. The amount is

dependent on the number of years of service of the

employee as follows:

Years of Service Amount of Bonus

5 years and below 50% of monthly salary

6 years to 10 years 100% of monthly salary

11 years to 25 years Php 30,000.00 or twice

the monthly salary,

whichever is higher.

ITERATIVE/ LOOPING

PROGRAM

STRUCTURE

For Flowchart, symbols used are the following:

Terminal symbol

I/O symbol

Preparation symbol

Flow Direction lines

Decision symbol

For Pseudo code, keywords used are the following:

1.) Accept

2.) Display

3.) if-then, if-then-else

4.) while-do

Syntax:

while <condition> do

begin

statements;

end

.

.

.

Iterative Program Structure

#1] Create a flowchart/pseudo code that will

generate the following number series:

10, 5, 9, 10, 8, 15, 7, 20, 6, and 25

FlowchartStart

End

If

X >= 6

N

Y

X = X – 1

Y = Y + 5

Output X, Y

X = 10Y = 5

Pseudo code

Variables Used:

X, Y are numeric

Begin

X = 10

Y = 5

while (X >= 6) do

begin

Display X, Y

X = X – 1

Y = Y + 5

end

End

Iterative Program Structure

#2] Create a flowchart/pseudo code that will

generate the following number series:

1, 1, 2, 3, 5, 8, 13, 21, 34, and 55

FlowchartStart

End

If

Sum <= 55

N

Y

Sum = X + Y

X = Y

Y = Sum

Output Sum

X = 0Y = 1

Sum = 1

Pseudo codeVariables Used:

X, Y, Sum are numeric

Begin

X = 0

Y = 1

Sum = 1

while (Sum <= 55) do

begin

Display Sum

Sum = X + Y

X = Y

Y = Sum

end

End

Assignment #1.4 Iterative Program Structure

Using Visual Logic:

#1] Create a flowchart/pseudo code that will

input five numbers and output how many of

the numbers entered are odd or even.

#2] Create a flowchart/pseudo code that will

input five numbers and output how many

of the numbers entered are positive or

negative.

Assignment #1.3 Iterative Program Structure

#3] Create a flowchart/pseudo code that will input 10

alphabetic characters and output how many of the

characters entered were vowels and consonants.

Disregard characters that are not alphabetic.

#4] Create a flowchart/pseudo code that will input 5

numbers and output the highest number entered.

#5] Create a flowchart/pseudo code that will input 6

score for quizzes (0-100). Eliminate the lowest quiz

and compute and output the average of the five

remaining quizzes.

Recommended