Algo Notes S4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 44

1

TABLE OF CONTENTS

Chapter I. Introduction

I.1 Definition of algorithm

I.2 Structure of an algorithm

I.3 Variables

I.4 Variable declaration

I.5 Rules for giving a valid name to a variable

I.6 Data type in algorithm

I.7 Operators in algorithm

Chapter II. Read and write functions

II.1 Read function

II.2 Write function

Chapter III. Introduction to coding

III.1 Boolean logic gates

III.2 Decimal base

III.3 Hexadecimal base

III.3.1Binary operations

III.3.2 Decimal to binary conversion

III.3.3 Hexadecimal to binary conversion

III.3.4 Binary to hexadecimal conversion

III.3.5 converting to any base

Chapter IV. Testes


2

IV.1 Structure of a test

IV.3 Conditions (if, if…else, switch)

IV.4 Nested Ifs

Chapter V. Loops

V.1 Loops (do while, until)

V.2 Iterative Loops

V.3 Loops in Loops

Chapter VI. Handle a table

VI.1 Use of tables in algorithm

VI.2 Dynamic tables

Chapter VII. Structured programming

VII. Hierarchical block

Chapter VIII. Create a program starting from an algorithm

VIII.1 Switch from the pseudo code to a defined language

Chapter IX. write a program starting from a flow chart

IX. Symbols representing the condition, actions, loop, Input/Output

IX. Difference between an algorithm and a flow chart


3
4

Chapiter I. Introduction

I.1 Definition of algorithm

Algorithm is the sequence of instructions which are involved in solving a given problem
or accomplishment of a given task.

It can be translated into a programming language in order to produce the result.

I.2 Structure of an algorithm

An algorithm is made mainly of the following parts:

The variable declaration line


The beginning of an algorithm
The instructions part
The end

Example:

Var A as Integer
Start
A←5
End

Explanations:
5

Var A as Integer is the variable declaration line

Start marks the beginning of an algorithm

A←5 the instructions part

End marks the end of an algorithm

I.3 Variables

A variable is a memory zone which is used to store data. It is characterized by a name, an


address and a data type.

- Variable name: a variable name helps to distinguish it from other variables,


- Variable address: helps to locate it in the memory,
- Variable data type: helps to know the operations allowed to be performed on it
and the size it occupies in the memory.

I.4 Variable declaration

A variable to be used must first be declared. Declare a variable; means create it by giving
it a name and a data type.

I.5 Rules for giving a valid name to a variable

- Start a variable name with a upper case letter


- Don’t use a blank space in variable name
- Instead of using a blank space in a variable name put underscore
6

- Don’t use reserved words


- Don’t use special characters

I.6 Data type in algorithm

Here are some data types used in algorithm


Name Description Size Range
Short integer Short integer 2 bytes -32768 to 32767

Integer integer 4 bytes -2147483648 to


2147483647

Float Floating point 4 bytes +/- 3.4e +/- 38 (~7


digits)
number
Double Double precision 8 bytes +/- 1.7e +/- 308 (~15
digits)
floating point
number
Boolean Boolean value. Can 1 byte true or false
take one of two
numbers: True or
False
Character character 1 byte characters

I.7 Operators in algorithm

A). Arithmetic operators

+ Addition

- Subtraction

* Multiplication

/ Division
7

↑ or ̂ Power

B). Comparison operators

> Greater than


< Less than

>= Greater than or equal to


<= Less than or equal to
= Equal to

!= Not equal to

C). Logic operators

AND operator
OR operator

D). Assignment operator

To put a value in a variable we use an assignment operator which has the following
symbol: ←
8

Chapter II. Read and write functions

II.1 Read function (Inputs)

A read function is a function which is used for inputs. It helps to receive the value entered
by a user and assign it to a variable.
9

Syntax of read function:

Read( )

Example:

Write an algorithm which receives a number entered by a user.

Answer:

Var A as Integer

Start

read(A)

End

II.2 Write function

Write function is used for Inputs; it displays the content of a variable of displays
messages.

Syntax of write function:

write( )
10

Example:

Write an algorithm which displays a value stored in a variable.

Answer:

Var B as Integer

Start

B← 5

write(“The content of the variable is: ”)

write(B)

End
11

Chapter III. Introduction to coding


III.1 Boolean logic gates

Logic gate Illustration Function Truth table

Transfer A A Out
0 0
1 1
NOT A Out
1 0
0 1
AND A.B A B Out
0 0 0
0 1 0
1 0 0
1 1 1
12

