0% found this document useful (0 votes)
34 views5 pages

Lecture 10 (Algorithms)

The document introduces algorithms and their key characteristics. An algorithm is defined as a set of steps to solve a problem or complete a task. It must be unambiguous, use well-defined inputs and outputs, finite, feasible, and language independent. The document provides examples of designing algorithms to add two numbers and find the largest of three numbers by defining the problem, inputs, outputs, and step-by-step solution. Algorithms are then tested by implementing them in a programming language like C.

Uploaded by

love109.kush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
34 views5 pages

Lecture 10 (Algorithms)

The document introduces algorithms and their key characteristics. An algorithm is defined as a set of steps to solve a problem or complete a task. It must be unambiguous, use well-defined inputs and outputs, finite, feasible, and language independent. The document provides examples of designing algorithms to add two numbers and find the largest of three numbers by defining the problem, inputs, outputs, and step-by-step solution. Algorithms are then tested by implementing them in a programming language like C.

Uploaded by

love109.kush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

INTRODUCTION TO ALGORITHMS

What is Algorithm?
The word Algorithm means “a process or set of rules to be followed in
calculations or other problem-solving operations”.
Therefore Algorithm refers to a set of rules/instructions that step-by-step define
how a work is to be executed upon in order to get the expected results.
It can be understood by taking an example of cooking a new recipe. To cook a new
recipe, one reads the instructions and steps and execute them one by one, in the given
sequence. The result thus obtained is the new dish cooked perfectly. Similarly,
algorithms help to do a task in programming to get the expected output.
The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language, and yet the output will be the
same, as expected.
Characteristics of an Algorithm?

In order for some instructions to be an algorithm, it must have the following


characteristics:

 Clear and Unambiguous: Algorithm should be clear and unambiguous.


Each of its steps should be clear in all aspects and must lead to only one meaning.

 Well-Defined Inputs: If an algorithm says to take inputs, it should be well-


defined inputs.

 Well-Defined Outputs: The algorithm must clearly define what output will
be yielded and it should be well-defined as well.

 Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite
loops or similar.
 Feasible: The algorithm must be simple, generic and practical; such that it can
be executed upon will the available resources. It must not contain some future
technology, or anything.

 Language Independent: The Algorithm designed must be language-

independent, i.e. it must be just plain instructions that can be implemented in any
language, and yet the output will be same, as expected.

How to Design an Algorithm?


In order to write an algorithm, following things are needed as a pre-requisite:

1. The problem that is to be solved by this algorithm.


2. The constraints of the problem that must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output to be expected when the problem is solved.
5. The solution to this problem, in the given constraints.
Then the algorithm is written with the help of above parameters such that
it solves the problem.
Example:
Consider the example to add two numbers and print the sum.
 Step 1: Fulfilling the pre-requisites
As discussed above, in order to write an algorithm, its pre-requisites must
be fulfilled.

1. The problem that is to be solved by this algorithm: Add 2 numbers and


print their sum.
2. The constraints of the problem that must be considered while
solving the problem: The numbers must contain only digits and no other
characters.
3. The input to be taken to solve the problem: The two numbers to be
added.
4. The output to be expected when the problem is solved: The sum of
the two numbers taken as the input.
5. The solution to this problem, in the given constraints: The solution

consists of adding the 2 numbers. It can be done with the help of „+‟ operator, or
bit-wise, or any other method.

Step 2: Designing the algorithm


Now let‟s design the algorithm with the help of above pre-requisites:

Example 1: Write an algorithm to add two numbers entered by the 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

Step 3: Testing the algorithm by implementing it.


In order to test the algorithm, let‟s implement it in C language.

// C program to add two numbers with the help of above designed algorithm

#include <stdio.h>
main()
{
int num1, num2; // Variables to take the input of the 2 numbers
int sum; // Variable to store the resultant sum
printf("Enter the 1st number: ");
scanf("%d", &num1); // Take the 2 numbers as input
printf("Enter the 2nd number: ");
scanf("%d", &num2);
sum = num1 + num2 // Calculate the sum using + operator and store it in variable sum.

printf("\nSum of the 2 numbers is: %d", sum); // Print the sum


}
Example 2: Write an algorithm to find the largest among three different
numbers entered by the 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
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

You might also like