Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2 Semester 1439-1440 H

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

College Of Computer Science

CSC 202
BASIC OF COMPUTER PROGRAMMING AND
ALGORITHMS
Done By: Rehab Aljaloud

CHAPTER 1
INTRODUCTION
2st Semester 1439-1440 H
What Is a Computer?
2

 Computer
 Executes statements (computations/logical decisions)
 Hardware :Physical devices of computer system
 Software: Programs that run on computers
Central Processing Unit

Control Unit

Input device Output Device

Arithmetic/Logic Unit

Memory Unit My data My Progam


What computers can do?
3

 –Office works (documents, letters, memos etc …)


 –Entertainment
 –Access information
 –Education
 –Online shopping
 –Video conferencing
 –Desktop publishing
Computer Parts -Hardware
4
Computer Parts -Hardware
5

 An input deviceis any hardware device that sends


data to the computer.
 •Without any input devices, a computer would only
be a display device and not allow users to interact
with it,
Hardware
6

 An output deviceis any piece of computer


hardware equipment used to communicate the
results of data processing carried out by a computer
to the outside world.
Computer Organization
7

 Six logical units of computer system


 Input unit
 Mouse, keyboard
 Output unit
 Printer, monitor, audio speakers
 Memory unit
 Retains input and processed information
 Arithmetic and logic unit (ALU)
 Performs calculations
 Control unit
 Supervises operation of other devices
 Secondary storage unit
 Hard drives, floppy drives
Computer System -Software
8

 Operating System
 Utility software
 Application software is computer software
designed to help the user to perform singular or
multiple related specific tasks.
 Word processor
 Spreadsheet
 Web browser
Basic Computer Operations
9

The computer performs four basic operations:


 Receive input data

 Process data

 Produce output information

 Store data,

information
and instructions.
What a computer program is?
10

 For a computer to be able to perform specific tasks


(i.e. print what grade a student got on an exam), it
must be given instructions to do the task.

 The set of instructions that tells the computer to


perform specific tasks is known as a computer
program
Computer Languages
11

 A set of
 Symbols (punctuation),
 Special words or keywords (vocabulary),
 And rules (grammar)
used to construct a program.
Evolution of Programming
12
Languages
 Languages differ in
 Size (or complexity)
 Readability
 Expressivity (or writability)
 "Level"
 closeness to instructions for the CPU
Types of Programming
13
Language
 There are three types of programming language:
 –Machine language(Low-level language)
 –Assembly language(Low-level language)
 –High-level language
 •Low level languages are closer to the language
used by a computer, while high-level languages are
closer to human languages.
Machine Language
14

 Binary-coded instructions
Address Contents
 Only languages understood
2034 10010110
by computers. 2035 11101010
 Used directly by the CPU 2036 00010010
 Lowest level language 2037 10101010
 Every program step is ultimately 2038 10010110

a machine language instruction 2039 11101010

 Almost impossible for humans to use


2040 11111111
2041 01010101
because they consist entirely of numbers.
2042 10101101
Machine Language
15

 Machine Language
169 1 160 0 153 0 128 153 0 129 153 130 153 0 131
200 208 241 96
 High level language

5 FOR I=1 TO 1000: PRINT "A";: NEXT I


Assembly Language
16

 Each CPU instruction is labeled with a mnemonic.


 Very-low level language
 Almost 1 to 1 correspondence with machine language
 Assembler: A program that translates a program written in
assembly language into an equivalent program in machine
language.
Sample Program

MUL X,10
ADD X,Y
Mnemonic Instruction STO Z,20
ADD 10010011 SUB X,Z
Click icon to add picture

Examples of Instructions in Assembly Language and Machine


17 Language
High-Level Languages
18

 Closer to natural language


 Programs written in a high-
level language need to be
translated into machine
language before they can be
executed.
 Compiler: A program that
translates a program written in
a high-level language into the
equivalent machine language.
Examples of High-Level
19
Languages
Language Primary Uses
Pascal Learning to program
C++ General purpose
FORTRAN Scientific programming
PERL Web programming, text processing
Java Web programming, application
programming
COBOL Business
The java Language
20

source
program.

Saved in File
ClassName.ja
va
An Overview Of Computer Languages
21

1-Machine language 2-Assembly 3-High-level


language language
representation Collection of binary Symbolic form of combines algebraic
numbers machine language expressions with
(I.e. symbolic names symbols taken from
are used to represent English language(ex.
operations, registers C, Pascal, FORTRAN,
& memory locations) …etc)
examples 10100001 00000000 00000000 MOV AX,A A=A+4 *c
00000101 00000100 00000000 MOV A , AX
10100011 00000000 00000000
ADD AX,
how does the Directly understood by a Assembler convert Compiler or
computer computer to machine language (interpter )convert
understand ? to machine language

