Ge3151 Unit 1
Ge3151 Unit 1
GE3151 UNIT 1
I. FUNDAMENTALS OF COMPUTING:
A computer is an electronic device that can be programmed to accept data (input), process it and
generate result. A computer along with additional hardware and software together is called a computer
system.
Data: Data is defined as an un-processed collection of raw facts, suitable for communication,
interpretation or processing
Information: Information is a collection of facts that is processed to give meaningful, ordered or
structured information.
Characteristics of Computer:
Speed
Diligent
Accuracy
Reliability
Storage capability
Resource Sharing
Resource Sharing
Output Unit:
- It provides the processed data (information) to the user,
- It is understandable by the user
- Some output devices are monitor, printer and speaker.
(iii). Registers
Registers are high speed storage area within CPU
Registers store data, instructions, addresses and intermediate results of processing. So, registers
are often called as CPU’s working memory.
The data and instructions that require processing must be brought in the registers of CPU before
they can be processed.
Some of the important registers are
ACC : Accumulator stores the result of arithmetic and logical operations
IR : Instruction register contains the current instruction most recently fetched
PC : Program counter contains the address of next instruction to be executed
MAR : Memory Address register
MBR : Memory Buffer register
DR : Data Register
3. Memory Unit:
Cache Memory:
Cache memory is placed between RAM and CPU.
Frequently used instructions are stored in it.
Primary Memory:
Primary memory is the main memory of computer. It is used to store data and instructions during the
processing.
It is of 2 kinds: RAM and ROM
RAM:
RAM is volatile
It stores data when computer is on, so it is a temporary storage
It provides limited storage capacity due to its high cost
ROM:
ROM is non-volatile and read only memory.
It is a permanent storage
It comes programmed by the manufacturer.
Secondary Memory:
The secondary memory stores data and instructions permanently.
It is non-volatile memory
Examples: Hard disk drive, floppy drive and optical disk drive
It takes longer time to access data. It has high storage capacity
It is cheaper than RAM
Applications:
Science
Education
Medical and Health Care
Engineering/Architecture/Manufacturing
Entertainment
Communication
Business Application
Publishing
Banking
Decomposition:
The first step of computational thinking is decomposition. This stage stars by analyzing the problem,
stating it precisely, and establishing the criteria for the solution.
A computational thinking approach to a solution often starts by breaking the problem down into
smaller more familiar components so they can managed easier. The more you can break a problem
down, the easier it to be solve.
Pattern recognition:
Abstraction:
The abstraction stage involves the identification of key components of the solution
It requires the ability to filter out unnecessary elements of a problem so that only focus on the
important elements.
Algorithm Design:
The final stage within the computational thinking process is algorithm design whereby a detailed step
by step set of instructions are created which explain how to solve the problems
III. ALGORITHMS:
Algorithm is a step by step procedure for solving problem
It is an ordered sequence of finite and well defined instructions for completing a task
It is independent of any programming language.
Time – To execute a program, the computer system takes some amount of time. The lesser is the time
required, the better is the algorithm.
Memory – To execute a program, computer system takes some amount of memory space. The lesser is
the memory required, the better is the algorithm.
Accuracy – Multiple algorithms may provide suitable or correct solutions to a given problem, some of
these may provide more accurate results than others, and such algorithms may be suitable. Properties of
Algorithm:
Properties of an Algorithm:
It should always begin with the word ‘Start’ and end with the word ‘Stop’
Each step should be in a separate line
Steps should be numbered as Step 1, Step 2 and so on.
Examples:
Algorithm 1 Write an algorithm to accept two numbers. Calculate sum and print the result.
Step 1: Start
Step 2: Read two numbers A and B from user.
Step 4: Add A and B and store the result in Sum
Step 5: Print Sum.
Step 6: Stop.
Algorithm 3 Write an algorithm to find the minimum number in a given list of numbers.
Step 1: Start
Step 2: Read n.
Step 3: Read all n elements and store in list A.
Step 4: Assume first element in the list as min.
Step 5: Read all elements in the list one by one
If A[i] < min then assign
Min = a[i]
Step 6: Repeat step 5 till all the elements are compared.
Step 7: Print min as minimum element.
Step 8: Stop.
A set of statement are repetitively executed based upon a condition. If a condition evaluates to
true, the set of statements are executed again and again .As soon as the condition becomes false
the repetition stops.
Example Write an algorithm to find the minimum number in a given list of numbers.
Step 1: Start
Step 2: Read n.
Step 3: Read all n elements and store in list A.
Step 4: Assume first element in the list as min.
Step 5: Read all elements in the list one by one
If A[i] < min then assign
Min = a[i]
Step 6: Repeat step 5 till all the elements are compared.
Step 7: Print min as minimum element.
Step 8: Stop.
4. Functions:
Function is self contained block.
It is used to perform specific task
Larger programs are subdivided into smaller one by the use of functions (Modular
programming)
Functions are also named as methods, subroutines or procedures.
V. NOTATION:
1. PSEUDOCODE :
Pseudo-imitation, Code- instruction
Pseudocode is a short, readable and formally styled English language for explaining an algorithm.
It is an outline of a program, written in a form that can be easily converted into real programming
statements
Some keywords used are;
Input : INPUT, HET, READ AND PROMPT
Output : OUTPUT, PRINT, DISPLAY AND SHOW
Processing: COMPUTE, CALCULATE, DETERMINE, ADD, SUBTRACT, MULTIPLY AND
DIVIDE.
Initialize : SET and INITIALIZE
Incrementing: INCREMENT
Benefits of pseudocode
Language independent.
It is easier to develop a program from a pseudocode than with a flowchart.
It is easy to translate pseudocode into a programming language.
It is compact and does not tend to run over many pages
. Its simple structure and readability make it easier to modify.
Limitations of pseudocode
It does not provide visual representation of the program’s logic
Programmers use their own style of writing. There are no accepted standards.
It cannot be compiled nor executed
Example: Find the product of any 2 numbers
READ values of A and B
COMPUTE C by multiplying A and B
PRINT the result C
2. FLOW CHART
A flowchart is a pictorial representation of an algorithm
A flowchart is drawn using boxes of different shapes with the lines connecting them to show
the flow of control.
Boxes represent operations and the arrows represent the sequence.
Flowchart Symbols
Advantages of using
Flowcharts:
Examples:
3. PROGRAMMING LANGUAGES:
Compiled language is a programming language whose implementations are typically compilers and
not interpreters.
Procedural (Imperative) programming language implies specified in the steps that the programs
should take to reach to an intended state.
It makes the programs structured and easily traceable for program flow.
A markup language is an artificial language that uses annotations to text that define how the text is to
be displayed.
Logic programming is a type of programming paradigm which is largely based on formal logic.
Any program written in logical programming language is a set of sentences in logical form,
expressing facts and rules about some problem domain.
Concurrent programming is a computer programming technique that provides for the execution
of operations concurrently, either within a single computer, or across a number of systems.
In the later case, the term distributed computing is used.
The next decision is to choose between solving the problem exactly or solving it approximately.
Based on this, the algorithms are classified as exact and approximation algorithms. There are three
issues to choose an approximation algorithm.
1. There are certain problems like extracting square roots, solving non-linear equations which
cannot be solved exactly.
2. If the problem is complicated, it slows the operations. E.g. traveling salesman problem.
3. This algorithm can be a part of a more sophisticated algorithm that solves a problem
exactly.
DECIDING ON DATA STRUCTURES
Data structures play a vital role in designing and analyzing the algorithms. Some of the algorithm
design techniques also depend on the structuring data specifying a problem's instance.
Algorithm + Data structure = Programs
3. . ALGORITHM DESIGN TECHNIQUES
An algorithm design technique is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing. Learning these techniques is
important for two reasons.
1. They provide guidance for designing for new problems.
2. Algorithms are the cornerstones of computer science.
Algorithm design techniques make it possible to classify algorithms according to an
underlying design idea; therefore, they can serve as a natural way to both categorize and study
algorithms.
METHODS OF SPECIFYING AN ALGORITHM
Flowcharts and pseudo code are used for specifying an algorithm.
Flowchart is a pictorial representation of algorithm.
A Pseudocode, which is a mixture of a natural language and programming language like
constructs.
algorithm needs. Another desirable characteristic is simplicity. Simper algorithms are easier to
understand and program, the resulting programs will be easier to debug.
6. CODING AN ALGORITHM
Programming the algorithm by using some programming language.
Validity is done through testing and debugging.
Inputs should fall within a range and hence require no verification. The analysis has to be done
in various sets of inputs.
A good algorithm is a result of repeated effort and work.
The program's stopping or terminating condition has to be set.
Another important issue is the question of whether or not every problem can be solved by an
algorithm.
And the last, is to avoid the ambiguity which arises for a complicated algorithm.
There are various kinds of algorithm development techniques formulated and used for different types of
problems.
1. Iteration 2. Recursion
1. ITERATION:
Iteration is a process of repeating the same set of statements again and again until the specified
condition holds true. Computers execute the same set of statements again and again by putting them in
a loop.
The number of iterations to be performed is known in advance. The counter-controlled loop starts with
the initial value of the loop counter and terminates when the final value of the loop counter is reached.
Since the counter-controlled loops iterate a fixed number of times, which is known in advance, they are
also known as definite repetition loops.
Example: Consider a program segment to find the sum of first 100 integers.
i =1
sum = 0
while (i<=100):
sum = sum + i
i=i+1
(ii) Condition-Controlled Loops
The number of times the iteration is to be performed is not known. The termination depends on the
special value in the condition. If the condition value is true, the loop body will be executed, otherwise
it will not. This is indefinite repetition loop.
Example: Consider a program segment to find the sum of first 100 integers.
1. Start
2. Read N
3. Assign sum = 0, i = 0
4. Calculate i = i + 1
5. Check whether i>=N, if not repeat step 4.Otherwise go to next step.
6. Print the value of sum
7. Stop
2. RECURSION:
In a recursive programming, a function calls itself. Recursion is used to solve the problems that can be
expressed in terms of similar problems of smaller size.
Direct Recursion:
A function is directly recursive if it calls itself.
Indirect recursion:
Indirect recursion occurs if the function calls another function.
Problem statement:
Finding the minimum number in a list
Algorithm:
1. Start
2. Assign the first value of the list as minimum value.
3. Compare this value to the other value starting from second value.
4. When a value is smaller than the present, then it becomes the new minimum.
5. Print the minimum value.
Flow Chart:
Start
Read n, List A
Min=A[0]
No
iF A[i]<Min
yes
Min=A[i]
Print Min
M
Stop
Pseudocode:
Min = A[0]
i=1
WHILE (i < = n-1)
IF(A[i] < MIN) THEN
Min = A[i]
END IF
i=i+1
END WHILE
PRINT Min
Problem Statement:
Imagine that you are playing a card game. You are holding the cards in your hand and these cards are
sorted. You are given one new card. You have to put into the correct place so that the cards you are
holding are still sorted.
Algorithm:
Step 1: Start
Step 2: Read all numbers in an array A.
Step 3: Read Key.
Step 4: From the first position to the end of the array compare key and array element.
Step 5: if key < A then assign i to pos, and return pos.
Step 6: from n-1 to pos move the elements one position down.
Step 7: Now store key value in pos location.
Step 8: Stop
Flowchart:
Start
for i=0;i<=n-1;i++
No
Yes
if key<A[i]
pos=i
for (i=n-1;i>=pos;i--)
A[i+1]=A[i]
A[pos]=key
Print array A
Stop
Pseudocode:
Problem Statement:
The objective is to randomly generate integer number from 0 to n. Then the player has to guess the
number. If the player guesses the number correctly output an appropriate message.
If the guessed number is less than the random number generated, output the message “Your guess
is lower than the number. Guess again” .Then the player guesses another number. This process is
repeated until the player guesses the correct number.
Algorithm:
1. Start
2. Generate a random number and read num.
a. Enter the number to guess.
b. If(guess is equal to num)
Print “You guess the correct number”
Otherwise
If guess is less than num0
Print “Your guess is lower than the number. Guess again”
3. Stop
Flowchart:
Start
Read num
<num >num
if guess
=num
Stop
Pseudocode:
Binsearch( int value)
{
int upperBound, lowerBound, mid;
upperBound = n - 1;
lowerBound = 0;
while ( lowerBound < = upperBound)
{
mid = (upperBound + lowerBound) / 2;
If(A[mid] = = value)
return mid;
else
if(value < A[mid])
upperBound = mid - 1;
else
lowerBound = mid + 1;
}
return;
}
4. TOWER OF HANOI PROBLEM:
Problem Statement:
Tower of Hanoi is one of the classical problems of computer science. The problem states that,
1. There are stands(Stand 1,2 and 3) on which a set of disks, each with a different diameter, are
placed)
2. Initially, the disks are stacked on stand 1, in order of size, with the largest disk at the bottom.
The initial structure of Tower of Hanoi with three disks is shown below.
The “Tower of Hanoi problem” is to find a sequence of disk moves so that all the disks moved from
Stand-1 to Stand-3, adhering to the following rules.
The general strategy for solving the Tower of Hanoi problem with n disks is shown below.
The movement of n-1 disks forms the recursive case of a recursive solution to move a disks. The base
case of a solution involves the movement of only one disk. The recurrence relation for solving the
tower of Hanoi problem can be written as:
Flowchart:
Hanoi
No
If disk=1
yes
Stop
Recursive Algorithm:
Pseudocode:
START
Procedure Hanoi(disk, source, dest, aux)
If disk==1, THEN
Move disk from source to dest
ELSE
Hanoi(disk-1,source.aux,dest)
Move disk from source to dest
Hanoi(disk-1,aux,dest,source)
END IF
END Procedure
STOP