KEMBAR78
PF Lec1 Introduction | PDF | Computer Program | Programming
0% found this document useful (0 votes)
53 views47 pages

PF Lec1 Introduction

Here is the algorithm: Step 1: Input VALUE1, VALUE2 Step 2: If VALUE1 > VALUE2 then MAX = VALUE1 Print "VALUE1 is the largest number." Else MAX = VALUE2 Print "VALUE2 is the largest number." EndIf This algorithm takes in two values, compares them using the > relational operator, and assigns the largest value to the variable MAX. It then prints a message identifying which of the original values was largest.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views47 pages

PF Lec1 Introduction

Here is the algorithm: Step 1: Input VALUE1, VALUE2 Step 2: If VALUE1 > VALUE2 then MAX = VALUE1 Print "VALUE1 is the largest number." Else MAX = VALUE2 Print "VALUE2 is the largest number." EndIf This algorithm takes in two values, compares them using the > relational operator, and assigns the largest value to the variable MAX. It then prints a message identifying which of the original values was largest.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

PROGRAMMING FUNDAMENTALS

By: SAJID ALI


Email: sajidali.pk2010@gmail.com
Cell#: 0321-8956305
PROGRAMMING FUNDAMENTALS
SPRING 2023
 Computational Thinking
 How computer see the world
 Review binary number system
 About Computer
 Program, Programmer & Programming
 Program development process
 Pseudo-code, Algorithms & Flow chart
 Computational thinking allows us to take a complex
problem, understand what the problem is and develop
possible solutions. We can then present these solutions in
a way that a computer, a human, or both, can understand.

 Computational thinking involves looking at a problem


and working out a way a computer might be able to help
you solve it. To do this, you need to understand how a
computer processes information.
 There are four key techniques (cornerstones) to
computational thinking:

 decomposition - breaking down a complex problem or system into


smaller, more manageable parts
 pattern recognition – looking for similarities among and
within problems
 abstraction – focusing on the important information only, ignoring
irrelevant detail
 algorithms - developing a step-by-step solution to the problem, or
the rules to follow to solve the problem
 Computers use electrical signals that are on or off,
so they have to see everything as a series of binary
numbers. This data is represented as a sequence of
1s and 0s (on and off). All data that we want a
computer to process needs to be converted into this
binary format.
 Binary is a number system that only uses two digits: 1 and
0. All information that is processed by a computer is in
the form of a sequence of 1s and 0s. Therefore, all data
that we want a computer to process needs to be converted
into binary.
 The binary system is known as a „base 2‟
system. This is because:
 there are only two digits to select from (1 and 0)
 when using the binary system, data is converted
using the power of two.
Wikipedia says
 “A computer is a general purpose device that can be
programmed to carry out a finite set of arithmetic or
logical operations.”

 Device? Hardware!
 Programmed? Software!
 Arithmetic or logical? Digital!
Hardware Software
Hardware Software
Hardware Software
 Computer program  Software

 Computer programming  “Making software”

 Computer programmer = A person who


develops software
Human Language Programming Language
Naturally created, e.g., Artificially developed, e.g., C,
Chinese, Korean, English,… Basic, C#, Fortran,…
Interpreted mostly by Interpreted by human and
human computer
Ambiguities and exceptions Clear and precise syntax
in syntax and grammar
1. Understand the problem
2. Develop a solution
3. Write a program
4. Test the program
Calculate the square meters of your house.

What is the definition of square meter?


How is the square meter going to be used?
-for insurance purposes?
-to paint ?

Is the garage included?
-…
 Structure chart
 Hierarchy chart

 Each step will be a separate module.


 First produce a general algorithm (one can use
pseudocode)
 Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
 Pseudocode is an artificial and informal language
that helps programmers develop algorithms.
Pseudocode is very similar to everyday English.
 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.
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”
 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
 (Dictionary) A schematic representation of a sequence
of operations, as in a manufacturing process or
computer program.

 (Technical) A graphical representation of the sequence of


operations in an information system or program. Information
system flowcharts show how data flows from source
documents through the computer to final distribution to
users. Program flowcharts show the sequence of instructions
in a single program or subroutine. Different symbols are
used to draw each type of flowchart.
A Flowchart
 shows logic of an algorithm
 emphasizes individual steps and their
