Você está na página 1de 8

1 Algo

What is debugging and what is profiling? Ans. Debugging is the process of identifying and fixing the errors in a program. Errors in a program can be identified by executing the program with a sample dataset. Profiling is the process of measuring the performance of the program by executing it on different data sets. Performance of a program is measured by recording the time and memory space that the program takes during its execution. What should I do if I have to solve a problem that contains many complex tasks? Ans: You can write separate algorithms for each task and then access these algorithms in other algorithms in a sequence. For example, if you have to solve a problem in which you have to perform the following tasks: Calculate the difference of two numbers Calculate the sum of other two numbers Print the output of both the operations You can write separate algorithms for each of the first two tasks and then print the output by writing a separate algorithm for the final task. Give at least 5 real life examples where we use stack operations? Ans. The real life examples of stacks are: Bangles in a hand: The bangles wore in a hand follow last-in-first-out (LIFO) strategy of stack. The bangle that you wear first is the last one to be taken out while removing all the bangles from the hand. The bangle that is worn last is the first one to be taken out. Same circumference circular rings in a pole: The rings having same circumference placed into a pole also follow LIFO strategy. The topmost ring, which was the last to be placed in the pole, is the first one to be taken out. Sacks full of wheat placed one over other: The sack at the top is removed first and the sack at the bottom is removed last. The bolts screwed to a single nut: When the bolts are screwed to a single nut, the last screwed bolt is unscrewed first and the bolt that was screwed first is unscrewed in the last. Battery cells in a torch: The battery cells in a torch also follow the same LIFO strategy of stack. 2. Give at least 5 real life examples where queue is used.? Ans. Real life examples of queue are: A queue of people at ticket-window: The person who comes first gets the ticket first. The person who is coming last is getting the tickets in last. Therefore, it follows first-in-first-out (FIFO) strategy of queue. Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out (FIFO) strategy of queue. Phone answering system: The person who calls first gets a response first from the phone answering system. The person who calls last gets the response last. Therefore, it follows first-infirst-out (FIFO) strategy of queue.

2 Algo

Luggage checking machine: Luggage checking machine checks the luggage first that comes first. Therefore, it follows FIFO principle of queue. Patients waiting outside the doctor's clinic: The patient who comes first visits the doctor first, and the patient who comes last visits the doctor last. Therefore, it follows the first-in-first-out (FIFO) strategy of queue.

What are the basic differences between a graph and a tree? Ans: The basic differences between a graph and a tree are: A tree cannot have any cycle, however, a graph can have a cycle in its structure. All the nodes in a tree structure should be connected to a root node either directly or through any other node, however, in a graph, it is not necessary. The numbers of edges in a tree are always one less than the number of nodes, however, in a graph, edges are less than the vertex but no relation exists between the vertex and its edges. Design and develop algorithms for multiplying n integers.? =Algorithm: Multiply_n_Integers Input: integers, number of integers to be multiplied (n), loop variable (i) Output: mul, updated Method: Display 'Enter the number of elements' Accept n Display 'Enter elements one by one' for (i = 1 to n in steps of 1 do) Accept a (i) end_for mul = 1 for (i = 1 to n in steps of 1 do) mul = mul*a(i) end_for Display 'multiplication of n integers is = ', mul Design and develop an algorithm for finding the middle element in three numbers.? Ans: To find the middle element one has to first sort the numbers in ascending order. The smallest number becomes the first element in the sorted list and the largest number becomes the last element in the sorted list. Consider three numbers 3, 1, and 7. The smallest number amongst these three is 1; therefore, 1 becomes the first element in the sorted list. Amongst 3 and 7, 3 is the second element. The larger number 7 is left out and becomes the last element. Hence the middle number 3 is displayed. Algorithm : Middle_3_Elements Input: a, b, c the three numbers to be sorted, two temporary variables k1, and K2 Output: Middle element in the three numbers. Method: If (a<b)

3 Algo

If (a<c) If (b<c) m=b else m=c end if else m=a end if else if (b<c) if (a<c) m=a else m=c end if else m=b end if end if

Develop an algorithm to find the number of Permutations and Combinations for a given n and r? =Permutation of a given number is given by n*(n-1)*(n-2)...up to r factors. This is a generalized algorithm for n>2 Algorithm: Permutation of a number for a given r Input: n and r Output: Permutation of n Method: a) per = 1 for (j = n to n - r + 1 in steps of -1 do) //where j is a loop variable per = per*j end_for Display ' Permutation = ', per b) Combination of a number n for a given r is calculated by nCr = nPr / r! Calculate the permutation nPr using the above algorithm Calculate the factorial for r using the algorithm fact = 1 for (j =1 to r in steps of 1 do) //where j is a loop variable fact = fact*j end_for comb = per / fact

4 Algo

Display ' Combination =', comb

