Information Technology Center

Algorithms I

BSS Main | This Lesson | Next Lesson

Each day we are faced with challenges which we must overcome. Your solution to each problem is your algorithm for that problem.

This lecture focuses on:

  1. What is an Algorithm?
  2. An Example
  3. How to solve a problem
  4. The Programmer's Challenge
  5. The Average Problem - No 1
  6. The Land Problem - No 2

What is an Algorithm?

An algorithm is a set of precisely defined steps guaranteed to arrive at an answer to a problem or a set of problems.

It does not matter what the problem is. We are faced with problems everyday. Each solution is an algorithm. There are usually different ways to solve a problem. Usually one of the approaches is the most efficient but they are all algorithms.

What if you had an interview for an important job and you wanted to get there on time to give a good first impression. How do you solve that problem?

Your algorithm may look like this :

  1. Get directions
  2. Decide on the mode of transport
  3. Note how long it takes you to prepare at home
  4. Visit the place - 1st visit
  5. Note the time it takes you to get there
  6. Return Home
  7. Ensure that you have all the relevant documents at hand
  8. Prepare for 2nd visit on date of interview
  9. Take selected mode of transportation

Things don't always go the way we plan it but this algorithm will very likely get you to your destination on time.

How To Solve a Problem

Your algorithm may look like this :

  1. Identify and name your problem.
  2. Study the problem
  3. Brake down the problem into its component parts.
  4. Decided on an approach to each component of the problem.
  5. Write down the steps in the correct order.
  6. Test your plan to ensure nothing is overlooked.
  7. Implemented plan.
  8. If the desired outcome is not obtained check for and correct errors
  9. Test your plan again.

The Programmer's Challenge

Before a programmer writes source code in a high level language he must design a solution. One approach is to first write an algorithm.

Keep these three points in mind

  1. Identify inputs needed.
  2. Decide on what action must be performed with the data you input
  3. Display your results.

Problem One

Write a program to find the average of any three numbers. These numbers will be input by the user of the program.

Version One


        Begin
         Input first number
          Input second number
            Input third number  
           calculate sum of the three numbers
         divide sum by three to calculate average
        print average
      End

The above algorithm can be condensed into a simpler version if we use letters for each input value and reduce the processing statements to simple mathematical equations.

Version Two

         Begin
              Input x, y, z
            sum = x + y + z
           average  = sum / 3
             Print average
           End

There is one INPUT line to get input for three numeric values. There are two PROCESSING lines. The first calculates the sum of the three values input and the second calculates the average of the three input values. Finally there is one OUTPUT statement.

Your Turn

Activity A-1

Write an algorithm to print the perimeter and the area of a rectangular piece of land.

Given :
perimeter = ( l + b )* 2
area = l * b

Remember, computer instructions fall into three main categories:
1. Get the required INPUT DATA first
2. Secondly perform all PROCESSING ( your calculations )
3. Finally print results or OUTPUT

...........

End of Introduction to Algorithms - Lesson One

  • InfoDAM Technologies