Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

Introduction to Computer Science &

Programming I

girmay_2008@yahoo.com
Basic Engineering
Computer Programming
Basics of computers
 Computer is an electronic device, operating
under the control of instructions called
programs .It is a device which can accept
data, manipulate the data according to
specified rules, produce information from the
processing, and store the results for future
use.
Characteristics of computers
Speed: - computers can calculate at very high
speed. Their speed is measured in milliseconds,
microseconds, nanoseconds
 Accuracy: errors in computing (data
processing) is mostly due to human factor
 Storage:-can store large amount of data
Versatility: can be programmed and applied
for different purposes
 Diligence:-can handle very routine and
repetitive tasks
Information and data processing
 Data is a collection of raw facts used to
generate information.
 Hence information can be defined as data
that has been transformed into a meaningful
and useful context for specific end users
 i.e. It is a refined or processed data.
Cont.
Hence to produce information raw data is
collected, the collected data is processed,
output is produced and the output is
transmitted to concerned users
Data processing has a cycle consists of three
parts
Input -------> Process -----> Output
Computer System
A computer system is composed of the
components that are classified either as
hardware or software.
Computer hardware
Computer hardware is
 a physical part of a computer
 a part we can see and touch
 composed of a number of electronic and
electromechanical parts, which interact with
each other to perform a given task .
 the different parts are interrelated by a cable
like device called Bus , which is used to
facilitate communication between parts
Computer is in general does is it takes an input in
various forms, it process the input according to a given
set of instructions and produces an output in various
forms.
The central processing unit (CPU) (microprocessor)

- it is most expensive and main component of a computer


