2. Computer Programming
What is Computer Programming?
Computer programming is a way of giving computers instructions about
what they should do next. These instructions are known as code, and
computer programmers write code to solve problems or perform a task.
ALGORITHM
S
WHAT IS AN ALGORITHM?
3. • An algorithm is a set of instructions designed to perform a specific
task.
• An algorithm is a step by step procedure to solve logical and
mathematical problems.
• A recipe is a good example of an algorithm because it says what must be
done, step by step. It takes inputs (ingredients) and produces an output
(the completed dish).
WHAT IS AN ALGORITHM?
ALGORITHM
S
WHAT IS AN ALGORITHM?
4. • Example: Given 𝑚 ≥ 1 and two positive numbers each containing 𝑚
digits, 𝑎𝑚−1𝑎𝑚−2 ⋯ 𝑎0 and 𝑏𝑚−1𝑏𝑚−2 ⋯ 𝑏0, add the two numbers.
ALGORITHMS WHAT IS AN ALGORITHM?
ENGR. ARIFA QAZI CS1210 – INTRODUCTION TO COMPUTING 4
Step 1: Set the value of 𝑐𝑎𝑟𝑟𝑦 to 0
Step 2: Set the value of 𝑖 to 0
Step 3: While the value of 𝑖 is less than or equal to 𝑚 − 1, repeat the instructions
in Steps 4 through 6
Step 4: Add the two digits 𝑎𝑖 and 𝑏𝑖 to the current value of 𝑐𝑎𝑟𝑟𝑦 to get 𝑐𝑖
Step 5: If 𝑐𝑖 ≥ 10, then reset 𝑐𝑗 to (𝑐𝑗 − 10) and reset the value of 𝑐𝑎𝑟𝑟𝑦 to 1;
otherwise, set the new value of 𝑐𝑎𝑟𝑟𝑦 to 0
Step 6: Add 1 to 𝑖, effectively moving one column to the left
Step 7: Set 𝑐𝑚 to the value of 𝑐𝑎𝑟𝑟𝑦
Step 8: Print out the final answer, 𝑐𝑚𝑐𝑚−1𝑐𝑚−2 ⋯ 𝑐0
Step 9: Stop
5. GENERAL FORM OF ALGORITHMS
• Algorithms are all around us!
ALGORITHMS GENERAL FORM OF ALGORITHMS
Step 1: Do something
Step 2: Do something
Step 3: Do something
⋮ ⋮
⋮ ⋮
Step N: Stop, you’re finished.
6. EXAMPLE
The algorithm from the back of a shampoo bottle.
• Is this a good algorithm?
• No!
• When to stop? Ambiguity!
ALGORITHMS EXAMPLE
Step 1: Wet hair
Step 2: Lather
Step 3: Rinse
Step 4: Repeat
7. EXAMPLE
An improved version of the shampoo bottle algorithm.
• One possible solution.
ALGORITHMS EXAMPLE
Step 1: Wet your hair
Step 2: Lather your hair
Step 3: Rinse your hair
Step 4: Lather your hair
Step 5: Rinse your hair
Step 6: Stop, you have finished shampooing your hair.
8. EXAMPLE
The algorithm to make a pie.
• Is this a good algorithm?
• No!
• How to make the crust? Ambiguity! Not primitives.
• What to do after Step 4?
ALGORITHMS EXAMPLE
Step 1: Make the crust
Step 2: Make the cherry filling
Step 3: Pour the filling into the crust
Step 4: Bake at 350 F for 45 minutes
9. EXAMPLE
The algorithm to display the 100th prime number.
• Is this a good algorithm?
• No!
• Not doable! The list of prime numbers is unending.
ALGORITHMS EXAMPLE
Step 1: Generate a list 𝐿 of all the prime numbers: 𝐿1, 𝐿2, 𝐿3, 𝐿4, ⋯
Step 2: Sort the list 𝐿 into ascending order.
Step 3: Print the 100th element in the list, 𝐿100
Step 4: Stop
10. PSEUDOCODE
• English-language constructs designed to resemble statements in
programming but do not actually run on a computer.
• Pseudo – Not real, not genuine, pretended.
• Notation resembling simplified programming language.
• Not actually a programming language but pretending to be a programming
language.
• Simple, highly readable, and has virtually no grammatical rules.
• Sometimes jokingly referred to as “a programming language without the
details.”
• Informal design notation used to solely express algorithms.
ALGORITHMS PSEUDOCODE
11. • Natural language is in the form of paragraphs and may lead to
ambiguities.
• High-level language is code which is difficult for even programmers to
understand.
• Pseudocode offers a compromise between the two.
• Not a formal language with rigidly standard syntactic and semantic
rules and regulations.
• Benefits
• Makes it easy to visualize the algorithm.
• Allows easy translation of the algorithm into high-level language because of
closeness in resemblance to such languages.
ALGORITHMS TYPES OF OPERATIONS
CS1210 – INTRODUCTION TO COMPUTING 11
12. FLOWCHART
• The diagrammatic representation of an algorithm.
• A diagram that represents a workflow or process.
• A picture of the separate steps of a process in a sequential order.
• Flowcharts use rectangles, ovals, diamonds and potentially numerous
other shapes to define the type of step, along with connecting arrows
to define flow and sequence.
• Flowcharts can be used to
• Demonstrate the way code is organized.
• Visualize the execution of code within a program.
• Show the structure of a website or application.
• Understand how users navigate a website or program.
ALGORITHMS FLOWCHART
13. ALGORITHMS FLOWCHART
SYMBOL NAME FUNCTION
Start/End
An oval (or ellipse) represents the start and end points of a flowchart.
Start written within the shape indicates the starting point, and
End/Stop/Exit indicates the ending point of the flowchart.
Arrows
Arrows are used to represent the workflow path. They connect the
shapes in a flow chart to show the relationship between them and the
flow or sequence of the problem-solving process.
Input/
Output
A parallelogram is used to read data (input) or print data (output).
Process
A rectangle is used to show the processing that takes place in the
flowchart.
Decision
A diamond with two branches is used to show the decision-making
step in a flowchart. A Yes/No question is specified in the diamond and
the next step in the sequence is based on the answer to that question
along the arrow labeled “Yes” or “No”.
ENGR. ARIFA QAZI CS1210 – INTRODUCTION TO COMPUTING 13
14. • As with algorithms, the three basic operations (sequential,
conditional, and iterative) can be represented using flowcharts as
well.
ALGORITHMS FLOWCHART
ENGR. ARIFA QAZI CS1210 – INTRODUCTION TO COMPUTING 14
15. EXAMPLE
Find the sum of two numbers A and B
ALGORITHMS EXAMPLE
Start
↓
A = 845
↓
B = 247
SUM OF 845 AND 247 ↓
Sum = 845 + 247
↓
Sum = 1092
↓
End
16. EXAMPLE
Flowchart to calculate the profit and loss.
ALGORITHMS EXAMPLE
Start
↓
FINDING PROFIT
AND LOSS WHEN
COST PRICE IS
$325 AND SELLING
PRICE IS $458
CP = 325
SP = 458
↓
Is 458 > 325? → Profit = 458 - 325
↓
Profit = $133
↓
End
17. EXAMPLE
Portion of a flowchart
illustrating the discount applied
on movie tickets.
ALGORITHMS EXAMPLE
18. EXAMPLE
Portion of a flowchart for
discount applicable.
ALGORITHMS EXAMPLE
ITEMS
ORDERED
DISCOUNT
RATE (%)
0 to 10 0
11 to 24 10
25 to 50 15
51 or more 20
19. EXAMPLE
Flowchart to generate the
multiplication table of a number N
ALGORITHMS EXAMPLE
Start
N = 12
Count = 1
MULTIPLICATION
TABLE OF 12
12 × 1 = 12
Count = 1 + 1 = 2
12 × 2 = 24
Count = 2 + 1 = 3
⋮
Count = 9 + 1 = 10
12 × 10 = 120
Count = 10
End
22. Computer Languages
• The programming language is the medium of communication
between human and computer systems. It is the set of
instructions written in a specific style (coding) to instruct the
computer to perform a specific task.
• Three types of Computer Languages
• The computer language is used to communicate with computers.
• Broadly the computer language can be classified into three
categories
• Low level orAssembly language, machine language, and
high-level language.
23. Computer Languages
Machine language
• Machine language or machine code is the native language directly
understood by the computer's central processing unit or CPU.
• This type of computer language is not easy to understand, as it only uses a
binary system, an element of notations containing only a series of numbers
consisting of one and zero, to produce commands.
24. Computer Languages
Assembly Level or Low-Level Language
• Assembly Level Language is a set of codes that can run directly on the
computer's processor.
• This type of language is most appropriate in writing operating systems
and maintaining desktop applications.
• It allows the programmer to perform several tasks like registry access,
memory management and clock cycle correspondence.
25. Computer Languages
High-level programming languages
• These are the machine-independent programming languages, which
are easy to write, read, edit, and understand.
• Languages like Java, .Net, Python, JavaScript, C++, C, C#, and others
(which are very popular now to develop user-end applications). These
languages come under the category of high-level programming
language.
26. Middle-level programming languages
Middle-level programming languages
• The programming languages which have features of Low Level as
well as High-Level programming languages known as "Middle Level"
programming language.
• C programming languages is the best example of Middle-Level
Programming languages as it has features of low level and high-level
programming languages both.
27. Relationship between HLP and MLP
• The High level program(HLP) is called the Source Program and the
Machine language Program translate(MLP) it into Object Program.
28. Compiler VS Interpreter
• A compiler takes entire program and converts it into object code which is
typically stored in a file. The object code is also refereed as binary code
and can be directly executed by the machine after linking. Examples of
compiled programming languages are C and C++.
• Interpreters convert code into machine code when the program is run.
Execute code line by line.
• An Interpreter directly executes instructions written in a programming or
scripting language without previously converting them to an object code
or machine code. Examples of interpreted languages are Perl, Python and
Matlab.
29. BASIS FOR COMPARISON COMPILER INTERPRETER
Input It takes an entire program at a time. It takes a single line of code or instruction
at a time.
Output It generates intermediate object code. It does not produce any intermediate object
code.
Working mechanism The compilation is done before execution. Compilation and execution take place
simultaneously.
Speed Comparatively faster Slower
Memory Memory requirement is more due to the
creation of object code.
It requires less memory as it does not
create intermediate object code.
Errors Display all errors after compilation, all at
the same time.
Displays error of each line one by one.
Error detection Difficult Easier comparatively
Pertaining Programming languages C, C++, C#, Scala, typescript uses
compiler.
PHP, Perl, Python, Ruby uses an
interpreter.