Logic Formulation

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Logic Formulation as PDF for free.

More details

  • Words: 4,369
  • Pages: 19
1

CHAPTER 1

INTRODUCTION, BASIC SYMBOLS AND VARIABLES

1 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

2

Lesson 1: Computer System Components In computer system, one cannot have an understanding of its operation without the careful analysis of its components. As a starter, it is important to first lean the basics and relationship of these components. When we are given an objective to compute large numbers, the first instinct that comes to our mind is use a calculator. When we have the calculator, there are things that we think and do unconsciously. The first response upon knowing the objective is automatic, we immediately analyze the objective is automatic, we immediately analyze the objective by identifying the mathematical operations needed (addition, subtraction, multiplication, division, exponentiation, etc.) then, we input the data to be computed by pressing the numbers and the identified mathematical operations, we then press the equal sign key to process the inputted numbers. The result is then shown on the display of the calculator; this result whether correct or erroneous is what we call output. The relationship of the input, process, and output components producing the result doesn’t stop there. The output we see on our calculator provides two things, first is the incorrect result, and second is the correct result. We get incorrect result when we accidentally input wrong data by pressing either negative values or error results. When we get correct result from our calculation, chances are another instinct comes in our mind; we either use the result again as an input for another computation, or store the computed result in the memory for future computations. These output whether correct or incorrect, and whether used as input for another computation is the feedback component of the computer system. The relationship of the components is shown below:

INPUT

OUTPUT

PROCESS

2 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

3

The Blue Print The relationship of the components is the blue print of a computer system. Blue print is the logic and ideas transformed into a map of how computer should do things. It is implemented through software (instructions that tells the computer what to do) and executed through the interaction of different hardware devices (physical computer itself). Blue print also serves as the basis of how a computer functions, and how data are processed inside the computer. Let us have a step backward, recall that in a computer system, we key in using a keyboard or mouse data for processing—data in computer system refers to numbers, alphabets and alphanumeric characters. The entered data is then processed by the Central Processing Unit (C.P.U.). The C.P.U. inside the computer box is just a tiny square piece of Integrated Circuit attached on the mother board that normally has a dimension of 1” x 1”, though small in size, its functions in the computer system is enormous because it functions as the brain of the computer. Central Processing Unit Components The computer brain (C.P.U.) has different components that work cooperatively to execute instructions. These components are: · ·

·

Control Unit (C.U.) – The command center of the processor. It has three primary functions: read and interpret instructions, direct flow of operations of computer and hardware devices and, control the flow of instructions and data. Register – A temporary storage microchip that holds data during processing. It is located inside the CPU and has the same function that of a memory, specifically the type Random Access Memory (R.A.M.) because of its volatile character – data are lost once electric current is turned off; it just holds data temporarily for processing. Arithmetic and Logic Unit (A.L.U.) – the most significant component inside the C.P.U. for our learning of the “behind the scene”. It is the chief of operations of the computer. A.L.U. performs two major operations in processing data: all mathematical computations (addition, subtraction, multiplication, and division) and all logical operations (comparisons of data such as; greater than, less than, equal, greater than or equal to, and less than or equal to). These processing are the main concentration of how a computer process data, and is the foundation of the “behind the scene”.

Behind the scene in action As we have read the blue print of behind the scene, it is now clear why a computer is defined as an electronic device that can accept, process, and store vast amount of data to produce useful information. Let us now zero in on behind the scene and understand its language of ABC. Computer programmers write computer instructions in computer programming language. Just as people speak their native dialect, programmers write programs in different languages; some examples are C++, Visual Basic, Java and COBOL. No matter which programming language a Computer programmer uses, the language has rules governing its

3 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

4

terminology and punctuation. These rules are called the language’s syntax. This syntax is a rule that governs a programming language is unique and have their specific set of syntax. When ideas are converted into a computer program, it is sent to the CPU processing. However, The CPU cannot understand the code (computer program) and will not do anything because of language incompatibility. For the CPU to understand the code, a language translator is needed. The language translator software is called compiler or interpreter. The role of compiler is to transform the code into a machine language (series of 1’s and 0’s to represent ON and OFF states of the computer devices. It is also the representation of the digital electronic charges inside the computer). Once the code is compiled, it changes its form from computer program with strict syntax to a readable machine language that can be interpreted by the CPU. CPU then executes the instructions and performs the tasks. Lifeblood of a Computer System Tasks executed by the CPU vary from simple acceptance of data to a complex merging of different files. Whatever tasks the CPU is instructed to perform, the instruction would have to be in the language of computers. Computer program is the lifeblood of a computer system it is the fluid that runs through the veins of the computer that tells what the organs to function and what action to perform. With the analogy, we can define computer program or code as a set of logical instructions given to a computer for data processing that is interpreted by a compiler to produce useful information. Programmers integrate ideas to a code in no simple way. In their practice of developing computer programs, it brings to them the hurdles to accomplish the task. Being new to programming, we might not be ready yet to overcome the challenges. As a guide, programmers suggest to observe the following guide questions in planning and developing a program: 1. 2. 3. 4. 5. 6. 7.

