Beruflich Dokumente
Kultur Dokumente
What is a computer?
Digital Computers
It works on data held as a large number of distinct items and operates in a
series of steps or instructions. Mostly used for commercial applications, also
by scientists and engineers engaged in the processing of large numbers of
mathematical calculations. Digital computers are the commonest computers
in the country and particularly in institutions computing centers. Some
examples of digital computers are IBM 360/370, PDP 11/34, and ECLIPSE
C60.
Analog Computers
These are types of computer which can accept data as a quantity varying
over a length of time, rather than series of distinct items each with a unique
value. Analog computers are mostly used in process control of industrial
operations and in simulation of processes.
Hybrid Computers
These combine some of the properties of both digital and analog computers.
Digital processing is usually faster and more sophisticated than the
processing which can be done by analog machines. We are mainly concerned
with digital computers in this manual.
1.3 Computer organization
Generally speaking, all computers consist of five functional parts:
Arithmetic and logical unit, memory or storage, input unit and output unit.
Memory
Input Unit
Control Unit
ALU
Output Unit
All data to be processed must first be entered into the computer memory
through the input unit. The data in memory is manipulated by the arithmetic
and logical unit. The result of the manipulation will be stored in the memory.
The result can be displayed through any output device. The control unit is
the one co-coordinating all operations of the various units within the
computer.
We shall now look at the various units one by one.
Input and Output Units
Input unit is a device used in placing data in the computer memory.
Output unit is a device used to extract information (finished product) from
the computer.
Input and output units are usually electromechanical devices. This shows
that they have moving parts rather than pure electronic parts. There are many
types of input and output devices.
Examples of input device include the card reader, optical mark reader and
keyboards and commonly used output devices are the line printer, plotter and
card punch. Other devices such as tape drive, disk drive, magnetic drum,
terminals and teletypewriter can be used both as input and output devices.
Arithmetic and Logical Unit
This unit performs the arithmetic and logical operations on the data stored in
the memory. An example of an arithmetic operation in deducting tax from an
employees gross-pay, adding the transport and housing allowances to
produce the net-pay. Example of a logical operation is to determine the sex
of an employee.
Memory or storage
The memory is a unit used to store the data on which the arithmetic and
logical unit, and control unit are to work upon. The most common primitive
unit of storage in a computer is the bit (binary digit 0 or 1).
The programmer must still know the details of how the specific
computer operates
He must also mentally translate complex operations and data
structures into sequences of low-level operations
He must also be intimately concerned with how and where data is
represented within the machine
Compiler
Object program
Translation Process
Another translator worth mentioning is the interpreter. Interpreters read the
source program directly, line by line, and generate the machine codes
necessary to carry out the instructions as they proceed through the program.
A compiler on the other hand, reads the source code and generates machine
code (called object code), usually saving it on a disk file. The object code is
loaded into the memory for execution. A language that uses interpreter is
BASIC.
CHAPTER TWO
Problem Solving With Computer
2.1 Algorithms
An algorithm is a step by step procedure that will solve a specific class of
problems. Examples are the square root algorithm, sorting algorithm,
traveling salesman algorithm, Newtons algorithm and a host of others. A
procedure is a finite sequence of well defined steps or operating, each of
which requires only finite amount of memory or working storage and takes a
finite amount of time to complete. An algorithm must have the following
properties:
1. Finiteness: an algorithm must be able to determine after a finite
Functions
Direction of flow
Indicates range of a loop
Input to a program
Output from a program
Initialization, arithmetic calculation, and moving
data (operation)
Connector
Terminator
Decision box, comparison
Iteration box
Example 1
Suppose there are 25 values to be read. Each value is to be read, squared and
the resulting value printed.
Step 1: Set = 1 (I = counter)
Step 2: Read one value and store it X
Step 3: Square X and store Y
Step 4: Print the result stored in Y
Step 5: Is counter already 25? If yes, go to step 7
examined all the necessary inputs and produce the expected results
(output).
4. Coding/programming
Now we have found a solution (algorithm) to the problem, the next step
is to develop (translate) the algorithm into a computer program.
Questions that readily come to mind include:
What are the values?
What are their types?
What subprograms are needed?
What programming language should be used?
5. Keypunching/ Data Entry
Next steps is to put the coding instruction in a specified input cards or
keyboards for input to the computer.
6. Program Testing
After the coding and keypunching, the next thing to do is to run the
program. This will help to ascertain the correctness of the program.
Testing the program will help get rid of syntactic, keypunching, and
logical errors. The programmer should ensure that he finally get the
program to run for a simple case (one that can be hand checked), and this
should be followed by a series of tests on the actual data until he has
done enough to be reasonably confident of the result.
Besides testing a program for correctness, other ways of test a program
include:
(i) Ascertaining the performance quality of the algorithm.
(ii)To determine the computational limitations of the algorithm.
These tests taken together try to provide experimental answers to the
questions: Does the algorithm work? How well does it work?
7. Execution
This is the actual program execution step, when the actual data are used,
and projects result collected. The step should be carried out only when
the program have been tested rigorously and certified correct.
8. Documentation
It is difficult to understand someone else raw code. The most obvious for
documentation is to enable individual to understand programs that they
did not write. It will also facilitate program changes, and make it easy to
use. Programs should make use of comments as much possible. A
program listing often includes:
The input specification
The output description
The deck set-up
Error messages and probable cause of machine constraints:
e.g. memory requirement, length of program execution and
devises used
SECTION B
Chapter One
Writing Programs in Basic
Preamble: Introduction a student to a programming language is like
introducing a student to a spoken language. Such introducing a student to
a spoken language. Such introduction usually starts with the allowable
alphabets. In BASIC programming language it will interest you to know
that more than the 26 English alphabets are used. This is one feature that
forms a key rudiment of learning a language. However, the three
attributes that form the rudiment of the BASIC programming language
are:
1. Well-defined characters/symbol what consist of the English alphabets
A Z, the numeric digits 0 9 and special symbols such as S, /, ;, :, *,
+, =, /, etc.
2. A set of commands or keywords such as INPUT, REM, GOTO,
DATA, END and so on. Keywords are specially defined words which
have special meaning to the computer in accordance with the defined
structure of the compiler chosen.
314.2E-2
3142.0E-3
0.003142E+3.
All these variance have the same value.
A fixed point number on the other hand refers to a representation convention
whereby the number 0 is written before decimal point and the nest number
range between 1 and 9. Examples are:
0.3142E+1
0.23E+2
0.9932E-4
to mean 3.142; 23.0; 0.00009932.
Logical Constant
Logical constant has two possible values namely TRUE and FALSE. And
the two operations that can be performed on then are called logical AND
logical OR. The various variances of logical constant by different
programming languages are;
0 and 1
TRUE And .FALSE
TRUE and FALSE
The BASIC language uses the last variance.
Literal Constant
Literal constant refers to a collection of characters placed between quotes.
Notice that a character is any symbol that can be used in computer.
Examples are :
AJIBOLA LECTURES COMPUTER SCIENCE
BIMBO BUBBLES
Y = A*x^2 + B* + C
FV = PV* (1 + R) ^ N
input/output statement
declaratives statements
assignment statement
conditional statement
5.
6.
looping/repetition/iterative statement
transfer/jump statement.
All the statements are made up of five key elements of BASIC programming
language, namely:
1.
2.
3.
4.
5.
Arithmetic operators
relational operators
functions
keywords
punctuation marks
ABS (X)
INT (X)
ATN (X)
EXP(X)
SQR (X)
SIN (X)
COS (X)
TAN (X)
SPC (X)
TAB (X)
SGN (X)
absolute value of X
greater integer not greater than X.
Arctangent of X
Exponential value of X
Square root of X
Sine of X
Cosine of X
Tangent of X
Leave X spaces
Start printing at (X+1)- th position
Sign of X sgn(X) = { -1 if x < 0
0 if x = 0
1 if x > 0
INPUT
AND
FOR.NEXT
OUTPUT OR
GOTO
ELSE
READ
DO
GOSUB
PRINT
LET
DIM
RETURN
REM
IF.THEN WHILE
END
STOP
SQR
Remark: we shall discuss some of these statements one after the other;
The REM statement- this refers to remark or comment which enables
intended users of a program to understand why, where and when the
program was written. Its purpose in a program is to document and it is
usually ignored by the compilers, because a REM statement does not
contribute to the logic of a program. The general format of REM statement
is
nn REM the remark expression
Example:
(1)
(2)
or
Example (1) a:
10 REM PROGRAM TO INPUT DATA
20 INPUT A, B, D$, WALE$
30 END
or what amount to the same thing.
Example 1 (b):
10 REM PROGRAM TO INPUT DATA
20 INPUT A
30 INPUT B
40 INPUT D$
50 INPUT WALE$
60 END
both program perform the same operation.
Example 2:
10 REM PROGRAM TO READ DATA
20 READ A4< B< COST$
30 DATA 50, -17, YOUR BILL is N20,000
40 END
Remark: The programs in example 1(a) (b) and (2) are only meant to read
(or input) those data but they will bring out the no answer on the screen
when they are run because you never requested the output from the
computer. To have output we use the PRINT statement.
The PRINT statement - With this statement any required information can
be output on the screen (VDU) of the computer in a predetermined format.
The general format is:
(nn) PRINT variable, variable . . .
Thus to show the Data that were read in example 2 above, we have the
following example:
15 REM PROGRAM TO PRINT READ DATA
20 READ A4, B, COST$
30 PRINT A4, B, COST$
40 DATA 50, -17, YOUR BILL IS $20,000
Remark: (1) When you run the program, the following output is shown on
the screen
? 50, -17, YOUR BILL IS $20, 000
(2) If you dont want everything on the same line you may write the same
program as follows:
10 REM PROGRAM TO PRINT READ DATA
20 READ 4A, B, COST$
30 PRINT A4
40 PRINT B
50 PRINT COST$
60 DATA 50, -17, YOUR BILL IS $20, 000
70 END
and the output on the screen will be
? 50
? -17
? YOUR BILL IS $20,000
(3) If you want to indicate which variable has what value then in any
chosen presentation mode chosen, you use the following kind of program.
For (2) below:
10 REM PROGRAM TO PRINT READ PROGRAM
20 READ A4, B, COST$
30 PRINT A4 =, A4
40 PRINT B = ; B
50 PRINT COST$ = COST$
60 DATA 50, -17, YOUR BILL IS $20,000
70 END
and your output looks like this
? A4 = 50
? B = -17
? COST$ = YOUR BILL IS $20, 000
(4) If you ended lines 30 and 40 in example 2 with coma, then you have
reverted to example 1, and your output will look exactly like they appear in
example 1, i.e. if you have written
10 REM PROGRAM TO PRINT READ DATA
20 READ A4, B, COST$
30 PRINT A4,
40 PRINT B,
50 PRINT COST$
60 DATA 50, -17, YOUR BILL IS $20,000
70 END
the output will be
? 50, -17, YOUR BILL IS $20,000
Thus, the comma mean the value of the next variable should be printed
immediately after variable behind which the comma is placed.
20 INPUT PV, r, n
30 R = 1 + r/m
40 N = nm
50 FV = PV* (R*N)
60 PRINT FV =; FV
70 END.
Remark: The problem solved with assignment statement only are straight
forward problems that refuse no critical decision at some points, it requires
no repetitive/recurrent/iterative operations. If such were involved then we
create a LOOP where there is a counter which enables the computer to
repeat the process for a number of times. For instance consider the
following:
Example1: Sum all the numbers between 1 and 100 inclusive.
100
n
T Tn
2
1
where Tn is the n-th term of the AP, substituting n, Ti, Tn in the formula we
have
S = 50* (1 + 100) = 50 * 101 = 5050.
And the program goes thus:
10 REM PROGRAM TO COMPUTE SUM OF AN AP
20 READ n, T1, T2
30 S = n* (T1 + T2)/2
40 PRINT n, = ,T1 = T2,SUM = ,S
50 DATA 100, 1, 100
60 END
(b) IF you are not mathematically sound then you may create loop using
various keywords and statements some of which are:
1. The ASSIGNMENT statement as counter
2. The IF . . . THEN . . . statement
3. The IF . . . GOTO . .. statement
4. The FOR . . . NEXT . . . statement
5. The IF . . . THEN . . . ELSE . . . statement
6. The DO . . . CONTINUE . . . statement
We shall discuss all this using our example. Notice that the additive identity
is 0. we therefore start summation from zero (i.e. 0) since S + 0 = S, thus
1. The ASSIGNMENT statement as counter
10 REM PROGRAM TO SUM BETWEEN 1 TO N
20 S = O
30 I = O
40 I = I + 1
50 S = S + I
60 IF I < 100 GOTO 40
70 PRINT S = ; S
80 END
Remark: in the last program, the whole logic is found in lines 40 to 60
where repetition is done 99 times. And this line 40 to 60 is called a loop.
Notice: When the LET is used, then (2) and/or (3) are used to take care of
the conditional logic (or simply put relational operations) in the program.
For instance line 60 in the program may be written:
60 IF I < 100 THEN 40
(^) takes care of (2) and (3) are now more than (4)
4. The FOR . . . NEXT statement applying this to the same problem we have
the following
10 REM PROGRAM TO SUM BETWEEN 1 TO N INCLUSIVE
20 READ S
30 FOR K = 1 TO 100
40 S = S + K
50 NEXT K
60 PRINT S = ; S
70 END
5. The IF . . . THEN . . . ELSE statement THE NATURE OF THE
OPERATION.
10 REM PROGRAM TO ROOTS OF A QUADRATIC
EQUATION
20 INPUT A, B, C.
30 P = A * C
40 D = B^2 (4*P)
50 IF D > = O THEN 60 ELSE 70
60 PRINT THE ROOTS ARE REAL ROOTS
70 PRINT THE ROOTS ARE COMPLEX ROOTS
80 END
Remark: the IF . . . THEN . . . ELSE statement enables us to define the
operation we want to perform on our variables if the condition stipulates in
the IF . . . THEN statement is false. The general format is:
(nn) IF condition THEN clause ELSE clause as in the statements
(a) 40 IF AGE > 40 THEN PRINT ADULT ELSE PRINT YOUTH
(b) 40 IF X = M THEN 50 ELSE GOTO 60
50 PRINT PAY AS YOU EARN
60 PRINT EXEMPTED etc.
Jumping Out of a loop There is provision to leave a loop if you desire,
you may then use the key word GOTO, or the conditional IF . . . THEN
statement. The following example will help students understanding.
10 REM PROGRAM TO SUM POSITIVE VALUES
20 SUM = 0
30 FOR I = 1 TO 10
40 READ X
45 IF X > 0 THEN
50 SUM = SUM + X
60 NEST I
70 PRINT X = ; sum
80 PRINT X = ; X
90 DATA 4, 15, -4, 10, 91
100 DATA 0, 9, 48, -2, 22
110 END
Another example could be the following
10 REM PROGRAM TO COMPUTE PRODUCT OF INTEGERS
20 PROD = 1
30 NOR J = 1 TO 4
40 READ P
50 IF R = INT (P) THEN 60 ELSE GOTO 80
60 PROD = PROD * R
70 NEXT J
80 X = INT (P)
90 PROD = PROD * X
100 PRINT PROD = ; PROD
110 DATA 14, 15.02, 19.10, 13
120 END
Another example is the following:
10 INPUT X
20 Y = SQR(X)
30 IF X < 0 THEN 50
40 GOTO 60
50 PRINT COMPLEX ROOT
60 PRINT THE SQUARE ROOT OF X IS; Y.
70 END
Nested loop- It is possible to have a loop within another depending on the
type of problem we are solving. Notice that two loops can not overlap each
other. an example of Nested Loop is as follows:
10 FOR I = 1 TO 6
20 PRINT * * * * * *
30 PRINT = = = = = =
40 GOTO 60
50 PRINT I AM QUALIFIED
60 PRINT = = = = = = = = = = = =
70 NEXT J
80 NEXT I
But the following example contains illegal loops
10 FOR I = 1 TO 3
20 PRINT MY RECORD IS CLEAN
30 FOR J = 2 TO 5
40 PRINT I AM PROUD TO BE A GRADUATE
50 NEXT I
60 PRINT PRAY WITHOUT CEASING
70 NEXT J
80 END
Remark: This is illegal in BASIC. If there should be more than one loops
then they must be entirely contained in one another.
The RESTORE command- Any time data are fed into the computer using
READ statement, the subroutine closes with a list of data which follows the
DATA- command. The RESTORE command returns the pointer to the start
of the DATA list. The general format is:
(nn) RESTORE this returns pointer to the start of the DATA list
also
(nn) RESTORE (line number) this return the pointer to the first
element of DATA statement on the line-number so chosen.
Example: consider the following program.
10 PRODUCT = 1
20 FOR I = 1 TO 10
30 READ A
40 PRODUCT = PRODUCT * A
50 NEXT I
55 M = J/10
60 PRINT PRODUCT =, N THROOTS PRODUCT
70 DATA 40, 2.43, 15.45, 32, 14.14
80 DATA 80, 2222, 0.37, 44, 17.
90 RESTORE
100 FOR J = 1 TO 10
110 READ A
120 PRINT DIFFERENCES OF N-TH ROOT FROM A; A N
THROOT
130 NEXT J
140 END
Notice that the last program computes the product of ten values, computes
the 10-th root of the product and obtains the difference between each value
and the computed 10-th root of the product.
CHAPTER THREE
Solving Practical Problem with Computer
Let us now apply what weve learnt so far to the kind of problems we are
familiar with
Question 1: Aseese Group of Companies (AGC) applied for a loan of
$15,000,000 at 20% compounded semi-annually from Boripe Bank Limited
payable in ten years. Write down the model to determine the total amount of
money Aseese Group of Companies will be required to pay back at the end
of the ten year, and write a programme for it.
Solution: Recall that this is asking for the future value of $15,000,000 at
20% compounded semi-annually in 10years. Thus,
FV = PV 1 r / 2 2n
Where r is the rate of interest, n is the number of years, PV is the present
value of the loan. The program is a straight forward one which requires no
loop. The following is a suggested program.
10 INPUT PV, r, n
20 Q = 1 + r/2
30 R = Q ^ (2* n)
40 FV = PV * R
50 PRINT PV = r = ; n = , n;
60 PRINT FUTURE VALUE IS; FV
70 END
Question 2: Write a program to solve the quadratic equation
2x2 7x + 5 = 0
Solution: Notice that the first point to consider is the determination of the
determinant
D = B2 - 4AC
Which determines the nature of the solution of the quadratic equation.
However, a program must take care of all possibilities.
Since there are three possible values of D namely
(i)
D > 0 with two distinct real values of D namely
(ii) D = 0 with two equal real values
(iii) D < 0 with two distinct complex values.
Then our program must take care of all the case, now the program.
10 READ A, B, C
20 D = B^2 = 4*(A*C)
30 IF D <> = 0 GOTO 70
40 X (1) = (1-B + SQR (D) / (2*A)
50 X (2) = (-B SQR (D) / (2*A)
60 GOTO 100
70 IF D = 0 GOTO 120
80 X(1) = - B/(2*A)
90 X(2) = B/(2*A)
100 PRINT X(1) = ; X(2) = ;X(2)
110 GOTO 160
120 REX(X) = - B/(2*A)
130 IM (X) = (SQR (ABS (D)))/(2*A).
140 PRINT REAL PART OF X IS; RE(X)
150 PRINT IMAGINARY PART OF X IS ; IM(X)
160 DATA 2, -7, 5
170 END
Notice that there is no remark in the program above, the reason is that it has
just been written and tested. Suppose we want to keep the program then we
put as many remarks as possible to make any future user understand the
program. This is what we referred to, in chapter two as documentation. If the
last program were to be documented, then, we have the following:
10 REM PROGRAM TO SOLVE QUADRATIC EQUATION
20 REM WRITTEN BY OJUOLAPE ENUOBO.
60 NEXT J
70 PRINT I HAVE FINISHED
80 NEXT I
90 DATA 5, 6
100 END
Solution: Following the logic of the program, the outcome looks like this on
the screen:
X + Y =11
*****
*****
*****
I HAVE FINISHED
X + Y = 11
*****
*****
*****
I HAVE FINISHED
X + Y = 11
*****
*****
*****
I HAVE FINISHED
X + Y = 11
*****
*****
*****
I HAVE FINISHED
X + Y = 11