CS2301:Computer Programming 2
1
Introduction to Computers
Programming Languages and
Algorithms
2
In this Chapter we will learn:
1.1 Introduction
1.2 Computers: What is a Computer?
1.3 Computers: Hardware and Software?
1.4 Computer Organization
1.5 Classification of programming languages
1.6 Machine Languages, Assembly Languages and High-Level Languages
1.7 Programming Language, computer program and Computer
Programmer
1.8 Program developing life cycle?
1.9 What do we mean by algorithm?
1.10 Examples of algorithms
3
OBJECTIVES
Computer
Is a device capable of performing computations and
making logical decisions.
Another definition:
(often referred to as hardware) are controlled by
software (i.e., the instructions you write to command
the computer to perform actions and make decisions).
4
1.2 Computers: What is a Computer?
Software
◦ Set of instructions that tells the computer to perform specific tasks.
◦ Computers process data under the control of sets of instructions called
computer programs.
◦ These programs guide the computer through orderly sets of actions specified
by people called computer programmers.
◦ Software may be divided into:
◦ Operating systems e.g. (Windows 7)
◦ Application programs e.g. (Microsoft word)
1.3 Computers: Hardware and Software (cont.)
Hardware
◦ Computer hardware is the collection of physical elements
that constitutes a computer system and assist to the
input, process, storage, output ….
◦ Various devices comprising computer
Keyboard, screen, mouse, disks, memory, CD-ROM,
processing units, etc.
6
1.3 Computers: Hardware and Software?
Five logical units of computer system
◦ Input unit (Mouse, keyboard)
◦ Output unit (Printer, monitor, audio speakers)
◦ Memory unit (Retains input and processed information)
◦ Central processing unit (CPU) which consists of:
Control unit (Supervises operation of other devices)
Arithmetic and logic unit (ALU):Performs arithmetic calculations and
logic decisions.
◦ Secondary storage unit (Hard drives, floppy drives)
Page 7
1.4 Computer Organization
8
1.4 Computer Organization (Cont.)Hardware Components
9
1.4 Computer Organization (Cont.)Hardware Components
10
1.4 Computer Organization (Cont.)Hardware Components in Actions
Execution of an Instruction
Internet
◦ A collection of interconnected networks, all freely
exchanging information. Or Global network of computers.
◦ Internet has become one of the world’s premier
communication mechanisms.
World Wide Web
◦ Also called the Web, WWW, or W3
◦ Organizes Internet resources throughout the world into a
series of menu pages, or screens, that appear on your
computer.
11
1.5 The Internet and the World Wide Web
Page 12
1.6 Classification of programming languages
Three types of computer languages1- Machine language
Language of Computer. Only language computer directly understands
“Natural language” of computer Defined by hardware designBinary digit (bit): The digit 0 or 1 Binary code: A sequence of 0s and 1sByte: A sequence of eight bits.
13
1.7 Programming Languages
Early computers were programmed in machine
language.
To calculate wages= rates * hours in machine
language
100100 010001 //load
100110 010010 // multiply
100010 010011 // store
14
1.7 Programming Languages(cont)
2- Assembly language
◦ English-like abbreviations representing elementary computer
operations.
Clearer to humans
Incomprehensible to computers
Example
load basepay
add overpay
store grosspay
Assembler: Translates a program written in assembly
language in to machine language.
15
1.7 Programming Languages(cont)
Machine language Assembly Language
100100 LOAD
100010 STOR
100110 MULT
100101 ADD
100011 SUB
Example of instructions in Assembly Language and Machine Language
16
1.7 Programming Languages(cont)
3- High-level languages
Include Basic, FORTRAN, COBOL, Pascal, C, C++, C# and Java
– Similar to everyday English
– Uses common mathematical notations
The equation: wages = rate* hours can be written in java as:
Wages = rate * hours;
Another Example:
grossPay = basePay + overTimePay
Compiler: translates a program written in a high-level language
in to machine language.
17
1.7 Programming Languages(cont)
Programming:
Is a process of problem solving by using a programming language.
Programming Language:
A set of rules, symbols and special words used to write computer
program.
Computer program:
is a list of instructions, written to perform a specified task on
a computer
Syntax:
Rules that specify which statements (instructions) are legal.
Algorithm
- Step–by–step problem solving processes using informal language.
18
1.8 Programming Language, Computer Program, and Computer Programmer
Examples of Programming Languages: Fortran COBOL Pascal Ada Basic C C++ Objective-C Visual Basic Visual C# PHP Python JavaScript Java
19
1.8 Programming Language, Computer Program, and Computer Programmer
A programmer, computer programmer, developer, coder, or software engineer: is a person who design, writes and maintains computer software.
20
Computer Programmer
21
1.9 What is program developing life cycle?
step1: Analyze the problem.
- Outline the problem and its requirements.
- Design steps (algorithm) to solve the problem.
Step 2: Implement the algorithm(coding).
- Implement the algorithm code.
- Verify that the algorithm works.
Step 3: Maintenance (when errors occurs)
- Use and Modify the program if the problem domain changes.
22
1.9 What is program developing life cycle?
Understand the overall problem.
Understand problem requirements.
- Does program require user interaction?
Example: When you calculate the area of a rectangle What do we require
from the user?
Enter the length of the rectangle, width of the rectangle
- Does program manipulate data?
Example: Equation calculating the area of a rectangle
Area= length * Width
- What is output?
Note
If the problem is complex, divide into sub problems.
Analyze each sub problem as above.
23
How Problem Analysis could be done
1.9.1 Defining the Problem The problem must be defined in terms of:
◦ Input: raw data to be processed.
◦ Output: production of useful information.
◦ processing: converting or transforming data into useful
outputs.
Keyboard ScreenProcessing
input data output data
1.9.2 Input and Output
Inputs
◦ Can come from many sources, such as users, files, and
other programs
◦ Can take on many forms, such as text, numbers,
graphics, and sound
Outputs
◦ Can also take on many forms, such as numbers, text,
graphics, sounds, or commands to other programs
1.9.3 The Problem Analysis-Coding-Execution Cycle
Stages of programming
1.9.3 The Problem Analysis-Coding-Execution Cycle
28
Typical Java development environment.
1.10.1 The Algorithm
Methods for representing algorithms
Pseudo code Flow Chart
Note:Before use any two methods, you must specify the input,
processing, and output
Pseudo Code◦ An informal language similar to English
Is not considered programming language.
Helps programmers develop algorithms
Does not run on computers
Should contain input, output and calculation actions
Can be easily converted for different programming languages
such as Pascal, Java, C++.
1.10.2 The Algorithm- Pseudo Code
Example 1: Using pseudo code ,write suitable algorithm that
calculate the area and perimeter of rectangle and print
them.
Area=length * width
Perimeter=2(length * width)
◦ Prompt the user
◦ Input Length and width
◦ Calculate Area = length*width
◦ Calculate Perimeter = 2*( length + width)
◦ Output Area
◦ Output Perimeter
◦ End
Example 2: Using pseudo code write suitable algorithm that calculate and output Sum and Average of 5 numbers
Input five number x1, x2, x3, x4, x5
Calculate Sum = x1+x2+x3+x4+x5
Calculate Average = Sum/5
Output Sum
Output Average
end
Input grade Check if (grade > 60) then Print "passed" Else Print "failed“ End
33
Example 3: Using pseudo code ,write suitable algorithm: If the student's degree is greater than or equal 60, print “pass” else print “fail”.
Example4: - Using Pseudo code, Write suitable algorithm: Calculate and print the average of temperature
degrees for the following readings T1, T2, T3.
34
Answer:• Input T1, T2, T3• Calculate avg= (T1 + T2 + T3)/3• Output avg • End
Flow chart
Is a graphical way that explain the steps to solve
problem and how they relate to each other
(it is a symbolic representation of the algorithm).
35
1.10.3 The Algorithm-Flow chart
Flow chartStart or Stop Program
Input or Output
Process, Calculate, Storage
Arrows determine the direction of the program's progress
Used to express logical operations or operations report(Condition)
Connection
37
Condition Branching
Yes NoCompare
yesNo
38
Condition Rotation
No
Yes
)1(
No
Yes
)2(
Sum = T1+T2+T3
- Example 5: - Using flow chart, Write suitable algorithm: - Calculate and print the average of temperature degrees for the following readings
T1, T2, T3.
start
Input T1, T2, T3
Avg = sum/3
Print avg
stop
Input : T1, T2, T3
claculation: Sum = T1+T2+T3Avg = sum/3
Output:Print Avg
Sum= T1+T2+T3
Example 6: - Using pseudo code and flow chart to
write suitable algorithm: - Calculate the average for temperature
for the following readings T1, T2, T3. - IF the average is greater than 15 print
the average, else print the weather is cold.
start
inputT1, T2, T3
Avg = sum/3
Print avg
avg > 15
Print “cold”
Stop
StartInput : T1,T2,T3Process and outputSum = T1+T2+T3
Avg = sum/3
If avg > 15 then
Print avg
Else
Print “cold” End if end
YesNo
Example7: Using flow chart to write an
algorithm. Enter number (X) and find the value (F), as shown in the following equations:
F= { x + 1 x >=0 x-1 x< 0
Flow chart start
Input x
Print F , x
X <= 0
End
YesNo
F= x+1 F = x-1
Example 8:Using pseudo code and flow chart to
write an algorithm. Enter number (X) and then find and print the value (Y), as shown in the following equation:
Y=(X-2)/X Note: (if X=0 enter value of X again)
Pseudo code input: X process: if x=0 Re-enter the new value of x because you can not divide by 0 Y=(X-2)/X output: Y
start
stop
Input X
Y=(X-2)/X
Print Y
If X=0 yes
No
Flow Chart
Discuss Using pseudo code and flow chart to write an algorithm that :
Accept 3 readings of temperature degrees and find the average then
check the average
if avg less than 15 : print the “cool air”
if avg between 15 and 25: Print “moderate air”
And if the avg is not verify the previous conditions: print “hot air”
Home Work (1)1. Using pseudo code and flow chart to write an algorithm that
calculate the area of a rectangle print it.Note: area of the rectangle = length × width.
2. Using pseudo code and flow chart to write an algorithm that find the area of a circle which its radius (R) and print it.Note: The area of a circle is equal to π * R2, where π is a constant value of approximately 3.14.
3. Using the methods (pseudo code and flow chart), we need to find and print the vicinity of the football stadium, and determine
4. whether if vicinity is greater than or equal to 600 m then print (international stadium) , and if vicinity is less than 600 then print (local stadium ),
Note: The perimeter of the rectangle is equal to (length + width)×2 .
End