What is the problem? What are the available data? What output information or data is needed? When do you need the output? What processing is needed to produce the output? What is the desired output? Where should the output go?

We can now agree that code is the gasoline that powers the computer. It is now the role of behind the scene to translate ideas into a form that is friendlier and easier to understand and illustrate. Behind the scene can be illustrated by the use of flow chart. Flowchart is a modeling tool used to illustrate data, instructions, process, information, and workflow by the use of specialized symbols. In the world of computers we can simply say that flow chart is a step-by-step graphical representation of a solution. The basic symbols used in programming are presented in Table 1. Table 1. Basic Flowchart Symbols SYMBOL

NAME

DESCRIPTION

4 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

5

1

A

No

TERMINAL

Defines the starting and ending point of a flowchart

INITIALIZATION

The preparation or initialization of memory space for data processing

INPUT/OUTPUT

The inputting of data for processing

PROCESS

Manipulation of data (assignments and mathematical computations)

FLOW LINES

Defines the logical sequence of the program. It points to the next symbol to be performed.

ON-PAGE CONNECTOR

Connects to the flowchart to avoid spaghetti connection on the same page.

OFF-PAGE CONNECTOR

Connects the flow chart on different page to avoid spaghetti connection

DECISION

Process conditions using relational operators. Used for trapping and filtering data.

yes

Variables The next most important thing to know is how data are represented in the program. Recall that data are entered into the program using a keyboard or mouse, and the data entered for processing are saved on a certain location. This location is called variable. Variable is a primary storage location that can assume different numeric or alphanumeric

5 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

6

values. It is also a memory space allocated by a computer user for processing and storing data. Variables like humans need names to be identified. Microsoft suggests a variable naming convention called Hungarian Notation. Hungarian Notation specifies a variable with both the data type and its description. The first three characters in this notation specify the data type and the remaining characters are the description of the variable. Date type is the type of the data being processed in the program. However, data type is language specific and may have different representations. Table 2 shows the basic data types with their descriptions. Table 2. Basic Data Types DATA TYPE

DESCRIPTION

*Integer

Whole number Integer with Decimal numbers Alphabets and special characters

**Float or double *character *string

Combination of characters Reply of the Compiler in the program that is performed behind the scene.

*Boolean

EXAMPLE 10 355 1590 54765 1 34000 3.1416 0.12 1.01 12.12 99.99 0.04 JWA *&? Sum age int_1 a total grand total Values are either 1 for YES or 0 for NO

*used mostly by all programming languages **C/C++, Java, Visual Basic and other programming language specific *** Note that the data types above are not the only data types used in programming. Other programming language may have their own set of data types with specific descriptions that are not presented above. Table 3. Sample variable names VARIABLE NAME Intsum DblTax FltRate ChrReply StrLname BooAns

using Hungarian Notation DATA TYPE Integer Double Float Character String Boolean

DESCRIPTION Sum. Tax Rate Reply Lname Ans

Data type is important in programming. However, data type is unique to every programming language. In the study of programming logic, data types will not be integrated in the discussion to allow pure analysis and logic development among students. The standard naming convention for variables used without any biases to any programming language are presented through its characteristics: Variables name characteristics

6 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

7

· · ·

Must be descriptive and significant to the data, problem and its solution. It should be descriptive enough to identify the process or data it is referring to. Combinations of alphanumeric characters (alphabet, numbers and special characters) can be used as a variable name provided that it starts with an alphabet. Although the length of variable name can have 256 characters, the lesser the number of characters the more efficient the program is.

Examples: Class_size sum net_income

tuition char3 l_name

intl f_name total

income answer grand_total

Computer Operators: Processing Data The languages of computers need to be converted to a language that the CPU can understand. For the programmer to instruct computers to process mathematical computations and logical operations, they would have to define symbols that the computers can understand. The symbols presented are the generic symbols used by computers for processing. However, as you progress to the different programming languages the mathematical and relational symbols may vary. ARITHMETIC OPERATORS OPERATION

SYMBOL

EXAMPLE

Multiplication

*

A*B

Division

/

X/Y

Addition

+

Intl +_ int2

Subtraction

-

A–B

Exponentiation

^

C^2

EXPLANATION Multiply the value of variable A to the value of variable B Divide the value of variable X by the value of variable Y, Add the value of variable intl to the value of variable int2 Subtract the value of variable B from the value of variable A. Square the value of variable C

Examples: MATHEMATICAL EXPRESSION

COMPUTER EXPRESSION

1. 3xy 2. x2 + y2

