Problem Solving I Chapter 1
Problem Solving I Chapter 1
1
Chapter Objective
After the completion of this chapter you are expected to
• Understand what computer system is, its essence, its classification
and the basic elements that comprise it and the functionality of these
elements.
• Understand the concept of algorithm, program, and their relation
with respect to problem solving
• Understand the steps involved in problem solving
• Learn about what programming language is, its essence and its
evolution.
• Learn about the tools and steps involved in program development
using C++
• Learn the different types of errors in program
2
CS221 : Computer Programming I
Overview of Computer
System
3
What is computer?
4
Characteristics of a computer
Characteristics of a computer:
– Speed
– Accuracy
– Reliability
– Diligence
– Memory and Mass Storage
– Versatility(Programmable)
5
Classification of computer
Based on their size and capacity
• Microcomputer: is a single user, compact and its memory
capacity is also low compare with others.
Palmtop, laptop, desktop
• Minicomputer: this kind of computer is multi-user .the
capacity of this computer is greater than Microcomputer.
Also known as mid-range computers,
• Mainframe computer: it is large computer and big
institution or organization has this type of computer.
• Supercomputer: very giant computer(They can occupy
anything from a few feet to hundreds of feet) and is found
only in few institution all over the world like NASA. Super
6
computer process vast amount of information.
Computer System
7
Architecture of a Computer
8
Architecture…
9
Basic Computer Hardware
Components
Five logical units in every computer:
1.Central Processing Unit (CPU)
2.Memory
3.Bus
4.Input / Output Interfaces
5.Peripheral devices
10
Basic Computer Components
11
CPU( Central Processing Unit )
CPU is the part of the computer that performs the bulk of the
data processing operations. Its main function is to execute
programs stored in the main memory by fetching the
instructions, examining them and executing them
12
Parts of CPU
Parts of CPU (3 parts)
1. Control Unit ( CU )
• it fetches, decodes and executes instructions
• it issues control signals that
control hardware components within the CPU
• it transfers data and instructions around the
system
2. Arithmetic and Logical Unit (ALU)
Performs operations such as
– subtraction, addition, (arithmetic)
– comparison (logical)
13
Parts of CPU
3. Registers
Very high speed memory units in the CPU used to store
address of instruction , data and the result of operation
Examples
• Program Counter (PC) – Points to the address of the next
instruction to be executed
• Instruction Register (IR) – Holds the instruction currently
14
being executed
Parts of CPU…
• Memory Address Register (MAR) – Stores the physical
memory address where the next piece of data will be written
or read from
• Memory Buffer Register (MBR) – Holds the contents of the
location that has been read or that is to be written
• Accumulator (A) – A Processor register which serves as both
the input and output for lots of operations. It basically stores
the intermediate results of most of the operations.
15
Memory
Memory refers to a place to store programs and data.
Categories:
• Primary Memory
• Secondary Memory
• Internal Memory
16
Primary Memory
Primary Memory:
Implemented in high speed devices located outside CPU, Costly
There are basically two sorts
• RAM - Random Access Memory
– general purpose memory
– can be used for storing currently running program and data
– Can be written to or read from
– Volatile
• ROM - Read Only Memory
– used to contain permanent data or instructions
– Replaces hardwiring(firmware)
– Non volatile
– In a PC it contains BIOS (basic
input/output systems) which contains POST (power on self test),
bootstrap loader, and BIOS drivers
17
Secondary Memory
Secondary Memory:
• Low speed memory(internal or external)
• Non-volatile
• Holds data and program not currently being executed
• Holds large data
• Less expensive
Examples Magnetic Disk, Tape, Optical Disc
18
Internal Memory
Internal Memory:
• Implemented in very high-speed devices located near
or within CPU
• Holds currently execution instructions and few data
related items
• Very expensive
• Examples: Registers, Cache
19
Memory Hierarchy
20
Buses
Buses are Parallel wires (a set of wires) which act as
a data highway for moving binary information around the
computer
•Carries instructions, data, addresses or commands
•Unidirectional or bi-directional
Three major categories
1. Address bus carries memory addresses from cpu to other
components such as RAM or input/output devices.
2. data bus used to carry data
3. control bus - carry control signals between the CPU
(central processing unit) and other devices. Carries
commands from the CPU and returns status signals from
the devices. 21
I/O Interfaces
I/O Interfaces are devices that interfaces the system to the
outside world.
22
A peripheral device
A peripheral device is typically a device that is external
to a computer and connected either wirelessly or via a
cable, although some are internal to the digital system
There are three different types of peripherals:
1. Input, used to interact with, or send data to the
computer (mouse, keyboards, etc.)
2. Output, which provides output to the user from the
computer (monitors, printers, etc.)
3. Storage, which stores data processed by the
computer (hard drives, flash drives, etc.)
23
Programming and Problem
Solving
24
Which side you are?
World
Problem, Solution and Problem Solving
27
Program versus Algorithm
28
Steps of programming
29
Steps…
30
Steps…
3. Implementation (Coding)
Each statement of the algorithm is translated into a
target programming Languages.
a) Select the appropriate programming language.
b) Code the program in that language following the
syntax carefully.
c) Use the programming language.
Editing: to write the source code.
Compiling: to convert source code into object code.
Linker(Linking): convert object file into executable file.
Loader(Running): to load the program into RAM for
execution.
31
Steps…
4. Program Testing
a) Check the program to discover errors
b) Run the program and debug it.
c) Run the program with real-world data.
32
Steps…
5. Documentation
To aid the maintenance of a program during its life
time.
Documentation may include: A problem statement, a
description of the system, i.e., program functions and
system specifications, a description of the program
that involves program flowcharts, program listing, test
data and result, and Instructions for installing and
running the program.
33
Steps…
6. Maintenance
Modify the program to meet changing requirements or
to correct any errors that show up while using it.
34
Algorithm
35
Basic Steps in Writing Algorithm
38
Pseudocode
1. Input/output
Input: GET var1, var2, … or READ var1, var2
Output: DISPLAY var1, var2, … or PRINT var1,
var2
2. Computation/Assignment
Compute var1 as Sum of X and Y or var1 X + y
ASSIGN X to Y or Y X
INCREMENT counter or counter counter + 1
DECREMENT counter or counter counter -1
40
Pseudocod Language…
3. Selection
a. Single-Selection IF
IF condition THEN
statement 1
statement2
etc
END IF
Statements 1, statement2, etc are executed if the
condition is evaluated true otherwise they are
skipped
41
Pseudocod Language…
b. Two-Way-Selection IF
IF condition THEN
statement 1
statement2
etc
ELSE
statement 1
statement2
etc
END IF
Statement 1, statement2, etc under the if are executed if
the condition is evaluated true otherwise Statement1,
statement2, etc under the else is executed
42
Pseudocod Language…
Multiple-Selection IF
IF condition THEN The first condition is evaluated. If it is true, the
statement 1
statement2 statements under that condition are executed
etc otherwise these statements are skipped and the
ELSE IF condition THEN second condition is evaluated and if it is true ,
statement 1
statement2 the statements under that condition will be
etc executed otherwise these statements are
….
skipped and so on. If none of the condition is
ELSE evaluated to true the statements under the
statement 1 “ELSE” will be executed. Only the statements
statement2
etc under one of the condition or under the ELSE
END IF will be executed.
. The ELSE IF and ELSE part is optional.
43
Pseudocod…
d. Multiple-Selection Switch
SWITCH expression TO Expression is evaluated. Depending on the
case 1: value of the expression, the control is
statement1 transferred to one of the cases or the default
statement2 case. If the value is 1, the statements under the
etc label case 1 will be executed. If it is 2, the
case 2 :
statement1 statements under case 2 will be executed and so
statement2 on. If the expression matches to none of the
etc cases, the statements under default will be
…. executed. Only the statements under one of the
default: cases or the default are executed.
statement1 The expression value and the case numbers
statement2 must be integers.
etc The default case is optional
END SWITCH
44
Pseudocod…
4. Repetition
a. While Structure
while condition is true, then do subordinate statements.
WHILE condition
statement 1
statement 2
etc.
END WHILE
45
Pseudocod…
b. Do-While Structure
DO – WHILE structure (like WHILE, but tests condition at
the end of the loop. Thus, statements in the structure will
always be executed at least once.)
DO
statement 1
statement 2
etc.
WHILE condition
46
Pseudocod…
48
Pseudocode Example2 (Selection)
49
Pseudocode Example 4 (Repetition)
50
Pseudocode Example5 (Repetition)
51
Pseudocode Example 6
1. It has been decided that a bonus of 12% of gross salary is to be given for each
employee in an organization. It was also agreed that if an employee has
worked for more than 13 years she/he is to receive an additional amount of
Birr 350.00. Write an algorithm that calculate and display the bonus and the
net salary.
2. Write an algorithm that chooses a random number between 1 and 20 and allow
the user to guess the number. The user is allowed only five trials. The algo
should display guess result (success or failure) and the number of trial.
52
Pseudocode Example 7
53
Flowchart
Flowchart is a graphical tool that diagrammatically depicts
the steps and structure of an algorithm. Most commonly used
symbol are listed below
Symbol Name/meaning Symbol Name/meaning
55
Flowchart…
Sequence
.
.
.
56
Flowchart…
SELECTION
SINGLE SELECTION IF
57
Flowchart…
58
Flowchart…
Switch F
59
Flowchart…
REPITITION
WHILE OR FOR STRUCTURE
60
Flowchart…
REPITITION
DO - WHILE STRUCTURE
s
61
Flowchart Example
62
Programming Languages
and its Evolution
63
Programming Languages and its
Classification
64
Machine Language
Advantage
⁻ Relatively simplifies the task of program
writing because programmers can easily
remember symbols
Disadvantage
⁻ still difficult to remember all symbols and
abbreviations of assembly languages.
⁻ Prone to error
67
High Level Language
High Level Language
Close to human language, using familiar notations and
words
Assembly language instructions High Level
language instructions
MOV A, 47 A = 47;
ADD A, B A = A + B;
A human readable program statements written in high
level language or assembly language that are not directly
readable by the machine is commonly referred as source
code.
The source code should be translated to machine code (or
called object code) by a translator program to be executed
by the machine.
68
Translators
Compiler or
Source Code Object Code
Assembler
69
Interpreter …
Using Compiler:
Object Code
Source Code Compiler
Execute Program
Using Interpreter:
Execute a line
Source Code Interpreter
of Program
70
High Level Language
Note:
• Programs written in machine languages are the fastest
programs
• High level programming languages can be translated in to
machine language using compilers or interpreters.
• Those high level languages which use compilers, such as
C, C++ etc,are called compiled languages. Those that use
interpreters, such as Java, are called Interpreted
languages.
• Compiled languages are generally faster and takes less
memory than interpreted languages
71
Introduction to C++
72
Origin of C++
Editor Step1
Preprocessor Step2
Syntax
Compiler Step3
Error
Library
Linker Step4
Loader Step5
Execution Step6
76
Program Errors
A mistake in a program is usually called a bug, and the process of
eliminating bugs is called debugging.
There are three kinds of program errors:
1. Syntax Errors: Violation of syntactic rules in a Programming
Language generates syntax errors.
Effect? Interpreter or Compiler finds it in Syntax Check Phase.
Program doesn't compile until all syntax errors are removed
2. Semantic or logical Errors: Doing logical mistakes causes semantic
errors in Source code. - this error occurs when we ask the computer to
do something, but mean for it to do something else.
Effect? Interpreters and Compilers can not notice them, but on
execution, they causes unexpected results.
3. Run-time Errors: Occur on program execution. Mostly caused by
invalid data entry or tries to use not existing resources or occurs
when we ask the computer to do something it can’t do.
Effect? It occurs on run time and may crash the program execution
77