NAND A B Out
0 0 1
0 1 1
1 0 1
1 1 0
OR A B Out
A+B 0 0 0
0 1 1
1 1 1
1 1 1
NOR A B Out
0 0 1
0 1 0
1 0 0
1 1 0
XOR A B Out
0 0 0
0 1 1
1 0 1
1 1 0
XNOR A B Out
0 0 1
0 1 0
1 0 0
1 1 1
13

III.2 Decimal base

Different base

B=b numbers are 0, 1, 2, 3, …, b-1


B=2 numbers are 0, 1
B=8 numbers are 0, 1, 2, 3, …,7
B=10 numbers are 0, 1, 2, 3, …,9
B=16 numbers are 0, 1, 2, 3, …,9 , For 10, 11, 12, 13, 14, 15 symbols representing them
are respectively A, B, C, D, E, F

Decimal base

It has 10 numbers ranging from 0 to 10

B=10 numbers are 0, 1, 2, 3, …,9

III.3 Hexadecimal base

The hexadecimal base has 16 numbers ranging from 0 to F

B=16 numbers are 0, 1, 2, 3, …,9 , For 10, 11, 12, 13, 14, 15 symbols representing them
are respectively A, B, C, D, E, F

III.3.1Binary operations

Addition in a binary system

Basic operations required to be known are:

0+0=0
0+1=1
14

1+0=1
1 + 1 = 0, retain of 1
1 + 1 + 1 = 1, retain of 1

Examples :

Perform the binary sum of (11011011)2 and (1001110)2


Perform the binary sum of (11101)2, (1010)2, (1100)2

I.4.2 Subtraction

Basic operations required to be known are


0–0=0
1–0=1
0 – 1 = 1, bollow 1 in the next column
1–1=0

Examples :

Perform the binary subtraction of (1111011)2 and (101001)2,


Perform the binary subtraction of (111101)2 and (10011)2

I.4.3 Multiplication

Basic operations required to be known are:

0x0=0

0x1=0

1x0=0
15

1 x 1 =1

III.3.2 Decimal to binary conversion

To convert decimal to binary is very simple, you simply divide the decimal value by 2
and then write down the remainder, repeat this process until you cannot divide by 2
anymore, for example let's take the decimal value 157:

157 / 2=78 with a remainder of 1

78 / 2=39 with a remainder of 0

39 / 2=19 with a remainder of 1

19 / 2=9 with a remainder of 1

9 / 2=4 with a remainder of 1

4 / 2=2 with a remainder of 0

2 / 2=1 with a remainder of 0

We stop on 1 which is less than 2 <--- to convert write it first.

(157)10=(10011101)2

Binary to decimal

Write each value of the binary number times two, from right to left, write the power of
two starting from 0

1*27 + 0*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 1*20 = 157.

(10011101)2 =(157)10

III.3.3 Hexadecimal, octal to binary conversion


16

To convert an hexadecimal number or octal number into a binary one you take each
number of an hexadecimal number and convert it in a binary and put it on the format of
four bits. For the octal you put it on the format of three bits.

Examples:

1. Convert an hexadecimal number (3DE)16 to a binary number

Hexadecimal 3 is equivalent to 0011

Hexadecimal D is equivalent to 1101

Hexadecimal E is equivalent to 1110

Thus (3DE)16 is equivalent to (001111011110)2

2. Convert an octal number (345)8 to a binary number

Octal 3 is equivalent to 011

Octal 4 is equivalent to 100

Octal 5 is equivalent to 101

Thus (345)8 is equivalent to (011100101)2

III.3.4 Binary to hexadecimal and octal conversion

Since the hexadecimal number has its equivalent value on 4 bits, we group the binary
number we want to convert in four bits then we search the corresponding number. For the
octal we group three bits.

Examples:

1. Convert a binary number (001111011110)2 to an hexadecimal number

We group these bits by four from right to left as follows:

0011 equivalent to 3
17

1101 equivalent to D

1110 equivalent to E

Thus (001111011110)2 is equivalent to (3DE)16

2. Convert a binary number (011100101)2 to an octal number

We group these bits by three from right to left as follows:

011 equivalent to 3

100 equivalent to 4

101 equivalent to 5

Thus (011100101)2 is equivalent to (345)8

III.3.5 converting to any base

To convert to any base may be done by division or writing in extended notation. Division
is used when converting to a small base. Writing in an extended notation is used when
converting to a greater base.

Chapter IV. Tests