- it consists of control unit (CU) , arithmetic and logic unit
(ALU) and registers
The purpose of the CPU is
- to fetch instruction ( to read instruction from memory
-interpret instructions and determine actions
-- fetch data : the execution of instruction may required
reading of data from memory or input devices
-- to process data : the execution of instruction may require
performing arithmetic or logic operations on the data .
-- write data : the result of execution may require writing a
data to memory or output devices
The control unit (CU)
 is also known as the nerve center of the computer system
 it controls , supervises the overall activities of the computer
 monitors the execution of any program processed
 The CU coordinates and controls the activities of different
components of the computer system in the same way that the
brain directs the action of human body
 Its primary function is to select and interpret instructions and
to send appropriate signals to other units in the computer.

i.e. CU does not execute instructions rather it directs to other


units
The arithmetic and logic unit (ALU)
- It performs Arithmetic operations ( addition ,
subtraction , multiplication, division) and logical
operation like comparisons .It performs 90% of
your computer job.

Registers
- They are memories , when an instruction
loaded from memory it is placed first in the
register, to wait signal from the control unit and
data also placed in register prior to execution in
the ALU .
Storage Devices
 Storage Devices: there are two types of
storages primary/main storage (memories) and
secondary/Auxiliary storages (memory).
Primary storages are of two types RAM and ROM

RAM main (primary) memory


- it referred to random access memory
- it is directly accessible by ALU and CU
- it is possible to write a data to and read data from RAM
- it is a working area of a computer
- every location(cell) with in the RAM has unique address
- it holds temporarily the instructions and data elements
that are currently being used in a process
i.e. it is volatile (when the power is off the data erased )
- it is most expensive
ROM
- it referred to read only memory
- it provides permanent storage
- ROM in a PC contains a basic set of instructions,
called the basic input output system (BIOS). the
PC uses the BIOS to start up the OS
- data can be read from ROM , but can’t be
written on ROM
Note nowadays there is erasable ROM like
EPROM (Erasable programmable ROM)
- are normally used to store information that are
the computer the computer may need frequently
for its own operations
Since RAM is expensive and volatile it is not
possible to store data permanently, hence we
need other storage devices secondary storages.
Secondary Storage (Auxiliary storages)
There are two types of secondary storages
magnetic storage and optical storage.

The commonly known magnetic storage devices are


 magnetic drum
 magnetic tape
 magnetic disk hard disk and floppy disk
Magnetic drum
- it is a metallic cylinder ranging from few inches to a
few feet s coated with a special magnetic alloy
- data is recorded as magnetic spots arranged in
binary form
- data is recorded by read /write head and one for
each truck
- magnetic drum provides random access for selecting
a truck but once the truck is identified , the
information is recorded sequentially
Magnetic tapes
- is the earliest form of secondary storages
- is a ½ or ¼ inch ribbon of plastic like material
coated with a thin layer of iron oxide material
- a device called a tape drive reads and writes
data to tape
- it has an advantage of being able to hold
enormous amount of data :for this reason it is used
to store information on the largest computer
systems
- is sequential access media
- data access time is slow compared to other
forms of storage media
Magnetic disks
Hard Disk
 it is a random access media i.e. it permits
direct addressing of data location
 consists of one or more metal plates coated
with ferric oxide
 the circular platters sealed in a metal box
with associated read/ write heads
 a data is recorded in the form of magnetic
spot on the platters
 a spot presence indicate a 1’s and its absence
indicate a 0
- it remains in the system unit it is removed
when damaged or when you want to upgrade it
with a larger capacity
- the surface of a magnetic disk is divided into
a number invisible concentric circles called
truck. The truck further subdivided into sectors
(blocks). each block or sector have a unique
address
- it is possible to read or write a block of data
at a time
Floppy Disk
- is a portable(removable ) , inexpensive
storage medium
- Consists of a thin, circular, flexible plastic
disk with a magnetic coating enclosed in a
square – shaped plastic shell, the disk jacket.
- has different sizes 5 ¼ and 3 ½
- use magnetic patterns to store items
- possible to access data and write data on a
magnetic disk any number of times.
Optical storage
Optical storage devices use the principle of light rather
than magnetism to store information.
CD-ROM (Compact Disk Read Only Memory)
- it is optical storage media
- it is possible to read from the disk but you can’t write
onto it
- is a rigid plastic disk that store s a large amount of data
through the use of laser optic technology
- its underside is coated with a very thin layer of
aluminum that reflects light
- data is written to the CD-ROM is by burning
microscopic pits into the reflective surface of the
disk with powerful laser
DVD -ROMs (digital video disc-ROM)
- is a very high capacity compact disk
- is used to store large amount of data even videos
Input devices
Input devices are
- Parts of the computer hardware that are
used to convert data or information into
electronic machine readable form
- used to gather data for the microprocessor
so that it can be processed
Examples of input devices
Keyboard, mouse, image scanner, disk
drives, microphone etc.
Keyboard
- is an input device with buttons or keys that
the user presses to enter data characters and
commands into a computer
- is most widely used input device
Keyboard contain the letters of the
alphabet, numbers and frequently used symbols
like $,%,& etc..
-The size of the keyboard depends on the
number of keys that are included in the
keyboard
- The normal PC keyboard includes 105 keys
.These keys can be divided in to five sections
1. Typing keys:- These keys work just like
conventional type writer. These keys include Alphabet
A-Z, digits 0-9, the Enter key, Space, the Back space,
the Shift key etc..
2. Numeric keypad
Is used to enter numeric data more easily when you
are working number intensive tasks. Num lock is used
to switch the light of numeric keypad to use the pad.
3. Function keys
There are 12 function keys labeled F1 to F12
arranged at the top .when any of the keys is
pressed, it performs a certain task. For instance, in
many programs F1 is pressed to display help
information.
4.Navigation keys
These are keys that are used to navigate (move)
through your document.
5.Computer keys
- These are not found on typewriter.
-They are performs different tasks when used alone
or in conjunction with other keys.
Example: Esc, Alt, Ctrl, print screen Insert etc…
Mouse
There are two types of mouse. Mechanical and
optical.
is the pointing device that is used to move the
insertion point around the screen.
is an inverted trackball device that has a
number of selection buttons associated with it
the hand moves it across a flat surface , and
its position is displayed on the screen
the buttons are used to select items and make
choices on the screen
Optical mouse use laser instead of trackball
Image scanner
Is used to input images and words directly into a
computer using a visible light or
electromagnetic radiation like laser. Is image
processing device.

Image processing is an activity of converting


visual information into a format that can be
managed with in a computer system.
Disk drives
Disk drives are devices that input data from the
storage devices for further processing

Examples:
hard disk drives and floppy disk drives

Microphone
Microphone converts sound into signals that can
be stored, manipulated and played by the
computer.
Output devices
Out put hardware consists of external devices
that used to get information (data) out of a
computer’s CPU to the computer user so that it
can be examined, analyzed, or distributed to
others
These are devices that are used to display the
processed information to the user either in Soft
copy of hard copy
Cont.
- Soft copy: is temporary output such as that
produced by a monitor (unprinted copy /
document)
-Hard copy: permanent output such as that
produced by a printer (printed copy /
document )
-Outputs convert the result of processing to a
form that is easy to understand by human
beings.
-Examples of output devices Monitor (screen),
printers, overhead projectors, (LCD), disk drives
,speakers etc..
Monitor (screen)
- Converts information gathered by then
computer into visual information.
- It looks like a television screen
- Is the most widely used output device
- It displays the output of the processing
activity for the user
- The monitor (screen) attached to the
computer gives the user feedback while the
user types a character from the keyboard
- It is used to display both text and graphics
data
LCD projector
LCD projector is an output device used for displaying
the output of the computer into a large screen, mostly
used in conferences and class room for presentations.

Printer
- Printer produces a permanent hardcopy output from the
computer by converting digital information into marks on
a paper
A printer allows the user to print out on paper a copy
of the screen or the data that is being processed by the
computer
-Printers are available in both color and black and white.
-color printers are slower and more expensive than black
and white printers
Printers can be categorized into impact and non impact
Impact printers
Impact printers are printers which are physically
strike the paper. i.e. the paper and the
characters being printed come in contact with
one another .
Impact printers has hammering parts, hence
printing is noisy.

Examples of impact printers are Dot matrix


Non-impact printers
These printers do not physically strike papers.
One advantage of these printers is that it can
print higher quality text and graphics than
impact printers.

Here the characters of the print material are


produced on the paper through a heat, chemical
or spraying process
Examples are laser printers and ink jet printers
Computer software
Software
is the invisible part of the computer system
iscollection of programs and routines that the
computer needs to perform a task
isa serious of instructions that tells the
hardware what to do
Usually software also includes documentation,
rules and operational procedures.
Software is in general classified into two
system software and application software
System software
- General purpose SW that is used to facilitate
the utilization of the hardware.
- consists of instructions or programs that are
used to manage the hardware resources of a
computer and perform required information
processing tasks
- it provides the interface between the
hardware and the user
System software includes operating systems
and system development (language software)
Programming (Knowledge Transfer)
Programming (Knowledge Transfer)
 Knowledge can be transferred from
 Human to human
 Human to computer
 Knowledge transfer from human to human is
much simpler than knowledge transfer from
human to computer. Why?
 All humans have the same internal working mechanism
 All humans have the ability to think
 Programming: is the system of transferring human
knowledge from human to computers.
 Programmer: is the one who can transfer
knowledge from human to computers.
 Program: is a human knowledge represented in
computer understandable format. Or in
professional meaning, a program is a list of
instruction that the computer must follow in
order to process data into information.
o What kinds of knowledge can be transferred from
human to computer?
o Answer: A knowledge that can be expressed
algorithmically.
 How can this knowledge be transferred?
 Answer: Using problem solving steps.
• Algorithm: is a sequence of logical steps
expressed informally that can accomplish a
given task.
• A Program: is a sequence of logical steps
expressed formally that can accomplish a given
task.
Example:
1. Given the following list, develop an algorithm
that finds the smallest number.

4 3 -1 6 -4 7
4 3 -1 6 -4 7

Step 1: 4 => Smallest Number


Step 2: 3 => 3<4 ? True: 3 =Smallest
Step 3: -1 => -1<3 ? True: -1 = Smallest
Step 4: 6 => 6<-1 ? False: No change
Step 5: -4 => -4<-1 ? True: -4 =Smallest
Step 6: 7 => 7<-4 ? False: No change

The list is exhausted and we have -4 as smallest


number
Exercise
2. Given following list, develop an algorithm
which searches a number in the list.

7 2 -1 8 10 8 Sn=11
Expressing Algorithm
There are two ways by which you can express or
describe your algorithm.
A. Using Pseudo code
An algorithm is a procedure for solving a
problem in terms of the actions to be executed
and the order in which those actions are to be
executed.
An algorithm is merely the sequence of steps
taken to solve a problem. The steps are
normally "sequence," "selection,” "iteration,"
and a case-type statement.
Pseudocode is an artificial and informal
language that helps programmers develops
algorithms. Pseudocode is a "text-based" detail
(algorithmic) design tool.

The rules of Pseudocode are reasonably


straightforward. All statements showing
"dependency" are to be indented. These include
while, do, for, if, switch. Examples below will
illustrate this notion.
Pseudo-code is an informal way to express the
design of a computer program or an algorithm.
The aim is to get the idea quickly and also easy
to read without details.
There are several ways of writing pseudo-code;
there are no strict rules.
let’s base the pseudo code on the few defined
conventions and carry out the exercises.
• It represents an algorithm in an English line
text.
• Some where between formal English and a
programming language.
• Must be language independent.
Examples:
1. To get the smallest number from the list for above
example.
Start
Let min=the first element
While not the end of list
Do
Compare min with the next element
If min is greater than the compared number
min=the compared number
Else
Do nothing
End Do
Display min
Stop
Examples:
2. the program computes the sum, average and
product of three numbers:

Read X, Y, Z
Compute Sum (S) as X + Y + Z
Compute Average (A) as S / 3
Compute Product (P) as X * Y * Z
Write (Display) the Sum, Average and Product
Examples:
3. a program that reads two numbers and
displays the numbers read in decreasing order:
Read A, B
If A is less than B
BIG = B
SMALL = A
else
BIG = A
SMALL = B
Write (Display) BIG, SMALL
B. Using Flowchart
Flowchart is a diagrammatic representation of
an algorithm. Flowchart is very helpful in
writing program and explaining program to
others.

Symbols Used In Flowchart Different symbols are


used for different states in flowchart,

For example: Input/output and decision making


has different symbols.
• Represent an algorithm graphically.
• Enforce programmer to put steps precisely.
• Makes an algorithm easier to convert into
program.
• More readable
• Can be easily hand traced.
• It uses standard symbols.
Flow chart symbols
Draw a flowchart to add two numbers entered by user
Draw flowchart to find the largest among three
different numbers entered by user.
Evolution of Programming Language
• Hardware programming
Called hard wiring
• Software programming
1. Machine Language
The first programming language.
 Consists of string of 0’s and 1’s.
 The one that CPU directly understand
2. Assembly Language
Consists of symbolic instructions
E.g MOVE, INT, ADD, SUB etc
It has to be converted into machine language before it is used
by the computer.
 Note:
 Machine language and assembly languages are
called low level programming language.
 The computer (CPU) can understand directly or
with little translator, so that they are very fast.
 They require less resource (Memory).
 You can get the power of the machine.
 Disadvantages:
Much closer to the machine so that you need
to know the architecture of the machine.
Difficult to learn, write and debug.
Machine dependent.
3. High level programming Languages
Consists of convenient letters, symbols or
English line text.
E.g cout<<endl;
cout<<”Hello World”;

There are more than 100 high level programming


languages.
 Structured: such as Basic, Pascal, Fortran
 Object Oriented: such as C++, Java, Smalltalk
 Windows Programming: Visual Basic, Visual C++,VB.Net
 Problem Solving Steps
1. Understanding the Problem
Specify the program objects and program users
Specify output and input requirement
Specify processing requirement
Example:
 Givenf(x) = ax2+bx+c where a, b, c are real
root and a≠0.
Input: a, b, c
Output: real roots
Process: Using Quadratic General Formula
2. Algorithm Development
Determine program logic through top-down
approach
Design details using pseudo code and/or using flow
chart
Test design with structure walk through
3. Program Coding (Program Development)
Select the appropriate programming language.
Code the program in that language following the
syntax carefully.

Use the programming language.


1. Editor (Editing): to write the source code.
2. Compiler (Compiling): to convert source code into
object code.
3. Linker (Linking): convert object file into executable
file.
4. Loader (Running): to load the program into RAM.
4. Program Testing
 Check the program to discover errors
 Run the program and debug it.
 Run the program with real-world data.
Errors
Syntax Error (Compile-time error): it occurs
when you violate the syntax.
And it is called Compiler complaints because it
is detected by the compiler.

Logical Error (Run-time error): when the


program generates unexpected result. But there
is no error in compilation. It produces wrong
output.
Debugging: is the process of finding out and
correcting program error.
5. Documentation
 Allyour works at different stages has to be
documented.
 Documentation is necessary for
 The program
 The other user
 The user
6. Maintenance
 After delivering the program to the end user,
different things might happened
 Error might be discovered
 User need(Requirements) might be changed
 The infrastructure might be changed (Hardware, Operating
system)
 In the above cases maintenance is needed
End of Chapter one

You might also like