Introduction to Algorithms and Programming



[pic]

College of Arts and Sciences

Department of Mathematics and Physical Sciences

Computer Science Section

SPRING 2011

COMP 151

Introduction to Algorithms and Programming

(3 Hours Lectures and 2 Hours lab)

Mr. M.Syed Shahul Hameed

CHAPTER 1

INTRODUCTION TO PROBLEM SOLVING AND ALGORITHMS

Algorithms and Humans

Algorithms are not a natural way of stating a problem’s solution, because we do not normally state our plan of action. We tend to execute as we think about the problem. Hence, there are inherent difficulties when writing an algorithm. We normally tailor our plans of action to the particular problem at hand and not to a general problem (i.e. a nearsighted approach to problem solving). We usually do not write out our plan, because we are usually unaware of the basic ideas we use to formulate the plan. We hardly think about it – we just do it. Computer programmers need to adopt a scientific approach to problem solving, i.e. writing algorithms that are comprehensive and precise. We need to be aware of the assumptions we make and of the initial conditions. Be careful not to overlook a step in the procedure just because it seems obvious. Remember, machines do not have judgment, intuition or common sense!

Problem:

In a place 4095 bananas were there. First a Monkey came and it ate a single banana, second monkey ate twice of the first monkey, and third monkey ate twice of the second monkey….its going on. At a particular stage all the bananas were exactly (no fraction) eaten by some number of monkeys. How many monkeys came there?

Problem:

The number 45 is broken into four parts such as part1=X + 2, part2=X – 2, part3=X * 2, and part4=X / 2. In all the parts X is same. Find the value of X.

What is an Algorithm?

▪ An Algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time

▪ Is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.

[pic]

More precisely, an algorithm is a method or process to solve a problem satisfying the following properties:

▪ Finiteness

– terminates after a finite number of steps

▪ Definiteness

– Each step must be carefully and clearly specified.

▪ Input

– Valid inputs must be clearly specified.

▪ Output

– Can be proved to produce the correct output given a valid can be proved to produce the correct output given a valid input.

▪ Effectiveness

– Steps must be sufficiently simple and basic.

Basic (primitive) operations involved

• Read the input from user

• Carry out basic arithmetical computations

• Print the output to the user

Variables

A variable is a symbolic name assigned to a data item by the programmer. At any particular time, a variable will stand for one particular data, called the value of a variable, which may change from time to time during a computing process. The value of a variable may change many times during the execution of a program. A variable is usually given a name by the programmer.

A named location that stores a value

A = 5

B = 3 A

C = A + B

B = 4

C = A + B

A = 7 B

A = A + 3

A = A + C

C

Arithmetic operators

+       add

-        subtract

*        multiply

/      divide

=       assign

% mod

Relational operators

= = Equal to

> Greater than

< Less than

>= Greater than or equal

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download