Information Technology Center


Home | About | Comments | Lectures | Notice | Registration

Algorithms I

Updated : January 20th, 2009
Tell a Friend


INTRODUCTION

You already know something about algorithms. I will prove it to you now. 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 Algorithm Case Study
  3. How to solve a problem
  4. The Programmer's Challenge
  5. The Average Problem - No 1
  6. The Land Algorithm Problem - No 2
  7. Answer to the land algorithm

What is an Algorithm?

An algorithm is a step by step solution to a problem. It 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. As mentioned before, 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. Ask around until you find someone who has an idea where the place is located
  2. You may decide to visit the place before.
  3. Decide on the best way to get there.
  4. Note the time it is likely to take to get there
  5. Note how long it takes you to prepare at home
  6. Ensure that you have all the relavant papers at hand
  7. Leave you home on time

Case Study

Mary wanted to bake a cake but did not know how to do it. She borrowed a recepie book from a friend. Selected a special recepie she liked. Purchased the ingredients. Followed the instructions. She then made a tasty cake.

In the above case study Mary had a problem and she found a solution. She developed an algorithm. In fact each recepie in the book is an algorithm. We can think of the baking book as a book of baking algorithms.

What is the relationship between life problems and the problems that a programmer face daily? We go about solving programming problems in very much the same way we solve other problems. If you can solve a real life problem then you can solve a programming problem if you put your mind to it. Many programming problems can be less complicated than most of the challenges we face in life on a regular basis.

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. When the desired outcome is not obtained repeat the process.

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. Computers need data for input.
  2. Decide on what action must be performed with the data you enter.
  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
         Inupt 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 condenced 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.

Now Try Problem Two

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
1. Get the required Input data first
2. Secondly perform all Processing ( your calculations )
3. Finally print results or Output

Compare your answer with mine.


Lecturer:

The Tutor
Do you have a question or a comment ?
tutordam@yahoo.com


Home | About | Comments | Lectures | Notice | Registration