There are situations in which a set of instructions are executed in one situation and
entirely another set of instructions to be executed in a different situation. In this kind of
situations a decision control instruction (test) is used. We can define a test as a structure
which controls the flow of instructions of a program during its execution. We can also
define it as a structure which helps us to evaluate a condition.

,.IV.1 Structure of a test


18

The structure of a test is made of two main parts: the part which evaluate a condition, and
a part of one instruction or a block of instructions.

IV.3 Conditions (if, if…else, switch)

IV.3.1 If statement

Syntax

If (condition) then

Instructions

End if

The if statement is used to make a decision. The block of instructions following the if
executes if the decision is true, and the block does not execute otherwise.

Example 1:

start

If the light is green then

Go

End if

End

Example 2:

start

If the light is red then

Stop
19

End if

End

Each of these statements is conditional. If the condition is true, the instruction following
the condition go in the first example and stop in the second example are executed. In case
the condition evaluate to false nothing is done.

Example 3:

Write an algorithm which receives a number and informs the user when it is positive.

Answer:

Var a as integer

Start

Write(“enter a number”)

Read(a)

If (a>0) then

Write(“the number is positive”)

End if

End

To this question when the condition evaluates true it displays the instruction: the number
is positive but when it evaluates for false it displays nothing.

IV.3.2 If…else statement

Syntax
20

If (condition) then

Instructions

Else

Instructions

End if

End

The if…else statement is used to make a decision and gives the alternative when the
condition evaluates to false. The block of instructions following the if executes if the
decision is true, and the block after else when the condition evaluates to false.

Example 1:

start

If the light is green then

Go

else

Stop

End if

End

If the condition is true, the instruction following the condition go is executed, when it
evaluates to false the instruction stop which follows else is executes.
21

Example 2:

Write an algorithm which receives a number and informs the user whether it is positive or
negative.

Answer:

Var a as integer

Start

Write(“enter a number”)

Read(a)

If (a>0) then

Write(“the number is positive”)

else

Write(“the number is negative”)

End if

End

To this question when the condition evaluates true it displays the message the number is
positive when it evaluates to false it displays the message the number is negative.

IV.3.3 Nested Ifs

If statement may be used inside another if statement, in such case we call it a nested if.

Example:

Write an algorithm which receives student note and it displays the grade as follows:

Note form 16 and above: Grade A


22

Note 14-16 : Grade B

Note 12-14 : Grade C

Note below 12 : Grade D

Enswer:

Var Note as integer

Start

Write(“enter the note”)

Read(Note)

If (Note>=16) then

Write(“Grade A”)

Else if(Note>=14)then

Write(“Grade B”)

Else if (Note>=12)then

Write(“Grade C”)

Else

Write(“Grade D”)

End if

end

IV.3.4 Multiple choice using ‘switch’

A multiple choice using switch helps to solve the problem caused by nested if statement
in case there are many conditions to be tested. Switch receives a variable then it evaluates
it using several Case.
23

Syntax:

Menu explaining to the user how to make a choice

Switch(variable)

Case 1

Instruction

Case 2

Instruction

….

Case n

Instruction

Default

Instruction

End switch

Example:

Write an algorithm which receives note and displays the student’s grade.

16 and above: grade A

14-16: grade B

12-14: grade C

Less than 12: grade D

Enswer:

Var Note as integer

Start
24

Write(“Use number to chose the range of your note”)

Write(“enter 1 for note ranging from 16 and above”)

Write(“enter 2 for note ranging from 14 to16”)

Write(“enter 3 for note ranging from 12 to14”)

Write(“enter 4 for note less than 12”)

Write(“enter your choice now using number:”)

Read(Note)

Switch(Note)

Case 1

Write(“You have grade A”)

Case 2

Write(“You have grade B”)

Case 3

Write(“You have grade C”)

Case 4

Write(“You have grade D”)

Default

Write(“Your choice is not listed, try again”)

End switch

End

Chapter V. Loops

What is a loop?
25

A loop helps to repeat instruction or block of instructions. It assists in the algorithm


where you want to carry out an activity for a certain number of times.

V.1 Loops (do while, until)

Do while loop

The do-While loop execute its statements at least once even if the condition fails for the
first time.

Syntax

Variable=<start value>

Do while <variable><comparison operator><end value>

Instruction or block of instructions

Variable=variable+1

Loop

Example:

Write an algorithm which use do while loop and displays numbers from 1 to 10

Answer:

Var A as integer

Start
26

A=1

Do while A<=10

Write(A)