4. Design an algorithm to generate all prime numbers within the limits l1 and l2. Algorithm: to generate all prime numbers between the limits l1 and l2. Input: l1 and l2 Output: Prime numbers between l1 and l2 Method: for (n=l1 to l2 in steps of 1 do) prime=true for (i=2 to n/2 in steps of 1 do) if (n % i =0) prime = false break end_if end_for if (prime = true) Display 'Prime number is =', n end_for Design an algorithm to find the reverse of a number. =Algorithm: Reverse of a number Input: number Output: Reverse of a number Method: new_number = 0 while (number > 0) n = number % 10 //n denotes a digit extracted from the number number = number / 10 new_number = new_number +n new_number= new_number*10 end while new_number = new_number/10 Display 'Reverse number is =', new_number

A number is said to be a palindrome if the reverse of a number is same as the original. Design an algorithm to check whether a number is a palindrome or not. =Algorithm: check whether the number is a palindrome or not Input: number, flag

5 Algo

Output: number is a palindrome Method: count = 0 while (number > 0) n = number%10 a(count)=n count = count+1 end while half = count/2 palin = true for (j=1 to half in steps of 1 and k=count to half in steps of 1 do) if (a (j)! =a(k)) palin = false break end if if (palin = true) Display 'Number is a palindrome' else Display 'Number is not a palindrome' end if end for How to calculate the factorial for number 0? =!0 =1 This exception is generally specified by the programmer. This condition can be checked using if else statement. How to find the largest of three numbers a, b, and c? = largest = a if (largest<b) largest = b if (largest<c) largest = c Display 'Largest number is', largest How to interchange the values of two numbers a and b? =Consider a variable c to temporarily store the value of variable, a. The following algorithm shows how to interchange the values of two numbers by using a temporary variable. Display 'Enter the value of a' Accept a Display 'Enter the value of b' Accept b c=a a=b b=c Display 'The values of a and b are ', a, b

6 Algo

How to interchange the values of two numbers a and b without using temporary variable? =Display 'Enter the value of a' Accept a Display 'Enter the value of b' Accept b a=a+b b=a-b a=a-b Display 'The values of a and b are ', a, b

Consider a data set of nine elements {10, 30, 45, 54, 56, 78, 213, 415, 500} and trace the linear search algorithm to find whether the keys 30, 150, 700 are present in the data set or not? =In linear search algorithm, each element in the list is compared with the given key element. If any of the elements in the list is equal to the given key element, the linear search algorithm returns TRUE, else it returns FALSE. Let us apply linear search to find the key element 30 in the given list. Take the first element 10 from the list and compare it with the key element 30. Clearly the two elements are not equal. Take the next element 30 from the list and compare it with the key element 30. Clearly, the two elements are equal. The algorithm returns the TRUE value, and the algorithm is terminated. Similarly, search for the key elements 150 and 700. At the end of the search, you will find that the key elements 150 and 700 are not found in the list. What should I do if I have to insert an element in a given list such that the elements in the list are in ascending order? =Sort the elements in the given list in ascending order using insertion sorting technique. Then select a position in the list where the new element is to be inserted. Move all the elements that are placed at the right hand side of this position by one position to the right. This process of moving the elements creates an empty space in the given list. Place the new element at this newly created space in the list. The space is created such that the element is placed in the ascending order in the list with respect to the elements already present in the list.

14. Design recursive algorithms to traverse a binary tree represented in linked lists in inorder, preorder and postorder. And hand simulate your algorithms with an example. Ans: The following figure shows a binary tree with 9 nodes where A is the root. A / \

7 Algo

B D

E F G H I There are three ways in which we can traverse a binary tree. They are inorder,preorder and post order. Inorder Traversal: The Steps for traversing a binary tree in the inorder sequence are as follws: 1.Traverse the left subtree 2.Visit the root 3.Traverse the right subtree The inorder sequence is as follows DHBEAFCIG Algorithm: In-order Traversal Input: rt,adress of the root node Output:Inorder sequence Method: if (rt!=NULL) { In-order Traversal([rt].L.child) Display ([rt].data) In-order Traversal ([rt].R.child) } Algorithm ends Preorder Traversal: The Steps for traversing a binary tree in the preorder sequence are as follws: 1.Visit the root 2.Traverse the left subtree 3.Traverse the right subtree The preorder sequence is as follows ABDHECFGI Algorithm: Pre-order Traversal Input: rt,adress of the root node Output: Preorder sequence Method: if (rt!=NULL) { Display ([rt].data) Pre-order Traversal([rt].L.child) Pre-order Traversal ([rt].R.child) } Algorithm ends Postorder Traversal: The Steps for traversing a binary tree in the post order sequence are as follws: 1.Traverse the left subtree 2.Traverse the right subtree 3.Visit the root Algorithm: Post-order Traversal Input: rt,adress of the root node Output: Postorder sequence

8 Algo

Method: if (rt!=NULL) { Post-order Traversal([rt].L.child) Post-order Traversal ([rt].R.child) Display ([rt].data) } Algorithm ends The postorder sequence is as follows HDEBFIGCA

Você também pode gostar