KEMBAR78
Fundamentals of Programming Lecture #1.pptx
CSEg 1102 Fundamentals of
Programming
Lecture #1
2021
Computer Science & Engineering Program
The School of EE & Computing
Adama Science & Technology University
Programming and Problem
Solving
2
Which side you are?
World
Problem, Solution and Problem Solving
A problem is the state of situation that needs to be
resolved. I t is something that we are not happy or
satisfied about it.
A solution is idea or artifact that removes the
problematic situation.
Problem solving is a process to derive a
solution to a problem.
Programming - Concepts
Programming is a problem-solving activity. It is the art and
science of creating computer solution to a problem(program)
Algorithm: is a sequence of logical steps expressed informally
that solves a given problem.
A program is a list of formal instruction that the computer
must follow in order to solve a problem. It is the solution to a
problem.
5
Program versus Algorithm
• Program is written in programming language whereas
algorithm is in English like pseudo language or
flowchart.
• When an algorithm is coded using any programming
language (e.g. C++), then it is called a program.
6
Steps for problem Solving
Six steps for solving problem
1. Understand the problem
a) Specify the program objects and program
users
b) Specify output and input requirement
c) Specify processing requirement
7
Steps…
2. Plan it out beforehand (write an algorithm) to
solve the problem
a) Determine program logic through top-down
approach
b) Design details using pseudo code and/or using
flow chart
c) Test design with structure walk through
8
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.
9
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.
10
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.
11
Steps…
6. Maintenance
Modify the program to meet changing requirements or
to correct any errors that show up while using it.
12
Algorithm
An algorithm is defined as a well-ordered collection
of unambiguous and effectively computable
operations, that when executed, produces a result
and halts in a finite amount of time.
13
Basic Steps in Writing Algorithm
1. Define the problem: State the problem you are
trying to solve in clear and concise terms.
2. Analyze the problem to identify the inputs, the
outputs and the process that convert the input to
the output
3. Describe the steps needed to convert or
manipulate the inputs to produce the
outputs(Write the algorithm)
4. Test the algorithm: choose data sets and verify
that your algorithm works!
14
Basic Algorithm Constructs
All algorithm are constructed using three control
structure or construct. These are:-
Sequence structure is the construct where one
statement is executed after another
Selection structure is the construct where statements
can executed or skipped depending on whether a
condition evaluates to TRUE or FALSE
Repetition structure is the construct where
statements can be executed repeatedly until a
condition evaluates to TRUE or FALSE
15
Algorithm languages
There are two well known language that are used to
describe algorithm: Pseudocode and Flowchart
16
Pseudocode
Pseudocode consists of natural language-like
statements that precisely describe the steps of an
algorithm or program
• Statements describe actions
• Focuses on the logic of the algorithm or program
• Avoids language-specific elements
• Written at a level so that the desired
programming code can be generated almost
automatically from each statement
17
Pseudocode Language Constructs
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
18
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
19
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
20
Pseudocod Language…
Multiple-Selection IF
IF condition THEN
statement 1
statement2
etc
ELSE IF condition THEN
statement 1
statement2
etc
….
ELSE
statement 1
statement2
etc
END IF
.
21
The first condition is evaluated. If it is true, the
statements under that condition are executed
otherwise these statements are skipped and the
second condition is evaluated and if it is true ,
the statements under that condition will be
executed otherwise these statements are
skipped and so on. If none of the condition is
evaluated to true the statements under the
“ELSE” will be executed. Only the statements
under one of the condition or under the ELSE
will be executed.
The ELSE IF and ELSE part is optional.
Pseudocod…
d. Multiple-Selection Switch
SWITCH expression TO
case 1:
statement1
statement2
etc
case 2 :
statement1
statement2
etc
….
default:
statement1
statement2
etc
END SWITCH
22
Expression is evaluated. Depending on the
value of the expression, the control is
transferred to one of the cases or the default
case. If the value is 1, the statements under the
label case 1 will be executed. If it is 2, the
statements under case 2 will be executed and so
on. If the expression matches to none of the
cases, the statements under default will be
executed. Only the statements under one of the
cases or the default are executed.
The expression value and the case numbers
must be integers.
The default case is optional
Pseudocod…
4. Repetition
a. While Structure
while condition is true, then do subordinate statements.
WHILE condition
statement 1
statement 2
etc.
END WHILE
23
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
24
Pseudocod…
c. FOR Loop Structure
FOR structure (a specialized version of WHILE for repeating
execution of statements a specific number of times)
FOR counter -> i TO n STEP k
statement 1
statement 2
etc.
END FOR
The statements under the for loop will be executed for every
iteration of the counter variable starting from initial value i to
n every time the counter value incremented by k (the step
value).
25
Pseudocode Example1 (Sequence)
Write an algorithm that calculates and display the area and
perimeter of a rectangle. The width and the length given by
the user.
26
Pseudocode Example2 (Selection)
Write an algorithm that determining the monthly income
of a salesperson by using the following commission
schedule:the user,
27
Monthly Sales Income
Greater than or equal to $50,000 $375 plus 16% of sales
Less than $50,000 but greater than or equal to $40,000 $350 plus 14% of sales
Less than $40,000 but greater than or equal to $30,000 $325 plus 12% of sales
Less than $30,000 but greater than or equal to $20,000 $300 plus 9% of sales
Less than $20,000 but greater than or equal to $10,000 $250 plus 5% of sales
Less than $10,000 $200 plus 3% of sales
Pseudocode Example 4 (Repetition)
Write an algorithm that calculates class average of n students
for math's score where n is supplied by the user.
28
Pseudocode Example5 (Repetition)
Repeat the previous problem where the number of student is
not known in advance. Assume an entry less than zero for
score signifies end of entry for score(sentinel value)
29
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.
30
Pseudocode Example 7
Write an algorithm that determines the square root of a
number
31
Flowchart
Flowchart is a graphical tool that diagrammatically depicts
the steps and structure of an algorithm. Most commonly used
symbol are listed below
32
Symbol Name/meaning Symbol Name/meaning
Process – Any type of internal
operation: data transformation,
data movement, logic operation,
etc
Connector – connects sections
of the flowchart, so that the
diagram can maintain a smooth,
linear flow
Input/Output – input or output
of data
Terminal – indicates start or end
of the program or algorithm
Decision – evaluates a condition
or statement and branches
depending on whether the
evaluation is true or false
Flow lines – arrows that indicate
the direction of the progression
of he program
Flowchart…
General rules for flowcharts
• All symbols of the flowchart are connected by flow lines
(note arrows, not lines)
• Flowlines enter the top of the symbol and exit out the
bottom, except for the decision symbol, which can have
flow lines exiting from the bottom or the sides
• Flowcharts are drawn so flow generally goes from top to
bottom
• The beginning and the end of the flowchart is indicated
using the Terminal symbol
33
Flowchart…
Sequence
34
.
.
.
Flowchart…
SELECTION
SINGLE SELECTION IF
35
T
F
Flowchart…
TWO WAY SELECTION IF
36
T
F
Flowchart…
Multi Way Selection IF OR
Switch
37
T
T
T
F
F
F
Flowchart…
REPITITION
WHILE OR FOR STRUCTURE
38
T
F
Flowchart…
REPITITION
DO - WHILE STRUCTURE
39
T
s
F
Flowchart Example
Write an algorithm using flowchart for Example 1-7
40
Programming Languages
and its Evolution
41
Programming Languages and its
Classification
A Computer Programming language is a set of rules that tells the
computer what operations to do. It is a tool used by a programmer
to write a computer program.
E.g. BASIC, FORTRAN, COBOL, PASCAL, C, C++, Java,
VISUAL BASIC.
Programming has gone through an evolution, which is derived by
the need to make program writing and maintenance simpler.
There are three major levels, or types of programming languages:
1. Machine Language
2. Assembly Language
3. High Level Languages
42
Machine Language
• The first programming language.
• Consists of string of 0’s and 1’s.
• The one that CPU directly understand
Advantage
⁻ the fastest type of computer program
Disadvantage
⁻ Much closer to the machine so that you need to know the
architecture of the machine.
⁻ Very Difficult to learn, write, debug and maintain
⁻ Prone to error
⁻ Machine Dependent
43
Assembly Language
Assembly language is a symbolic representation of machine
language, in that each machine language instruction is
represented by a symbol or abbreviation in assembly
language.
A program written in assembly language should be translated
to machine language by a translator known as Assembler
before it is executed by the machine.
Machine language and assembly language together are called
Low Level Language
Assembly language instructions Machine language
instructions
MOV A, 47 1010 1111
ADD A, 26 01101011 00011010
44
Assembly…
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
45
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.
46
Translators
There are two types of translator:
1. Interpreter- is a program that converts each high-level
language statement into machine language, when needed to
be executed immediately, statement by statement.
2. Compiler: is a program that converts the entire program
(source code) of a high level language into machine code
before the computer executes the program.
47
Source Code
Compiler or
Assembler
Object Code
Interpreter …
48
Using Compiler:
Source Code Compiler
Object Code
Execute a line
of Program
Using Interpreter:
Source Code Interpreter
Execute Program
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
49
Introduction to C++
50
Origin of C++
C is a programming language developed by Dennis Ritchie of
AT&T Bell Laboratories in 1970's alongside the UNIX
operating system. It evolved from two other programming
languages, BCPL and B
• C provides a comprehensive set of features for handling a
wide variety of applications, such as systems
development and scientific computation.
• C++ is an “extension” of the C language, in that most C
programs are also C++ programs.
• C++, as opposed to C, supports “object-oriented
programming.”
• Both C & C++ are compiled languages.
51
Phases of Program Development in
C++
Programs typically go through six phases to be executed. These are:-
1. Edit: - in this phase a program is created using a text editor. (e.g.
Turbo C++ editor, notepad, DOS-Editor, etc). The result of the
edit phase is a collection of text files containing C++ source code.
C++ source files usually have a ".cpp" or ".cc" extension.
2. Preprocess: - the preprocess, compile and link phases are usually
done together, with the preprocessor running first, the compiler
next and the linker. The role of the preprocessor is to transform
the source file into an equivalent file by performing the
preprocessing instructions contained by it. These instructions
facilitate a number of features, such as:
• file inclusion
• conditional compilation
• macro substitution
• Removal of comment, and unnecessary spaces and newline
character 52
Phases…
3. Compile: - the compiler translates the C++ source code into
machine (or object) code that the computer can execute. The
type of machine code depends on the type of computer. The
result of the compile phase is a binary file containing machine
code. Object files usually have a ".o" or ".obj“
ending/extension. While object file contain machine code, they
do not contain a complete program, so they cannot be executed
by themselves.
4. Link: A linker combines the object files into a single
executable file that the computer can run. The linker may also
extract object code from libraries. Libraries are files containing
the object code of commonly used functions. The executable
file contains machine code. Executable files in a MS-DOS or
Microsoft Windows system have a ".com" or ".exe" ending.
53
Phases…
5. Load:- A loader program loads the executable file from the disk into
memory so that it can be run by the CPU
6. Execute:- Each instruction of the machine code are fetched from the
RAM one by one in to the CPU and executed by the processor.
54
Linker
Editor
Preprocessor
Compiler
Loader
Execution
Library
Syntax
Error
Step1
Step2
Step3
Step4
Step5
Step6
C++ Program
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
55