A=A+1

Loop

End

Do… loop Until

Syntax:

<Variable>=<start value>

Do

Instruction or block of instructions

Variable=variable+1

Loop until while <variable><comparison operator><end value>

Example:

Write an algorithm which use do while loop and displays numbers from 1 to 10

Answer:

Var A as integer

Start

A=1

Do
27

Write(A)

A=A+1

Loop until A>10

End

V.2 Iterative Loops

For loop

The for loop is an iterative loop, it specifies some elements about the loop in one single
line.

a. Setting a loop counter to an initial value.

b. End which determine whether its value has reached the number of repetitions
desired

The value of the loop counter will be increased each time (iteration), and segment within
the loop will be executed.

Syntax:

for (<initialize counter> to <end of repetitions desired>) do

Instruction or bloc of instructions

end for

example:

Write an algorithm which ask a user to enter a number and it displays the 10 next
numbers.

Var I,A as integer


28

Start

Write(“enter a number”)

Read(I)

For (A=1 to 10) do

I<-I+1

Write(I)

End for

End

V.3 Loops in Loops

Loops in loops refer to what we call nested loops. These are loops that are such that when
one increment by one the other continues inside the first.

Example:

Var i, j As Integer

start

For i = 1 To 9 do

For j = 1 To I do

Write(j)

End for

End for

End

Chapter VI. Handle a table

VI.1 Use of tables in algorithm (Arrays)


29

An array is a variable with elements of the same data type. To access the array elements,
we use the array index.

For example if you need to record notes of 20 student in a given course, you will have to
declare 20 variables as follows:

Var N1, N2, N3, N4, N5, N6, N7, N8, N9,N10, N11, N12, N13, N14, N15, N16, N17,
N18, N19, N20 as integer

Writing these variables and assigning values to them is hard.

We can use one variable called an array to hold all these numbers.

Declaring an array

Syntax

Var <array name> <size> as <data type>

Example:

Var N(19) as integer

This array will look as follows:

Variable i is used in a loop to move from indice 0 of the array to indice 19 perfoming
given instructions on the array

How do we assign values to an array?

Let us declare an array which will hold five numbers and assign to it 5 numbers of our
choice.

Var N(4) as integer

Start
30

N(0) <-8

N(1) <- 10

N(2) <- 12

N(3) <- 6

N(4) <- 5

End

To display the content of this array we will need a loop.

Var i as integer

For i=0 to 4 do

Write N(i)

End for

If we put together these parts we will have

Var N(4) as integer

Var i as integer

Start

N(0)<-8

N(1) <-10

N(2) <-12

N(3) <-6

N(4) <-5

For i=0 to 4 do

Write N(i)
31

End for

End

In case we are told that the user will enter numbers in array we will use another array for
input, and our algorithm will change as follows:

Var N(4) as integer

Var i as integer

Start

For i=0 to 4 do

read N(i)

End for

For i=0 to 4 do

Write N(i)

End for

End

Loop in green color helps to enter data in an array while the array in blue color helps to
display them.

Arrays dimensions
A dimension is a direction in which you can vary the specification of an array's elements.

One-dimensional array
32

Two-dimensional array

Three-dimensional array

Suppose you want to track sales amounts for every day of the present month. You might
declare a one-dimensional array with 31 elements, one for each day of the month, as the
following example shows.

Dim salesAmounts(30) As Double


33

Now suppose you want to track the same information not only for every day of a month
but also for every month of the year. You might declare a two-dimensional array with 12
rows (for the months) and 31 columns (for the days), as the following example shows.

Dim salesAmounts(11, 30) As Double

Now suppose you decide to have your array hold information for more than one year. If
you want to track sales amounts for 5 years, you could declare a three-dimensional array
with 5 layers, 12 rows, and 31 columns, as the following example shows.

Dim salesAmounts(4, 11, 30) As Double

sorting in algorithm

Sorting is the process of putting data in order; either numerically or alphabetically. It is


often necessary to arrange the elements in an array in numerical order from highest to
lowest values (descending order) or vice versa (ascending order). There are many of
different ways to sort arrays. The basic goal of each of these methods is the same: to
compare each array element to another array element and swap them if they are in the
wrong position.

Selection sort

The selection sort is a combination of searching and sorting.

During each pass, the unsorted element with the smallest (or largest) value is moved to its
proper position in the array.
The number of times the sort passes through the array is one less than the number of
items in the array. In the selection sort, the inner loop finds the next smallest (or largest)
value and the outer loop places that value into its proper location.

