0% found this document useful (0 votes)
35 views

ITC Lecture 10

The document discusses key concepts in programming including algorithms, flowcharts, pseudocode, programming languages, and basic programming structures like variables, loops, decisions, and functions. It provides examples of pseudocode and flowcharts to illustrate algorithms for tasks like calculating age from year of birth, converting feet to centimeters, and finding the largest of two numbers. Key points covered include using variables to store and manipulate data, the common features of programming languages, and visual representations of program logic using flowcharts and pseudocode.

Uploaded by

Muhammad Taimoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

ITC Lecture 10

The document discusses key concepts in programming including algorithms, flowcharts, pseudocode, programming languages, and basic programming structures like variables, loops, decisions, and functions. It provides examples of pseudocode and flowcharts to illustrate algorithms for tasks like calculating age from year of birth, converting feet to centimeters, and finding the largest of two numbers. Key points covered include using variables to store and manipulate data, the common features of programming languages, and visual representations of program logic using flowcharts and pseudocode.

Uploaded by

Muhammad Taimoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

 Series of instructions to a computer to

accomplish a task
 Instructions must be written in a way the
computer can understand
 Programming languages are used to write
programs
 Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
 Algorithm: A detailed description of the exact
methods used for solving a particular problem.
 To develop the algorithm, the programmer needs
to ask:
▪ What data has to be fed into the computer?
▪ What information do I want to get out of the computer?
▪ Logic: Planning the processing of the program. It
contains the instructions that cause the input data to be
turned into the desired output data.
 A step-by-step program plan is created
during the problem solving phase.
 The two major notations for planning
detailed algorithms:
 Pseudocode: A verbal shorthand method that
closely resembles a programming language, but
does not have to follow a rigid syntax structure.
 Flowchart: Series of visual symbols representing
the logical flow of a program
 A flow chart is a graphical or symbolic
representation of a process
 emphasizes individual steps and their
interconnections
 A flowchart must have a start and stop
 A steps in a flowchart must connect. Can’t leave a
step “hanging” with no connection.
e.g. control flow from one action to the next
General Used 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


 START/END

 INPUT/OUTPUT

 PROCESS

 DECISION
 Task: add two numbers
 Pseudo code:
 Start
 Get two numbers a,b
 Add them a+b
 Print the answer
 End
 The pseudocode from the previous example
would look like this as a flowchart:

Start
Print answer
Get 2 numbers
End
Add them
 START/END  INPUT/OUTPUT
 Used at the beginning  Shows when
and end of each information/data comes
flowchart. into a program or is printed
out.
 PROCESS DECISION
 Used to show Used to show that the
calculations, storing of program must decide whether
data in variables, and something (usually a
other “processes” that comparison between
take place within a numbers) is true or false. YES
program. and NO (or T/F) branches are
usually shown.
Y

N
Write an algorithm to determine the age of a
person from the year born and if the age is
greater than 50 then print ‘OLD’ at output.
 Pseudocode:
 Start
 Get year born
 Calculate age
 Print age
 If age > 50 print OLD
 End
Start
 Flowchart
 Start
Get yr
 Get year born
 Calculate age Calc age
 Print age
 If age > 50 print OLD Print age
 End
OLD Y Age>50?

N
End
 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)
Flowchart
 Detailed Algorithm
 Step 1: Input Lft START

 Step 2: Lcm  Lft x 30 Input


Lft
 Step 3: Print Lcm
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
Detailed 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
ax 2  bx  c  0
 Hint: d = sqrt ( b  4ac ), and the roots are: x1 =
2

(–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

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

X2  (–b – d) / (2 x a)

Print
x1 ,x2

STOP
 All programming languages have certain features in common.
For example:
 Variables
 Commands/Syntax (the way commands are structured)
 Loops
 Decisions
 Functions
 Each programming language has a different set of rules about
these features.
 Variables are part of almost every program.
 A variable is a “place to put data” and is usually
represented by a letter or a word. (Think of a
variable as a container with a label on it.)
 Variable names cannot contain spaces.
 Some programming languages have very
specific limits on variable names.
 Usually there are several ways to put
information into a variable.
 The most common way is to use the equal sign
(=).
 X = Y + 7 means take the value of Y, add 7, and
put it into X.
 COUNT=COUNT + 2 means take the current
value of COUNT, add 2 to it, and make it the new
value of COUNT.
 Sometimes you must specify the type of data
that will be placed in a variable.
 Here are some examples of data types:
 Numeric (numbers of all kinds)
 String (text, “strings of letters”)
 Integer (whole numbers)
 Long (large numbers)
 Boolean (true/false)
 Variables may be classified as global or local.
 A global variable is one that can be shared by all
parts of a program, including any functions or
sub-programs.
 A local variable is one that is used only within a
certain part of the program, for example, only in
one function or sub-program.
 A loop is a repetition of all or part of the
commands in a program.
 A loop often has a counter (a variable) and
continues to repeat a specified number of
times.
 A loop may also continue until a certain
condition is met (e.g., until the end of a file or
until a number reaches a set limit)
 You saw a flowchart symbol for decisions.
 A program often needs to decide whether
something is true or false in order to see which
way to continue.
 Programs often use IF (or IF THEN or IF THEN
ELSE) statements to show a decision.
 An IF statement always has a condition to check,
often a comparison between a variable and a
number.
 The IF statement also must specify what to do if
the condition/comparison is true.
 These instructions (for “true”) may come after
the word THEN, or they may simply be listed.
 In an IF THEN statement, when the condition is
false, the program simply ignores the THEN
commands and continues to the next line.
 In an IF THEN ELSE statement, commands are
given for both the true and false conditions.
 The structure is as follows

If condition then
true alternative
else
false alternative
endif
Y N
is
A>B

Print A Print B
 The algorithm for the flowchart is as follows:

If A>B then
print A
else
print B
endif
Flow Chart to find largest of two numbers:

Start

Read A, B

Yes No
Is A > B

Print A Print B

End
 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
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

You might also like