3*x*y (x*x) +(y*y) or x^2 + y^2 (3*X) / (y*y*y)or (3*x) / (y^3) [(a*a) + (b*b)] / (c*d) or (a^2 + b^2) / (c*d)

3. 3x Y3 4. . a2 + b2 . Cd

7 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

8

In the examples, it is important to note that the arithmetic hierarchy of computation still applies in computers. The addition of exponentiation and parenthesis is now included in the hierarchy giving the “MDAS” added acronyms, PEMDAS (Parenthesis, Exponentiation, Multiplication, Division, Addition and Subtraction) ARITHMETIC OPERATORS OPERATION

SYMBOL

EXAMPLE

Greater than

>

A>B

Less than

<

X
==

Int1 = = int2

Not equal to

<>

Net <> gross

Greater than or equal to

>=

Pay1>=pay2

Less than or equal to

<=

Age1 <=age2

Equal to

EXPLANATION The value of variable A is greater than the value of variable B. The value of variable X is less than the value of variable Y. The value of variable int1 is equal to the value of variable int2 The value of variable net is not equal to the value of variable gross The value of variable pay1 is greater than or equal to the value of variable pay2 The value of the variable age1 is less than or equal to the value of variable age2

TYPES OF VARIABLES The role of variables in programming is very crucial. In the processing stage of a program, the variables hold the data and information (whether it will be stored in the memory or used for computation or printing processed data). The challenging part in this stage is identifying who gets the processed data? This can be clarified by the descriptions of the types of variables below: · ·

Destructive Variable – variables that destroy or change its value after processing. This type of variable is normally the temporary storage of data during processing. Constructive Variable – variables that maintains its value after being used in the process.

Let’s illustrate: Suppose that we have variables A and B and the values of A and B are 5 and 10 respectively. What will happen if we have an instruction of A and B after performing the instruction? Who gets what data? This is the confusing part . . . to further illustrate:

8 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

9

5

=

These are the original values of the variables before processing

1

A

B

During the process this is what’s happening to the variables:

Variable A gets value of variable B

= 5

1

A

the

B

After processing the instruction this is what happened to the value of the variables.

5

=

1

A

Take note of the value of variable A and the value of variable B after processing

B

As illustrated, Variable A has now the same value with variable B, 10, and the original data of variable A, 5 was destroyed. This is what we mean by destructive and constructive variable. As a rule, the manner of transferring data from one variable to another is always from right to left. Do not confuse your self from your algebra. Let it sink to you from now on that this is how you will transfer data from one variable to another.

9 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

10

CHAPTER 2

Working with Process Symbol

10 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

11

Flowcharting is as easy as typing ABC or counting 123, the challenging part in drawing flowchart is the analysis. Let us start harnessing our logic and look at the analyses of the problems in drawing flowcharts. Basic things to remember in flowcharting:

START

A flowchart always starts with the instruction START or BEGIN… the only guideline to remember in the terminal symbol is the consistency of terminologies. The organized way of writing the instruction is to use the

Initialization symbol is where you prepare the variables to be available for use. When you initialize, you are also allocating memory space for your data storage. As a rule we always

The processing symbol is only limited to; assignment of new value to the variables and mathematical computations.

STOP

Since we use START in the opening of our flowchart, for consistency

To be familiar in drawing flowcharts, the learning exercises below will guide us in understanding the foundations needed in logic formulation needed in logic formulation, and for us to be ready to face challenging problems as we progress. Learning exercises: 1. 2. 3.

Create a flowchart that will store the value of A and B, where A= 5. Create a flowchart that will assigned stored value of C to both A and B where A = 15, B = 10 and C = 20 Create a flow chart that will move the value of A and B and B to C. The values of the variables are: A = 25, B = 30 and C = 35.

11 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

12

4. 5.

Create a flowchart that will arrange the values of A, B and C in ascending order where Variable A will gets the lowest value and variable C gets the highest value. The values of the variables are: A = 15, B = 10 and C = 20. Create a flowchart that will arrange the values of A,B,C and D in highest to the Lowest order where variable A gets the highest value and variable D gets the lowest value. The values of the variables are: A = 3, B = 9, C = 12 and D = 6

Solution No. 1 Problem: Create a flowchart that will store the value of A to B, where A = 5. Analysis: If we are to solve the problem, it is clearly stated that our objective is to store the value of variable A to variable B. We are not interested if variable B has a value because all we need to do is store the value of variable A to variable B. We know that variable B is the destructive variable and it will destroy its original value and get the value of variable. Flowchart:

START

A=5 B=0

B=A

The uses of initialization in this flowchart are three things: 1) Allocate two memory locations for variables A and B; 2) Assign number 5 to variable A; and 3) Assure that variable B has no value.

The expression B=A is an assignment operation. The value of variable B, Destroying the original value, 0 of variable B. After the assignment process, the new value of Variable B is 5.