Let's look at our same table of elements using a selection sort for descending order.
Remember, a "pass" is defined as one full trip through the array comparing and if
necessary, swapping elements
34

While being an easy sort to program, the selection sort is one of the least
efficient. The algorithm offers no way to end the sort early, even if it begins
with an already sorted list.

// Selection Sort Function for Descending Order


void SelectionSort(apvector <int> &num)
{
int i, j, first, temp;
int numLength = num.length( );
for (i= numLength - 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
if (num[j] < num[first])
first = j;
}
temp = num[first]; // Swap smallest found with element in position i.
num[first] = num[i];
num[i] = temp;
}
return;
}

Insertion sort

The insertion sort, unlike the other sorts, passes through the array only once. The
insertion sort is commonly compared to organizing a handful of playing cards. You pick
up the random cards one at a time. As you pick up each card, you insert it into its correct
position in your hand of organized cards.

The insertion sort splits an array into two sub-arrays. The first sub-array (such as the
35

cards in your hand) is sorted and increases in size as the sort continues. The second sub-
array (such as the cards to be picked up) is unsorted, contains all the elements to yet be
inserted into the first sub-array, and decreases in size as the sort continues.

Let's look at our same example using the insertion sort for descending order.

The insertion sort maintains the two sub-arrays within the same array. At the beginning
of the sort, the first element in the first sub-array is considered the "sorted array". With
each pass through the loop, the next element in the unsorted second sub-array is placed
into its proper position in the first sorted sub-array.

The insertion sort can be very fast and efficient when used with smaller arrays.
Unfortunately, it loses this efficiency when dealing with large amounts of data.

// Insertion Sort Function for Descending Order


void InsertionSort( apvector <int> &num)
{
int i, j, key, numLength = num.length( );
for(j = 1; j < numLength; j++) // Start with 1 (not 0)
{
key = num[j];
for(i = j - 1; (i >= 0) && (num[i] < key); i--) // Smaller values move up
{
num[i+1] = num[i];
}
num[i+1] = key; //Put key into its proper location
}
return;
}

Buble sort
36

n the bubble sort, as elements are sorted they gradually "bubble" (or rise) to
their proper location in the array, like bubbles rising in a glass of soda. The
bubble sort repeatedly compares adjacent elements of an array. The first and
second elements are compared and swapped if out of order. Then the second
and third elements are compared and swapped if out of order. This sorting
process continues until the last two elements of the array are compared and
swapped if out of order.

When this first pass through the array is complete, the bubble sort returns to
elements one and two and starts the process all over again. So, when does it
stop? The bubble sort knows that it is finished when it examines the entire
array and no "swaps" are needed (thus the list is in proper order). The
bubble sort keeps track of occurring swaps by the use of a flag.

The table below follows an array of numbers before, during, and after a bubble
sort fordescending order. A "pass" is defined as one full trip through the array
comparing and if necessary, swapping, adjacent elements. Several passes have
to be made through the array before it is finally sorted.

The bubble sort is an easy algorithm to program, but it is slower than many
other sorts. With a bubble sort, it is always necessary to make one final "pass"
through the array to check to see that no swaps are made to ensure that the
process is finished. In actuality, the process is finished before this last pass is
made.
37

// Bubble Sort Function for Descending Order


void BubbleSort(apvector <int> &num)
{
int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable
int numLength = num.length( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1] > num[j]) // ascending order simply changes
to <
{
temp = num[j]; // swap elements
num[j] = num[j+1];
num[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return; //arrays are passed to functions by address; nothing is returned
}

Quick sort

The shell sort is named after its inventor D. L. Shell. Instead of comparing adjacent
elements, like the bubble sort, the shell sort repeatedly compares elements that are a certain
distance away from each other (d represents this distance). The value of d starts out as half
the input size and is halved after each pass through the array. The elements are compared
and swapped when needed. The equation d= (N + 1) / 2 is used. Notice that only integer
values are used for d since integer division is occurring.
38

Let's look at our same list of values for descending order with the shell sort.
Remember, a "pass" is defined as one full trip through the array comparing and
if necessary, swapping elements.

First Pass: d = (6 + 1) / 2 = 3. Compare 1st and 4th , 2nd and 5th, and 3rd and
6th items since they are 3 positions away from each other))
Second Pass: value for d is halved d = (3 + 1) / 2 = 2. Compare items two
places away such as 1st and 3rd ……
Third Pass: value for d is halved d = (2 + 1) / 2 = 1. Compare items one
place away such as 1st and 2nd …..
Last Pass: sort continues until d = 1 and the pass occurs without any swaps.