Fundamentals of Programming Lecture #1.pptx

  • 1.
    CSEg 1102 Fundamentalsof Programming Lecture #1 2021 Computer Science & Engineering Program The School of EE & Computing Adama Science & Technology University
  • 2.
  • 3.
    Which side youare? World
  • 4.
    Problem, Solution andProblem Solving A problem is the state of situation that needs to be resolved. I t is something that we are not happy or satisfied about it. A solution is idea or artifact that removes the problematic situation. Problem solving is a process to derive a solution to a problem.
  • 5.
    Programming - Concepts Programmingis a problem-solving activity. It is the art and science of creating computer solution to a problem(program) Algorithm: is a sequence of logical steps expressed informally that solves a given problem. A program is a list of formal instruction that the computer must follow in order to solve a problem. It is the solution to a problem. 5
  • 6.
    Program versus Algorithm •Program is written in programming language whereas algorithm is in English like pseudo language or flowchart. • When an algorithm is coded using any programming language (e.g. C++), then it is called a program. 6
  • 7.
    Steps for problemSolving Six steps for solving problem 1. Understand the problem a) Specify the program objects and program users b) Specify output and input requirement c) Specify processing requirement 7
  • 8.
    Steps… 2. Plan itout beforehand (write an algorithm) to solve the problem a) Determine program logic through top-down approach b) Design details using pseudo code and/or using flow chart c) Test design with structure walk through 8
  • 9.
    Steps… 3. Implementation (Coding) Eachstatement 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. 9
  • 10.
    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. 10
  • 11.
    Steps… 5. Documentation To aidthe 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. 11
  • 12.
    Steps… 6. Maintenance Modify theprogram to meet changing requirements or to correct any errors that show up while using it. 12
  • 13.
    Algorithm An algorithm isdefined as a well-ordered collection of unambiguous and effectively computable operations, that when executed, produces a result and halts in a finite amount of time. 13
  • 14.
    Basic Steps inWriting Algorithm 1. Define the problem: State the problem you are trying to solve in clear and concise terms. 2. Analyze the problem to identify the inputs, the outputs and the process that convert the input to the output 3. Describe the steps needed to convert or manipulate the inputs to produce the outputs(Write the algorithm) 4. Test the algorithm: choose data sets and verify that your algorithm works! 14
  • 15.
    Basic Algorithm Constructs Allalgorithm are constructed using three control structure or construct. These are:- Sequence structure is the construct where one statement is executed after another Selection structure is the construct where statements can executed or skipped depending on whether a condition evaluates to TRUE or FALSE Repetition structure is the construct where statements can be executed repeatedly until a condition evaluates to TRUE or FALSE 15
  • 16.
    Algorithm languages There aretwo well known language that are used to describe algorithm: Pseudocode and Flowchart 16
  • 17.
    Pseudocode Pseudocode consists ofnatural language-like statements that precisely describe the steps of an algorithm or program • Statements describe actions • Focuses on the logic of the algorithm or program • Avoids language-specific elements • Written at a level so that the desired programming code can be generated almost automatically from each statement 17
  • 18.
    Pseudocode Language Constructs 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 18
  • 19.
    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 19
  • 20.
    Pseudocod Language… b. Two-Way-SelectionIF 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 20
  • 21.
    Pseudocod Language… Multiple-Selection IF IFcondition THEN statement 1 statement2 etc ELSE IF condition THEN statement 1 statement2 etc …. ELSE statement 1 statement2 etc END IF . 21 The first condition is evaluated. If it is true, the statements under that condition are executed otherwise these statements are skipped and the second condition is evaluated and if it is true , the statements under that condition will be executed otherwise these statements are skipped and so on. If none of the condition is evaluated to true the statements under the “ELSE” will be executed. Only the statements under one of the condition or under the ELSE will be executed. The ELSE IF and ELSE part is optional.
  • 22.
    Pseudocod… d. Multiple-Selection Switch SWITCHexpression TO case 1: statement1 statement2 etc case 2 : statement1 statement2 etc …. default: statement1 statement2 etc END SWITCH 22 Expression is evaluated. Depending on the value of the expression, the control is transferred to one of the cases or the default case. If the value is 1, the statements under the label case 1 will be executed. If it is 2, the statements under case 2 will be executed and so on. If the expression matches to none of the cases, the statements under default will be executed. Only the statements under one of the cases or the default are executed. The expression value and the case numbers must be integers. The default case is optional
  • 23.
    Pseudocod… 4. Repetition a. WhileStructure while condition is true, then do subordinate statements. WHILE condition statement 1 statement 2 etc. END WHILE 23
  • 24.
    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 24
  • 25.
    Pseudocod… c. FOR LoopStructure FOR structure (a specialized version of WHILE for repeating execution of statements a specific number of times) FOR counter -> i TO n STEP k statement 1 statement 2 etc. END FOR The statements under the for loop will be executed for every iteration of the counter variable starting from initial value i to n every time the counter value incremented by k (the step value). 25
  • 26.
    Pseudocode Example1 (Sequence) Writean algorithm that calculates and display the area and perimeter of a rectangle. The width and the length given by the user. 26
  • 27.
    Pseudocode Example2 (Selection) Writean algorithm that determining the monthly income of a salesperson by using the following commission schedule:the user, 27 Monthly Sales Income Greater than or equal to $50,000 $375 plus 16% of sales Less than $50,000 but greater than or equal to $40,000 $350 plus 14% of sales Less than $40,000 but greater than or equal to $30,000 $325 plus 12% of sales Less than $30,000 but greater than or equal to $20,000 $300 plus 9% of sales Less than $20,000 but greater than or equal to $10,000 $250 plus 5% of sales Less than $10,000 $200 plus 3% of sales
  • 28.
    Pseudocode Example 4(Repetition) Write an algorithm that calculates class average of n students for math's score where n is supplied by the user. 28
  • 29.
    Pseudocode Example5 (Repetition) Repeatthe previous problem where the number of student is not known in advance. Assume an entry less than zero for score signifies end of entry for score(sentinel value) 29
  • 30.
    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. 30
  • 31.
    Pseudocode Example 7 Writean algorithm that determines the square root of a number 31
  • 32.
    Flowchart Flowchart is agraphical tool that diagrammatically depicts the steps and structure of an algorithm. Most commonly used symbol are listed below 32 Symbol Name/meaning Symbol Name/meaning Process – Any type of internal operation: data transformation, data movement, logic operation, etc Connector – connects sections of the flowchart, so that the diagram can maintain a smooth, linear flow Input/Output – input or output of data Terminal – indicates start or end of the program or algorithm Decision – evaluates a condition or statement and branches depending on whether the evaluation is true or false Flow lines – arrows that indicate the direction of the progression of he program
  • 33.
    Flowchart… General rules forflowcharts • All symbols of the flowchart are connected by flow lines (note arrows, not lines) • Flowlines enter the top of the symbol and exit out the bottom, except for the decision symbol, which can have flow lines exiting from the bottom or the sides • Flowcharts are drawn so flow generally goes from top to bottom • The beginning and the end of the flowchart is indicated using the Terminal symbol 33
  • 34.
  • 35.
  • 36.
  • 37.
    Flowchart… Multi Way SelectionIF OR Switch 37 T T T F F F
  • 38.
  • 39.
  • 40.
    Flowchart Example Write analgorithm using flowchart for Example 1-7 40
  • 41.
  • 42.
    Programming Languages andits Classification A Computer Programming language is a set of rules that tells the computer what operations to do. It is a tool used by a programmer to write a computer program. E.g. BASIC, FORTRAN, COBOL, PASCAL, C, C++, Java, VISUAL BASIC. Programming has gone through an evolution, which is derived by the need to make program writing and maintenance simpler. There are three major levels, or types of programming languages: 1. Machine Language 2. Assembly Language 3. High Level Languages 42
  • 43.
    Machine Language • Thefirst programming language. • Consists of string of 0’s and 1’s. • The one that CPU directly understand Advantage ⁻ the fastest type of computer program Disadvantage ⁻ Much closer to the machine so that you need to know the architecture of the machine. ⁻ Very Difficult to learn, write, debug and maintain ⁻ Prone to error ⁻ Machine Dependent 43
  • 44.
    Assembly Language Assembly languageis a symbolic representation of machine language, in that each machine language instruction is represented by a symbol or abbreviation in assembly language. A program written in assembly language should be translated to machine language by a translator known as Assembler before it is executed by the machine. Machine language and assembly language together are called Low Level Language Assembly language instructions Machine language instructions MOV A, 47 1010 1111 ADD A, 26 01101011 00011010 44
  • 45.
    Assembly… Advantage ⁻ Relatively simplifiesthe 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 45
  • 46.
    High Level Language HighLevel 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. 46
  • 47.
    Translators There are twotypes of translator: 1. Interpreter- is a program that converts each high-level language statement into machine language, when needed to be executed immediately, statement by statement. 2. Compiler: is a program that converts the entire program (source code) of a high level language into machine code before the computer executes the program. 47 Source Code Compiler or Assembler Object Code
  • 48.
    Interpreter … 48 Using Compiler: SourceCode Compiler Object Code Execute a line of Program Using Interpreter: Source Code Interpreter Execute Program
  • 49.
    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 49
  • 50.
  • 51.
    Origin of C++ Cis a programming language developed by Dennis Ritchie of AT&T Bell Laboratories in 1970's alongside the UNIX operating system. It evolved from two other programming languages, BCPL and B • C provides a comprehensive set of features for handling a wide variety of applications, such as systems development and scientific computation. • C++ is an “extension” of the C language, in that most C programs are also C++ programs. • C++, as opposed to C, supports “object-oriented programming.” • Both C & C++ are compiled languages. 51
  • 52.
    Phases of ProgramDevelopment in C++ Programs typically go through six phases to be executed. These are:- 1. Edit: - in this phase a program is created using a text editor. (e.g. Turbo C++ editor, notepad, DOS-Editor, etc). The result of the edit phase is a collection of text files containing C++ source code. C++ source files usually have a ".cpp" or ".cc" extension. 2. Preprocess: - the preprocess, compile and link phases are usually done together, with the preprocessor running first, the compiler next and the linker. The role of the preprocessor is to transform the source file into an equivalent file by performing the preprocessing instructions contained by it. These instructions facilitate a number of features, such as: • file inclusion • conditional compilation • macro substitution • Removal of comment, and unnecessary spaces and newline character 52
  • 53.
    Phases… 3. Compile: -the compiler translates the C++ source code into machine (or object) code that the computer can execute. The type of machine code depends on the type of computer. The result of the compile phase is a binary file containing machine code. Object files usually have a ".o" or ".obj“ ending/extension. While object file contain machine code, they do not contain a complete program, so they cannot be executed by themselves. 4. Link: A linker combines the object files into a single executable file that the computer can run. The linker may also extract object code from libraries. Libraries are files containing the object code of commonly used functions. The executable file contains machine code. Executable files in a MS-DOS or Microsoft Windows system have a ".com" or ".exe" ending. 53
  • 54.
    Phases… 5. Load:- Aloader program loads the executable file from the disk into memory so that it can be run by the CPU 6. Execute:- Each instruction of the machine code are fetched from the RAM one by one in to the CPU and executed by the processor. 54 Linker Editor Preprocessor Compiler Loader Execution Library Syntax Error Step1 Step2 Step3 Step4 Step5 Step6 C++ Program
  • 55.
    Program Errors A mistakein 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 55