Logic Formulation
Logic Formulation
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:
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.
INPUT OUTPUT
PROCESS
2
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
3
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.
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”.
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.
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:
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.
4
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
5
The preparation or
INITIALIZATION initialization of memory space
for data processing
Manipulation of data
PROCESS (assignments and
mathematical computations)
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.
The standard naming convention for variables used without any biases to any
programming language are presented through its 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 tuition intl income
sum char3 f_name answer
net_income l_name total grand_total
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
Examples:
1. 3xy 3*x*y
2. x2 + y2 (x*x) +(y*y) or
x^2 + y^2
3. 3x (3*X) / (y*y*y)or
Y3 (3*x) / (y^3)
4. . a2 + b2 . [(a*a) + (b*b)] / (c*d)
Cd or (a^2 + b^2) / (c*d)
7
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
8
ARITHMETIC OPERATORS
TYPES OF VARIABLES
· 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
A B
A B
After processing the instruction this is what happened to the value of the variables.
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
10
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
11
Learning exercises:
11
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
12
4. 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.
5. 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
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:
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
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:
30 35
A B C
Output Requirement:
30
A B C
35
14
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
15
A B C
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.
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 C
15
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
16
Temp A B C
Flowchart:
START
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
P the requirements of the problem.
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
Output Requirement
12 9 6 3
A B C D
3 9 12 6 0
17
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
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 9 12 3 3
A B C D temp
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 9 6 16 3
A B C D temp
The final data of the variables after the final swapping process is:
12 9 6 3 3
18
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327
19
A B C D
temp
19
F.C Ledesma Avenue, San Carlos City, Negros Occidental
Tel. #: (034) 312-6189/(034) 729-4327