Sunteți pe pagina 1din 33

Computer

Programming
Computer Programs
Software refers to programs that make the computer perform
some task.
A program is a set of instructions that tells the computer what to
do.
When you have written a program, the computer will behave
exactly as you have instructed it. It will do no more or no less
than what is contained in your specific instructions.

2
Programming Languages:
Machine Language
Assembly Language
High level Language

3
Machine Language
The fundamental language of the computer’s processor,
also called Low Level Language.
All programs are converted into machine language before
they can be executed.
Consists of combination of 0’s and 1’s that represent high
and low electrical voltage.

4
Assembly Language
A low level language that is similar to machine language.

Uses symbolic operation code to represent the machine


operation code.

5
Assembly
code

Assembler

Object code
High Level Language
Computer (programming) languages that are easier to learn.
Uses English like statements.
Examples are C ++, Visual Basic, Pascal, Fortran and …....

7
High Level Language Examples
Consider the following programs that add two numbers together:
BASIC Pascal C++ LOGO
10 I = 3 program AddIt; int main( ) to add :I :J :K
20 J = 2 var { MAKE “I :3
30 K = I + J i, j, k : integer; int i, j, k; MAKE “J :2
begin i = 3; MAKE “K :I + :J
i := 3; j = 2; end
j := 2; k = i + j;
k := i + j; return 0;
end. }

8
Interpreters and Compilers
Programmers writing in a high-level language enter the
program’s instructions into a text editor.
The files saved in this format are called text files.
A program written in a high-level language is called source
code.
The programs are translated into machine language by
interpreters or compilers.
The resulting machine language code is known as object code.

9
Interpreters
An interpreter is a program that translates the source code
of a high-level language into machine language.
Each instruction is interpreted from the programming
language as needed (line by line of code).
Every time the program is run, the interpreter must
translate each instruction again.
In order to “run” the program, the interpreter must be
loaded into the computer’s memory.

10
Compilers
A compiler is another program that translates a high-level
language into machine language.
A compiler makes the translation once so that the source
code don’t have to be translated each time the program is
run.
The source code is translated into a file called an object file.
A program called a linker is used to create an executable program.
Most modern compilers let you compile and link in a single
operation, and have an “IDE” (Integrated Development
Environment) to enter text, debug, compile, link, and run
programs.

11
Debug
Bug: An error in coding or logic that causes a program to
malfunction or to produce incorrect results.
Debug: To detect, locate, and correct logical or syntactical
errors in a program.

12
Syntactic and Semantic Errors
There are two kinds of errors:
Syntax errors occur due to grammatically incorrect statements.
Typical errors might be an illegal character in the input, a
missing operator, two operators in a row, two statements on the
same line with no intervening semicolon, unbalanced
parentheses, a misplaced reserved word, etc.

Semantic errors occur during the execution of the code. These


have to do not with how statements are constructed, but with
what they mean. Such things as incorrect variable types or sizes,
nonexistent variables, subscripts out of range, and the like, are
semantic errors.
Understand the Problem
1. Define the problem
2. Analyze the problem
3. Develop an algorithm/method of solution
4. Write a computer program corresponding
to the algorithm
5. Test and debug the program
6. Document the program (how it works and
how to use it)
Major problems in software developments …

The requirements The developers This is how the problem


This is how the
specification was understood it in is solved now
that way problem was
defined like this
solved before.

This is how the program is This, in fact, is what the


described by marketing customer wanted … ;-)
That is the program after
department
debugging
An informal definition of an algorithm is:

Algorithm: a step-by-step method for solving a


problem or doing a task.

8.16
Algorithm
 A step-by-step problem-solving procedure
 An algorithm is a sequence of unambiguous
instructions for solving a problem.
 The number of steps of an algorithm will be
countable and finite.
 It is a sequence of instructions (or set of
instructions) to make a program more readable; a
process used to answer a question.
There are two commonly used tools
to help to document program logic
(the algorithm). These are :
 Flowcharts

 Pseudo code
Flowchart
A flowchart is a type of diagram that
represents an algorithm or process, showing the
steps as boxes of various kinds and their
order by connecting these with arrows. This
diagrammatic representation can give a step-
by-step solution to a given problem.
Symbols
 Start and end symbols, represented as ovals or rounded rectangles,
usually containing the word "Start" or "End“.
Arrows, showing what's called "flow of control" in computer
science. An arrow coming from one symbol and ending at another
symbol signifies flow passes to the symbol the arrow points to.
 Processing steps, represented as rectangles. Examples: "Add 1 to
X"; "replace identified part"; "save changes" or similar.
 Input/Output, represented as a parallelogram. Examples: Get X
from the user; display X.
 Conditional (or decision), represented as a diamond.These
typically contain a Yes/No question or True/False test .
Start or end of the program

Computational steps or processing


function of a program

Input or output operation

Decision making and branching

Connector or joining of two parts of


program
Flowchart Example:
Start Terminal.
Start Program start
here

Read A Input.
Read B Enter values for
A and B

Calculate Resut
C=A*B Process

Display the
Result C Output

Stop Terminal
Stop Program end
here
What’s Pseudocode ?
Artificial and Informal language
Helps programmers to plan an algorithm
Similar to everyday English
Not an actual programming language
Pseudocode Example:
Read A, B
Calculate C = A*B
Display C
Stop
1- Simple sequential Flowchart
Example 1:
- Read X,Y, Z
- Compute Sum (S) as X + Y +Z
- Compute Average (A) as S / 3
- Compute Product (P) as
XxYxZ
- Write (Display) the Sum,
Average and Product
Example 2:
Find the sum of two numbers.
–Variables:
• A: First Number
• B: Second Number
• C: Sum (A+B)
– Algorithm:
• Step 1 – Start
• Step 2 – Input A
• Step 3 – Input B
• Step 4 – Calculate C = A + B
• Step 5 – Output C
• Step 6 – Stop
• Example 3:
Find the difference and the division of two numbers and
display the result
• Example 4:
Find the circle area and circumcenter of a circle where R
(radius) is given (exercise)
–Variables:
• R: Radius
• A: Area
•C: Circumcenter
– Algorithm:
• Step 1 – Start
• Step 2 – Input R
• Step 3 – Calculate Pi
• Step 4 – Calculate A = Pi(R)2
• Step 5 – Calculate C = 2Pi(R)
• Step 6 – Print R, A, C
• Step 7– Stop
2- Branched Flowcharts
• Example 1:
Draw a flowchart that shows the traffic light processing
(exercise)

– Algorithm:
• Step 1 – Start
• Step 2 – make a Decision (what is the color)
• Step 3 – if the color is Red then STOP
• Step 4 – if the color is Yellow then WAIT
• Step 5 – if the color is Green then PASS
• Step 6– Stop
3- Loop Flowcharts
• Example 1:
• Example 2
Average for 10 numbers

S-ar putea să vă placă și