PROBLEM SOLVING TECHNIQUES SECTION - A 1. Answer any ten of the following a. Define an algorithm. An algorithm is a finite set of instructions that if followed, accomplishes a particular task. b. Define a program. A program is a set of instructions to solve a particular problem by the computer language. c. What is a verification condition (VC)? Symbolic executing enables us to transform the verification procedure into proving that the input assertion with symbolic values substituted for all input variables implies the output assertion with final symbolic values substituted for all variables. A proposition phrased in this way is referred to as a Verification Condition (VC). d. What is Fibonacci series? The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... The next number is found by adding up the two numbers before it. e. Given a number 189 10 convert it to octal equivalent. 275 8 f. What is the error condition (fixed error) that might be used to terminate square root algorithm? 1 x 10-6 or 0.0000001 g. What are prime numbers? Write first 10 prime numbers. Prime number is a number which is not divisible by any other number other then 1 and itself. First 10 prime numbers are 2,3,5,7,11,13,17,19,23,29. h. Find the GCD of 18 and 30. 6 i. What is longest monotone subsequence? A monotone increasing subsequence is a subset of numbers which are strictly increasing from left to right. This definition does not require that the numbers be adjacent in the original set or that the longest sequence is unique. For example: j. In which applications removing duplicates from an array algorithm is used? Data Compression and text processing problems k. What is sorting? Sorting is any process of arranging items systematically l. What is hashing? Hash searching is used to index and retrieve items in a list using a shorter fixed-length value because it is faster to find the item using the shorter hashed key than to find it using the original value.
SECTION - B Answer any four of the following 2. What is a flowchart? Draw and explain various symbols used in flowchart. Flowchart can be defined as a diagrammatic representation of and algorithm. Symbol Used Geometrical Name Oval Uses Parallellogram Input Rectangle Start and Stop Output Processing and 4. Write and explain the expression and parameters used for generating random numbers using Linear Congruential Method. where the parameters a, b, m and x0 must be carefully chosen in advance according to certain criteria. The parameters a, b, and m are referred to as the multiplier, increment, and modulus respectively. All parameters should be integers greater than or equal to zero and m should be greater than x 0,a and b. Parameter x 0 : The parameter x 0 can be chosen arbitrarily within the range 0--x 0 <m. Parameter m: The value of m should e greater than or Arrows Small Circle Rhombus Hexagon Connectors Connector Decision Making Repetition or Looping equal to the length of the random sequence required. In addition it must be possible to do the computation (a*x)+b mod m without roundoff. Further requirements on a are that it should be larger than m and less than m- m, (a-1) should be a multiple of every prime dividing into m, and if m is a multiple of 4 then (a-1) should also be a multiple of 4. These conditions together with the requirements that b should relatively prime to m are needed to guarantee that the sequence has a period of m. Parameter a: The choice of a depends on the choice of m. If m is a power of 2 then a should satisfy the condition: a mod 8 = 5 If m is a power of 10, then a should be chosen such that: a mod 200 = 21 3. Write the algorithm to exchange the values of two variables. PROBLEM STATEMENT: Given two variables, a and b, exchange the values assigned to them. INPUT: a as integer, b as integer OUTPUT: new value of a must be old value of b, new value of b must be old value of a. ALGORITHM exchange(a,b) t <-- a; a <-- b; b <-- t; Paramter b: The constant b should be odd and not a multiple of 5. When a, b, and m are chosen according to the conditions outlined above a sequence of m pseudorandom numbers in the range 0 to (m-1) can be generated before the sequence begins to repeat. 5. Write the algorithm to compute nth Fibonacci number. ALGORITHM fibonacci(n) //PROBLEM STATEMENT: Algorithm to print the first n fibonacci numbers.
//INPUT: n - numbers to be generated of the series. preceding term term a - as number before the b - as number preceding c - new term // OUTPUT: fibonacci series a <-- 0; b <-- 1; i <-- 2; write (a); write (b); while i < n do return; a <-- a + b; b <-- a + b; write (a); write (b); i <-- i + 2; 6. Explain how you rearrange the elements in an array so that they appear in reverse order. The problem of reversing the order of an array of numbers appears to be completely straightforward. We start the design of this algorithm by careful examination of the array before and after it has been reversed; for example, In terms of suffixes the exchanges are : Examining the effects of these exchanges we discover that after step 3 the array is completely reversed. In setting up our algorithm we need a pair of suffixes that model this increasingdecreasing behavior. Our increasing suffix can be the variable i which is incremented by1.for our decreasing suffix we might try n-i since this decreases by 1 with each increase in i by 1.This means that when i=1 we find that n-i is equal to n- 1 rather than n. We can correct this by adding 1. So the suffix n-i-1 can be used at other end rather than using n-i. 7. Write a short note on Two way merge. Merging two or more sets of data is a task that is frequently performed in computing. It is simpler than sorting because it is possible to take advantage of the partial order in the data. Examination of two ordered arrays should help to discover the essential of a suitable merging procedure. Consider the two arrays: A little though reveals that the merged result should be as indicated below: What we observe from our diagram is that the first element ends up in the last position. Carrying this process through we get the following set of exchanges. The origins are written above each element in the c array. What we see here is c is longer than a and b. In fact c must contain a number of elements corresponding to the sum of the elements in a and b (i.e., a+b). To see how this might be done let us consider the smallest merging problem. To merge the two one dimensional array all we need to do is select
the smaller of the a and b elements and place it in c.the larger element is then placed in c. 8 is less than 15 so 8 will take c[1] place and 15 c[2] place. In the same way we start merging arrays of lengths m and n.the comparison between a[1] and b[1] SECTION - C Answer any four of the following 8. a. Explain various characteristics of an algorithm. INPUT: An algorithm may accept zero or more quantities. OUTPUT : An algorithm must produce atleast one quantity. DEFINITENESS: Each instruction is clear, precise and well defined. There should not be any ambiguity. FINITENESS: The algorithm must have finite number of instructions. Thus it terminate after a finite number of steps. must EFFECTIVENESS: Every instruction must be basic so that it must be carried out, in principle, by a person using only pencil and paper. allows us to set c[1]. After placing 8 in c[1] we need a way of deciding which element must be placed next in the c array. In the general case the next element to be placed into c is always going to be the smaller of the first elements in the unmanaged parts of arrays a and b. To keep track of the yet to be merged parts of both the a and b arrays two index pointers i and j will be needed. As an element is selected from either a or b the appropriate pointer must be incremented /decremented by 1. a set of more precisely defined subtasks. These subtasks should more accurately describe how the final goal is to be reached. With each splitting of a task into subtasks it is essential that the way in which the subtasks need to interact with each other be precisely defined. Only in this way is it possible to preserve the overall structure of the solution to the problem. Preservation of the overall structure in the solution to a problem is important both for making the algorithm comprehensible and also for making it possible to prove the correctness of the solution. The process of repeatedly breaking a task down into subtasks and then each subtask into still smaller subtasks must continue until we eventually end up with subtasks that can be implemented as program statements. 9. Write the algorithm and description of sine function computation. Algorithm Description b. What is top-down design? Explain how you break a problem in sub problems. A technique for algorithm design that tries to accommodate this human limitation is known as top-down design or stepwise refinement. Topdown design provides us with a way of handling the inherent logical complexity and details, frequently encountered in computer algorithms. Breaking a problem into subproblems Top-down design suggests that we take the general statements that we have about the solution, one at a time, and break them down into Algorithm ALGORITHM sin(x) //PROBLEM STATEMENT : Given a value x generate an algorithm to evaluate the function sin(x) as defined by the series expansion sin(x) = x/1!- x3/3! + x5/5! - x7/7!+... //INPUT : x as real //OUTPUT: to generate sin(x)
//error is termination d <- 3; condition while (n mod d = 0) AND (d < r) error <-- 1.0* e-6; i <-- 1; x2 <-- x * x; term <-- x; tsin <-- x; while abs(term) > error do (i* (i-1)); i <-- i + 2; term <-- -term * x2 / do d <- d + 2; if n mod d = 0 then sdivisor <- d; else sdivisor <- 1; tsin <-- tsin + term; return tsin; 10. a. Write the algorithm to find GCD of two numbers. ALGORITHM gcd(n,m) PROBLEM STATEMENT: Given two positive non-zero integers n and m design an algorithm for finding their greatest common divisor (GCD). INPUT: n, m as integers whose gcd is to be sought OUTPUT: gcd of n, m integers //r :integer variable to store the remainder repeat r <--n mod m; n <-- m; m <-- r; until r=0 return m; b. Write the algorithm to find smallest divisor of an integer. ALGORITHM smallestdivisor(n) if NOT odd(n) then sdivisor <- 2; else r <- trun(sqrt(n));
11. Write short notes on a. Finding maximum in a set. b. Array Counting or Histogramming. b. Given an element x and a set of data that is in ascending order, write an Binary Search algorithm to find whether or not x is present in the set. //PROBLEM STATEMENT: Algorithm to find an element using binary search method. //INPUT: a: integer array n : integer variable to hold upper limit 12. a. Given a set of numbers below, sort them into non decreasing order using insertion sort 20 35 18 8 14 41 3 39 element key: integer variable, key //OUTPUT: position of the element found. Algorithm BinarySearch(a,n,key) bottom = 1; do top = n; mid = (bottom + top) / 2; if (item < a[mid]) top = mid - 1; else if (item > a[mid]) bottom = mid + 1; while (item!= a[mid] && bottom <= top); if (item == a[mid]) return mid+1; else return -1;