This sorting process, with its comparison model, is an efficient sorting


algorithm.

//Shell Sort Function for Descending Order


void ShellSort( apvector <int> &num)
{
int i, temp, flag = 1, numLength = num.length( );
int d = numLength;
while( flag || (d > 1)) // boolean flag (true when not equal to 0)
{
flag = 0; // reset flag to 0 to check for future swaps
d = (d+1) / 2;
for (i = 0; i < (numLength - d); i++)
{
if (num[i + d] > num[i])
{
temp = num[i + d]; // swap positions i+d and i
num[i + d] = num[i];
num[i] = temp;
flag = 1; // tells swap has occurred
39

}
}
}
return;
}

VI.2 Dynamics tables


It happens frequently to not in advance the number of elements to store in an array. For
instance when students come at the beginning of the year you may not know the number
of those who will be registered in advance. In such case may be one may declare an array
of as many elements as possible. With such decision there might be a misuse of the
computer memory.

In order to solve this problem in a proper way, we declare an array without specifying its
size, we fix it later by resizing the array, here we use the instruction redim.

Example: write an algorithm which receive the students notes it calculates their average
and it displays it.

Enswer:

Var Note() as integer

var nb as integer

start

write(“how many notes do you want to record”)

read (nb)

redim Note(nb-1)


40

End

Chapter VII. Structured programming

Structured programming consists of dividing a program into sub-sections.

Low level structure language

At a low level, structured programs are often composed of simple, hierarchical program
flow structures. These are sequence, selection, and repetition:

 "Sequence" refers to an ordered execution of statements.

 In "selection" one of a number of statements is executed depending on the state of the


program. This is usually expressed with keywords such
as if..then..else..endif, switch.

 In "repetition" a statement is executed until the program reaches a certain state or


operations are applied to every element of a collection. This is usually expressed with
keywords such as while, repeat, for or do..until. Often it is recommended that each
loop should only have one entry point (and in the original structural programming,
also only one exit point), and a few languages enforce this.

VII. Hierarchical block

Chapter VIII. Create a program starting from an algorithm

While making an algorithm a programmer needs not to pay attention on the elements of
the programming language, he has to pay attention to the logic of solution to the problem.
Translating an algorithm in a program using a programming language you need to pay
attention on the elements of that programming language.

VIII.1 Switch from the pseudo code to a defined language

Translating an algorithm in program produced using a C programming language


41

We know that a C program is a collection of functions. For such a simple algorithm, we


only need a single function. Since every C program must have a function main(), we can
implement the algorithm by defining main().

How do we define a function?


Defining a function in C consists of 2 parts - the header, and the body

#include<stdio.h>

Int main() header

Variable declaration;

Instruction; body

Return 0;

Chapter IX. write a program starting from a flow chart

- A flowchart is a type of diagram, that represents an algorithm or process, showing the


steps as boxes of various kinds, and their order by connecting these with arrows.

-It is pictorial representation of step by step solution of a problem.

- is a schematic representation of an algorithm or a process.

Purpose of flowchart

• An aid in developing the logic of a program.

• Verification that all possible conditions have been considered in a program.

• Provides means of communication with others about the program.

• A guide in coding the program.

• Documentation for the program.


42

To write a program starting from a flowchart you need to understand the logic given in
the flowchart and follow the notations of a programming language.

IX. Symbols representing the condition, actions, loop, Input/Output

A typical flowchart may have the following kinds of symbols:

Example:
43

A flowchart for computing factorial N (10!) where N! = (1*2*3*4*5*6*7*8*9*10). This


flowchart represents a "loop and a half".

IX. Difference between an algorithm and a flow chart

An algorithm is a precise rule (or set of rules) specifying how to solve some problem. An
algorithm lists the steps that must be followed to complete the process. Therefore
various formal methods of describing algorithms have been developed. The simplest of
these is the flowchart.

A flowchart is a diagram of the sequence of operations (pictorial representation of


algorithm) it uses different symbol such as rectangle, circle etc. Flowcharting combines
symbols and flowlines, to show figuratively the operation of an algorithm.

Exercises:

Question 1 : Write an algorithm which displays your names 60 times using a do…loop
until /5pts
44

Question 2: write an algorithm which receives a number entered by the user and it
display 5 numbers which come after it. For example is the user enter 2 it will display
3,4,5,6,7 /5pts

You might also like