Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

MAT-Lecture-7 - Algorithms & Flowcharts

Uploaded by

khotso.tsatsi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

MAT-Lecture-7 - Algorithms & Flowcharts

Uploaded by

khotso.tsatsi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

C5-MAT-20

MATHEMATICS
Lecture-8

Algorithms & Flowcharts


Objectives of today’s class:
What is a computer program?
Variables, constants and assigning values
What is a algorithm?
Qualities of a good algorithm
Algorithm examples
What is a flowchart and symbols used
Flowchart rules
Flowchart examples
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 e.g.
Java, C, C++, C#, , VB.NET, etc
What is a computer program?
A computer program is a sequence of instructions written using a
Computer Programming Language to perform a specified task by the
computer.
Source Program Object
Object Program
Program
Written in High Level Compiler Machine
Machine Language
Language
Language
Codes/Instructions
Codes/Instructions

Source program is a program written by a programmer by using HLL (High Level


language), which is easily readable by humans

•Object Program refers to low level code which is understandable by machine.


Object code is generated from source code after going through compiler or other
translator

•A compiler is a software that converts the source code to the object code. In other
words, we can say that it converts the high-level language to machine/binary
language. Moreover, it is necessary to perform this step to make the program
executable. This is because the computer understands only binary language.
Variables and Constants
What is a variable?
Ideally, computer programs get input data from users, other programs
process the data and return output.
• Every piece of data (input, intermediate results, final output) the
computer works with must be stored inside the computer
• A variable is a storage location in computer memory reserved for a
specific data item
– Each variable must have a name or label, called variable-name or data-
name
– It is advisable to use variable-names that indicate the kind of data item
they represent, e.g. customerName could be a variable-name to store a
customer’s name
– Variable-names must not have space in between. Suppose you want to
create a variable with the words ‘customer name’, you can combine them
and then capitalise each first letter after the first word (i.e.
customerName) or you can use an underscore (_) to combine them (i.e.
customer_name).
What is a constant?
A constant is a data item that does not change during the execution of a
program
• Constants are sometimes called literals.
• You can have both numeric constants and nonnumeric constants
• Usually, constants are declared using uppercase letters, e.g. RATE,
while variables use lowercase letters, e.g. interest rate or interest Rate

Assigning values
Storing a value in a variable or constant is called assigning a value to the
variable or constant.
• To store a value in a variable or constant, we use the assignment
operator (=).
• The value to be assigned must always be to the right, while the variable
in which the value is to be stored must always be to the left.
• The following examples illustrate assignment: firstName=“John”
RATE=0.75 Balance=10000, X=100, Y=200
What is a algorithm?
•is a step-by-step list of instructions for solving a particular problem
•It is a series of steps for achieving a specific task

Qualities of a good algorithm


•Input and output should be defined precisely.

•Each step in algorithm should be clear and unambiguous.

•Algorithm should be most effective among many different ways to solve


a problem.

•An algorithm shouldn’t have computer code. Instead, the algorithm


should be written in such a way that, it can be used in similar
programming languages.
•Write an algorithm to add two numbers entered by user.

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

Step 5: Display sum

Step 6: Stop
Write an algorithm to find the largest among three different
numbers entered by user.

Step 1: Start

Step 2: Declare variables a, b and c.

Step 3: Read variables a, b and c.


Step 4: If a>b, If a>c Display a is the largest number.
Else If b>a,If b>c Display b is the largest number.
Else Display c is the greatest number.

Step 5: Stop
Write an algorithm to find a number is even or odd:

Step 1- Start

Step 2- Read / input the number.

Step 3- if n%2==0 then number is even.

Step 4- else number is odd.

Step 5- display the output.

Step 6- Stop
Factorial of a number:
n ! = n × (n – 1) × (n – 2) ×………..3 × 2 × 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1= 6
4! = 4 * 3 * 2 * 1= 24
5! = 5 * 4 * 3 * 2 * 1= 120

Write an algorithm to find factorial of a number:


Step1: Read ‘n’ as a positive integer
Step2: If n = 0 print 1
Else
Step3: Assign an integer value variable (fact = n)
Step4: n = n – 1
Step5: fact = fact * n
Step6: if n > 1 go to step 4
Else
print fact
Write a algorithm for finding the Reverse of a number:

Step 1: Input N
Step 2: Rev = 0
Step 3: While (N != 0)
Rem = N % 10;
Rev = Rev * 10 + Rem;
N = N / 10;
Step 4: Print Rev
Excercises:

1) Write a algorithm for displaying all the even numbers


from 1 to N

2) Write a algorithm for displaying all the odd numbers from


1 to N

3) Write a algorithm for summing of “N” digit number

4) Write a algorithm for displaying numbers from 1 to 10


FLOW CHARTS

What is a flow chart?

•A flowchart is a graphical representations of steps. It was originated


from computer science as a tool for representing algorithms and
programming logic

•A flowchart is a visual representation of an algorithm


Flowchart Symbols

Symbol Symbol Name Purpose

Used at the beginning and end of the algorithm to


Start/Stop show start and end of the program.

Process Indicates processes like mathematical operations.

Input/ Output Used for denoting program inputs and outputs.

Stands for decision statements in a program,


Decision where answer is usually Yes or No.

Arrow Shows relationships between different shapes.

On-page Connects two or more parts of a flowchart, which


Connector are on the same page.

Off-page Connects two parts of a flowchart which are


Connector spread over different pages.
Flowchart Rules

1. Generally, flowcharts are drawn from top to bottom.

2. All shapes of a flowchart must be connected with an arrow. The arrow


shows the direction of flow.

3. A flowchart must always start with a terminal (START) or a process.

4. A decision symbol must have two exit points, one for YES (TRUE)
and the other one for NO (FALSE)
Examples of flow charts:
1) Design a flowchart for a program that adds 2 numbers
2) Design a flowchart to find largest of 2 numbers
3) Design a flowchart to find largest of 3 numbers
4) Design a flowchart to find sum and average of three numbers
Loops

They are repetitive statements that allow one or more statements to be


repeatedly executed until a certain condition is me. These execute certain
instructions for a number of times, say, N.

1) Every finite loop must have 3 parts- initialization,


increment/decrement and stopping condition.
– Initialization sets the starting value of the loop
– Increment/decrement increases or decreases the initial value in each
cycle of the loop (aka step size)
– Stopping condition specifies the condition for exiting from the loop
i.e. stopping the repeated loop execution.

2)If a stopping condition or increment/decrement are not specified, the


loop will run/execute endlessly, & it is called an infinite loop
While loop(Top testing)

1 while (condition)
2 # statement 1
3 # statement 2
4 # ...
5 # statement n
6 end
Do-While loop(bottom testing)
1{
2 # statement 1
3 # statement 2
4 # ...
5 # statement n
6 }
7 while (condition)
8 end
1) Design a flow chart for displaying numbers from 1 to 10
2) Design a flow chart for summing of a N digit number:
Write a flowchart for a program that performs the following
tasks:
 Allows the user to enter the name and mark for 10 students
at the keyboard and computes the average of the marks.
 The program must also print all the name of students who
got marks below 50
Thank you!

You might also like