You are on page 1of 27

CSC103 Programming

Fundamentals

Lecture 02 – Introduction to Programming


Language Levels, Compiler/Interpreter,
Algorithm, Pseudocode, Flowcharts
Programming Languages

• A program is just a sequence of instructions telling the


computer what to do
• Obviously, we need to provide these instructions in a
language that computers can understand
• We refer to this kind of a language as a programming
language
• C, C++, Python and Java are examples of programming
languages
• Every structure in a programming language has an exact
form (i.e., syntax) and a precise meaning (i.e., semantic)

2
Levels of Programming Languages

• Machine Code (lowest level)


• Code that the computer can directly execute
• Binary (0 or 1)
• Low Level Language
• Interacts with the hardware of the computer
• Assembly language
• High Level Language
• Compiled or interpreted into machine code
• C, C++, Java, Python etc

3
Programming Languages

• Strictly speaking, computer hardware can only understand a


very low-level language known as machine language
• If you want a computer to add two numbers, the instructions
that the CPU will carry out might be something like this:

Load the number from memory location 2001 into the CPU
Load the number from memory location 2002 into the CPU A Lot of
Add the two numbers in the CPU
Work!
Store the result into location 2003

4
High-Level to Low-Level
Languages
• In a high-level language like C, the addition of two
numbers can be expressed more naturally:

c=a+b Much Easier!

• But, we need a way to translate the high-level language


into a machine language that a computer can execute
• To this end, high-level language can either be compiled or
interpreted

5
Programming Languages

6
Compiling a High-Level Language

• A compiler is a complex software that takes a program


written in a high-level language and translates it into an
equivalent program in the machine language of some
computer

7
Interpreting a High-Level Language

• An interpreter is a software that analyzes and


executes the source code instruction-by-instruction
(on-the-fly) as necessary

• For example, Pyhton is an interpreted language


8
Compiling vs Interpreting

• Compiling is a static (i.e., pre-execution), one-shot


translation
• Once a program is compiled, it may be run over and over again
without further need for the compiler or the source code
• Interpreting is dynamic (i.e., happens during execution)
• The interpreter and the source code are needed every time
the program runs
• Compiled programs tend to be faster, while interpreted
ones lend themselves to a more flexible programming
environments (they can be developed and run
interactively)

9
Portability

• The translation process highlights another advantage


that high-level languages have over machine languages,
namely, portability
• A program for an Intel-based machine will not run on an
IBM-based machine since each computer type has its
own machine language
• On the other hand, a program written in a high-level
language (say, a C program) can be run on many
different kinds of computers as long as there is a
suitable compiler or interpreter
• C programs are said to be portable!

10
Algorithms

• A typical programming task can be divided into two


phases:
• Problem solving phase
• produce an ordered sequence of steps that describe solution
of problem
• this sequence of steps is called an algorithm
• Implementation phase
• implement the program in some programming language

11
Steps in Problem Solving

1. Identify or Define the problem


2. Analyze the problem in terms of inputs, outputs,
formulas, constants)
3. Design the Solution
4. Implement (program coding)
5. Evaluate

12
Analyze the Problem

❖We need to read it till we understand every detail

❖We need to dissect the problem into its component


parts (e.g., problems and sub-problems)

❖We need to remove any ambiguity, extra information

❖We need to determine our knowns and our unknowns

❖We need to be aware of any assumptions we are


making.

13
Design the Solution

• Developing the algorithm that solves the problem


• Identify alternative ways to solve the problem
• Select the best way to solve the problem from the list of
alternative solutions
• List instructions that enable you to solve the problem using
selected solution

• The algorithm is expressed as flowchart or pseudo-


code

14
Program Components
 A few basic instructions appear in every language:
 Input - Get data from the keyboard, a file, or some
other device.
 Output - Display data on the screen or send data to a
file or other device.
 Math Perform basic mathematical operations like
addition and multiplication.
 Conditional execution - Check for certain conditions
and execute the appropriate sequence of statements.
 Repetition/Looping - Perform some action
repeatedly, usually with some variation.

15
Algorithm & Pseudocode

• An algorithm is an ordered sequence of steps


that describe the solution of a problem

• A pseudocode is an artificial and informal


language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.

16
Algorithm & Pseudocode

• Example 1: Write an algorithm to determine a


student’s final grade and indicate whether it is
passing or failing. The final grade is calculated
as the average of four marks.

17
Algorithm & Pseudocode

Pseudocode:

• Input a set of 4 marks


• Calculate their average by summing and dividing
by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”

18
Algorithm & Pseudocode

Detailed Algorithm:

Step 1: Input M1,M2,M3,M4


Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif

19
Flowcharts

• Flowchart is a graph used to depict or show a step by


step solution using symbols which represent a task.

• The symbols used consist of geometrical shapes that


are connected by flow lines.

• It is an alternative to pseudocoding; whereas a


pseudocode description is verbal, a flowchart is
graphical in nature.

• A flow chart:
• shows logic of an algorithm
• emphasizes individual steps and their interconnections
• e.g. control flow from one action to the next
20
Flowchart Basic Symbols
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program

21
Flowchart - Example

22
Flowcharts

• Example 2: Write an algorithm and draw a flowchart


to convert the length in feet to centimeter.

Pseudocode

• Input the length in feet (Lft)


• Calculate the length in cm (Lcm) by multiplying Lft
with 30
• Print length in cm (Lcm)

23
Flowcharts

• Example 2: Write an algorithm and draw a flowchart


to convert the length in feet to centimeter.

Algorithm

• Step 1: Input Lft


• Step 2: Lcm  Lft x 30
• Step 3: Print Lcm

24
Flowcharts

• Example 2: Write an algorithm


and draw a flowchart to START

convert the length in feet to


centimeter.
Input
Lft

Lcm  Lft x 30

Print
Lcm

STOP

25
What is debugging?
❖ Programming errors are called bugs and the process of tracking
them down and correcting them is called debugging.
❖ Three kinds of errors can occur in a program:
1. Syntax errors
▪ A program can only be executed if it is syntactically correct; otherwise, the
process fails and returns an error message.
▪ syntax refers to the structure of a program and the rules about that
structure.
2. Runtime errors
▪ So called because the error does not appear until you run the program.
▪ These errors are also called exceptions because they usually indicate that
something exceptional (and bad) has happened.
3. Semantic errors
▪ If there is a semantic error in the program, it will run successfully, in the
sense that the computer will not generate any error messages, but it will
not do the right thing. It will do something else. Specifically, it will do what
the programmer told it to do.
▪ But the written program does not solve the original problem. The meaning
of the program (its semantics) is wrong.

26
Exercises

1. Write an algorithm and draw a flowchart that will


read the two sides of a rectangle and calculate its
area.

2. Write an algorithm and draw a flowchart that will


calculate the roots of a quadratic equation

ax 2 + bx + c = 0

3. Create an algorithm and a flowchart that will


accept/read two numbers and then display the bigger
number.

27

You might also like