STOP

12 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

13

Solution No. 2 Problem: Create a flowchart that will assign stored value of C to both A and B where A = 15, B = 10 and C = 20 Analysis: The objective in this problem is to store the value of variable C to variables A and B. Regardless of the data stored on both variables A and B, we have to assign the value of variable C. This would mean that the original data of variables A, 15 and B, 10 will be destroyed.

Flowchart: The processing of data in the process symbol is suggested to be as detailed And exact as possible. In the solution shown, we can see that the assignment of the data from variable C to variables A and B are done separately. This is to ensure that the processes are clear and done in step-by-step manner. If the programmers place all the assignments in one process symbol, the process will look like this:

START

A = 15 B = 10 C = 20

A=C=B A=C Or

B=C

13 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

14

A = C; B = C

Clearly we can see the confusion that we cannot identify who gets the data and who retains the original data. More so, in separating instructions with a semi-colon, it sends a meaning that two assignments are performed in a STOP single process symbol and should not be practiced. Solution No. 3 Problem: Create a flowchart that will move the value of A to B and B to C. The values of the variable are: A = 25, B = 30 and C = 35. Analysis: The greatest challenge in this learning exercise is not moving the values of A to B and B to C, but on how to preserve the value of B before it will be stored to C. If we move the data of A to B immediately we destroy the data of B, and the data on B can no longer be stored to C because it was already lost. The best way is to use the swap technique. Let’s illustrate: Given variables and their values:

30 A Output Requirement:

35

B

C

30 A

B

C

Moving the data of A to B destroys the value of B 25

35

14 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

15

A

B

C

Moving the destroyed data of B to C now gives a wrong solution. 25

Implementing Swap Technique would need a new variable say Temp to temporarily hold the data of B so that during the process the data is preserved. Since we know that we have to save first the data of B before it accepts the data from A, let us move the data of B to Temp.

0 Temp

A

B

C

We can now destroy the data on B since it is now saved on a temporary variable. 25

Temp

A

B

C

The first objective of moving the data of A of B was accomplished and we can now save the data from Temp to C to accomplish the problem. 30

Temp

A

B

The output after the using the swapping technique is:

15 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

C

16

Temp

A

B

C

Flowchart:

START

A=20 B=25 C=30

Since

Temp = B

B=A

The swapping technique is very clear in this flowchart. You can see the pattern of the swapping of data from variable of variable.

In the first process, the data on B was first saved to Temp . the data was saved, we can now destroy the data on B. In the second process, B gets the value of A, Destroying the data of B. We don’t have to worry if in this process the data B will be destroyed because the original data was already save to Temp.

A = Temp In the last process, the preserved data on Temp is now save to A destroying the original data. Moving the data to A from Temp completes the swapping process and the requirements of the problem.

P

Solution no.5 Problem:

16 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

17

Create a flowchart that will arrange the values of A,B.C and D in a Highest to Lowest order where variable A gets the highest value and variable D gets the lowest value. The values of the variable are: A = 3, B= 9,C=12 and D=6. Analysis: The problem in this learning exercise is to arrange the data in Highest to Lowest order. It also has a clear direction on where to save the arranged values, variable A should get the highest value (12) and variable D should get the lowest value (3). We know from the given values that the highest value stored in variable C should be placed in variable A, and logically we connot immediately move the data from C to A because we might destroy the data n A. Swapping the contents would need additional variable to temporary hold the data that we want to swap and to preserve the data. Variable temp will initialize to function as temporary holder of the data to another. The solution using the swapping technique is illustrated as follows:

3

9

12

6

A

B

C

D

12

9

6

3

A

B

C

D

Output Requirement

Given variables and their values: The presence of variable Temp is for the preservation of data that will be used to save the data on variable A for A to accept Highest number.

3

9

12

6

17 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

0

18

A

B

C

D

temp

Since the data of Variable A is now saved, we can now destroy the data of A and move the Highest.

3 A

9

12

B

3

3

D

temp

C

The value of variable C, 12, the highest value is now stored on variable A, Logically, both C and A have the same values now and we can now destroy the value of variable C. Since variable B has the second highest value, variable B will not be part of the process of swapping. Let us now move the data on variable D to variable C.

12

9

12

6

3

A

B

C

D

temp

To complete the solution, the data kept on the temporary variable should be placed to variable D.

12 A

9

6

16

C

D

B

3 temp

The final data of the variables after the final swapping process is:

12

9

6

3

18 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

3

19

A

B

C

D

temp

19 F.C Ledesma Avenue, San Carlos City, Negros Occidental Tel. #: (034) 312-6189/(034) 729-4327

Related Documents

Logic Formulation
November 2019 22
Logic
May 2020 28
Logic
November 2019 49
Logic
November 2019 44
Logic
May 2020 29
Logic
December 2019 64