Easier to use

More powerful
The Code life Cycle!!
22

Edit  compile  run


Levels of Program Development
23

1. Define the problem. Human thought


2. Plan the problem solution.  writing the
algorithm [pseudo-natural language (English,
Arabic)]
3. Code the program.  High Level
Programming Language (C, C++, Java, …)
4. Compile the program.  Machine Code
5. Run the program.
6. Test and debug the program.
1-Defining the Problem
24

 The problem must be defined in terms of:


 Input: Data to be processed.
 Output: The expected result.
 Look for nouns in the problem statement that suggest output and input.
 and processing: The statements to achieve.
 Look for verbs to suggest processing steps.

input data output data

Keyboard Processing Screen


Input and Output
25

 Inputs
 Can come from many sources, such as users, files, and
other programs
 Can take on many forms, such as text, graphics, and
sound

 Outputs
 Can also take on many forms, such as numbers, text,
graphics, sounds, or commands to other programs
Example 1
Area and Perimeter of a rectangle
26

 Input
 Length
 width
 Processing
 Area = length*width
 Perimeter = 2*( length + width)
 Output
 Area
 Perimeter
Example 2
Sum and Average of 5 numbers
Page 27

 Input
 five number X1, X2, X3, X4, X5
 Processing
 Sum = X1+X2+X3+X4+X5
 Average = Sum/5
 Output
 Sum
 Average
Example 3
Area and Perimeter of a circle
Page 28

 Input
 Radius
 PI
 Processing
 Area = PI * Radius * Radius
 Perimeter = 2 * PI * Radius
 Output
 Area
 Perimeter
2-Planning the Solution
29

 When planning, algorithms are used to outline the


solution steps using English like statements, called
pseudocode.
 Pseudocode permits you to focus on the program logic
without having to be concerned just yet about the
precise syntax of a particular programming language.
 A flowchart is useful for the graphical representation of
an algorithm.
 They are drawn using rectangles, diamonds, ovals, and
small circles.
Write a Program to Print the Sum of two
integer Numbers
30

start
1. Start the program
2. Read the first number and save in
the variable ( N1 ) Read N1
3. Read the second number and save in
the variable ( N2 ) Read N2
4. Sum the both numbers and save the
result in the variable ( Sum )
 Sum = N1 + N2 Sum = N1 + N2
5. Print the variable ( Sum )
6. End the program Print Sum

End
3-Coding the Program
31

 Coding is writing the program in a formal language


called Programming Language.
 Programming Language : A set of rules, symbols and special words used
to write statements.
 The program is written by translating the algorithm steps
into a programming language statements.
 The written program is called Source code and it is saved
in a file with “.cpp” extension. Algorithm Pseudocode

Coding Translating

Program Source Code


(The “.cpp”)
Why Coding in Programming Languages
32

 We write computer programs (i.e. a set of


instructions) in programming languages such as C,
C++, and Java.

 We use these programming languages because


they are easily understood by humans

 But then how does the computer understand the


instructions that we write?
4-Compiling Computer Programs
33

 Computers do not understand programs written in


programming languages such as C, C++ and Java
 Programs must first be converted into machine
code that the computer can run
 A Software that translates a programming
language statements into machine code is called a
compiler
Program Source code

Compiling Translating

Machine code Machine Code


Programming Language Compiler
34

 A compiler is a software that:


 Checks the correctness of the source code
according to the language rules.
 Syntax errors are raised if some rules were violated.
 Semantic Errors: Doing logical mistakes causes
semantic errors in Source code, they causes unexpected
results.
 Translates the source code into a machine code if
no errors were found.
5-Running The Program
35

 Before running..
 links the object code with the code for the missing functions to produce
an executable image (with no missing pieces) , this is called link
phase .

 If the program compiles and links correctly, a file called a.out is


produced.

 Before a program can be executed, the program must first be placed in


memory this called load phase .

 the computer, under the control of its CPU, executes the program
one instruction at a time
6-Testing and Debugging the Program
36

 Testing
 Be sure that the output of the program conforms with the input.
 There are two types of errors:
 Logical Errors: The program run but provides wrong output.
 Runtime errors: The program stop running suddenly when asking the
OS executing a non accepted statement (divide by zero, etc).
 Debugging
 Find, Understand and correct the error
37

You might also like