interconnections
 e.g. control flow from one action to the next
NAME SYMBOL USE IN FLOWCHART

O val D e n o t e s the b eg i nni ng or e n d of the p r o g r a m

Parallelogram D enotes an input operation

R e c t a n g le D e n o t e s a p r ocess to b e carried out


e.g. addition, subtraction, division etc.

D e n o t e s a decision (or b r anc h) to b e m a d e .


D ia m o n d
T h e p r o g r a m s houl d continue al ong o n e of
tw o routes. (e.g. I F / T H E N / E L S E )

H y b r id D enotes an output operation

F l ow line D e n o t e s the direction of logic flow in the p r o g r a m


START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
 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)
Algorithm Flowchart
 Step 1: Input Lft START

 Step 2: Lcm  Lft x


Input

 Step 3: 30 Print Lcm Lft

Lcm Lft x 30

Print
Lcm

STOP
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
 Input the width (W) and Length (L) of a rectangle

 Calculate the area (A) by multiplying L with W

 Print A
Algorithm START

 Step 1: Input W,L


Input
 Step 2: A L x W W, L

 Step 3: Print A
A  L xW

Print
A

STOP
 Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
ax2  bx  c  0
 Hint: d = sqrt ( b2  4ac), and the roots are: x1
= (–b + d)/2a and x2 = (–b – d)/2a
Pseudocode:
 Input the coefficients (a, b, c) of the
quadratic equation
 Calculate d

 Calculate x1

 Calculate x2

 Print x1 and x2
START

Algorithm:
Input
 Step 1: Input a, b, c a, b, c
 Step 2: d  sqrt ( bb  4 a  c)
d  sqrt(b x b – 4 x a x c)
 Step 3: x1  (–b + d) / (2 x a)
 Step 4: x2  (–b – d) / (2 x a) x1 (–b + d) / (2 x a)
 Step 5: Print x1, x2
X2  (–b – d) / (2 x a)

Print
x1 ,x2

STOP
 The expression A>B is a logical expression
 it describes a condition we want to test
 if A>B is true (if A is greater than B) we take the
action on left
 print the value of A
 if A>B is false (if A is not greater than B) we take
the action on right
 print the value of B
Y N
is
A>B

Print Print
A B
 The structure is as follows
If conditionthen
true alternative
else
false alternative
endif
 The algorithm for the flowchart is as follows:
If A>B then print
A
else
Y N
print B is
A>B
endif
Print Print
A B
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
 Write an algorithm that reads two values, determines the
largest value and prints the largest value with an identifying
message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
START

Input
VALUE1,VALUE2

Y is N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”,
MAX

STOP
 One of the alternatives within an IF–THEN–
ELSE statement
 may involve further IF–THEN–ELSE statement
 Writing and editing programs
 Text editor
 Source file

 Compiling programs
 Compiler:
preprocessor/translator

 Linking programs
 The linker assembles all
of functions (source and
system‟s) into final
executable program.
 Getting the program into memory is the function
of an operating system (OS) program known as
the loader.
 Specification errors
 When the problem definition is either incorrectly stated
or misinterpreted.
 They should be caught during blackbox testing.

 Code errors
 Compiler error message

 Logic errors
 They can be corrected only by thorough whitebox
testing.
In this lecture …
 Computation thinking
 Binary numbers
 How can we differentiate Program, Programmer &
Programming?
 What is program development process?
 What is the role of Pseudo-code, Algorithms & Flowchart?
 How to translate Pseudo-Code into Algorithm
 How to translate Algorithm into Flow char
 Some example

ANY QUERY ?
Review
 Binary to Decimal
 Decimal to Binary

 Octal, Hex-Decimal

 Addition & Subtraction

 1‟s & 2‟s Complements


Write down algorithm and draw the flowchart for
following question
 Q1. Find the average of two numbers

 Q2. Find the sum of 5 numbers

 Q3. Print Hello World 10 times

 Q4. To log in to Facebook account

 Q5. Reads three numbers and prints the value of


the largest number.
 PROBLEMS# (32-34) from “A structures
approach using C++”

You might also like