Professional Documents
Culture Documents
14EC320 - Assignment
14EC320 - Assignment
1 Fundamental algorithms:
Swapping:
1. Given two glasses marked A and B. Glass A is full of raspberry drink and glass B is full
f lemonade. Suggest a way of exchanging the contents of glasses A and B.
4. Given two variables of integer type a and b, exhange their values without using a third
temporary variable.
Counting
5. Given a set of n students' examination marks (in the range 0 to 100) make a count of the
number of students that passed the examination. A pass is awarded for all marks of 50
and above.
6. In the above problem, assume that your program is to start out with the variable count
initialized to the total number of students n. On termination, the value of count should
again represent the number of students that passed the examination. If there were more
passes than fails, why would this implementation be better than the original one?
7. Design an algorithm that reads a list of numbers and makes a count of the number of
negatives and the number of non-negative members in the set.
12. Develop an algorithm to compute the sums for the first n terms (n>=O) of the following
series:
(a) s = 1+2+3+
(b) s= 1+3+5+ * * *
(c) s= 2+4+6+
(d) s = 1 + 1/2+ 1/3+
13. Generate the first n terms of the sequence without using multiplication.
1 2 4 8 16 32 ......
14. Develop an algorithm that prints out n values of the sequence
1 -1 1 -1 1 -1 ......
15. Develop an algorithm to compute the sum of the first n terms ( n >=1 ) of the series
S = 1-3+5-7+9-.......
FACTORIAL COMPUTATION
22. Generate and print the first n terms of the Fibonacci sequence where n >=0
The first few terms are:
0 , 1 , 1 , 2 , 3 , 5 , 8 , 1 3,. ..
23. The first few numbers of the Lucas sequence which is a variation on
the Fibonacci sequence are:
1 3 4 7 11 18 29 ...
Design an algorithm to generate the Lucas sequence.
24. Given a = 0, b = 1, and c = 1 are the first three numbers of some sequence. All
other numbers in the sequence are generated from the sum of their three most
recent predecessors. Design an algorithm to generate this sequence.
25. Given that two numbers d and e are suspected of being consecutive members
of the Fibonacci sequence design an algorithm that will refute or confirm this
conjecture.
26. Generate the sequence where each member is the sum of adjacent
factorials, i.e.
Base Conversion
FACTORING METHODS
34. The geometric mean is used to measure central tendency. It is defined as
ARRAY TECHNIQUES
44. Rearrange the elements in an array so that they appear in reverse order.
45. Design an algorithm that places the kth element of an array in position 1 , the
(k+l)th element in position 2, etc. The original lst element is placed at ( n - k +
l ) and so on.
46. Given a set of n students' examination marks (in the range 0 to 100) make a
count of the number of students that obtained each possible mark.
47. It is required to generate a histogram distribution for a set of daily average
temperatures recorded in Antarctica. The temperatures are integer values in the
range -40°C to +5"C. Design an algorithm to input n such temperatures and
produce the appropriate distribution.
48. Find the maximum number in a set of n numbers.
49. Design an algorithm to find the minimum in an array.
50. Design an algorithm to find the number of times the maximum occurs in an
array of n elements. Only one pass through the array should be made.
51. Design an algorithm to find the maximum in a set and the position
(a) where it first occurs;
(b) where it last occurs.
52. Design an algorithm to find the maximum absolute difference between
adjacent pairs of elements in an array of n elements.
53. Design an algorithm to find the second largest value in an array of n elements.
54. Find the position of a number x (if it occurs) in an array of n elements.
55. Design an algorithm for finding the maximum in a set by comparing each
number with all other numbers. Your algorithm should terminate when it finds
a number that is greater than or equal to all other numbers. What happens
when you look for the most efficient implementation of this method?
56. Design an algorithm to find the minimum, the maximum, and how many times
they both occur in an array of n elements.
57. The minimum and maximum in an array of size n can be found using (3/2)n
comparisons instead of 2n comparisons by considering the elements in pairs
and comparing the larger element of the pair against the current maximum and
the smaller element of the pair against the current minimum. Implement this
algorithm.
58. Remove all duplicates from an ordered array and contract the array
accordingly.
59. Remove from an ordered array all numbers that occur more than once.
60. Delete from an ordered array all elements that occur more than k times
61. Given a randomly ordered array,of n elements, partition the elements into two
subsets such that elements ~x are in one subset and elements >x are in the
other subset.
62. Another simple but less efficient way to partition the data is to apply the
following rules with each iteration:
(a) if a[i]~xth en increase i;
(b) otherwise if a[j]>x then decrease j;
(c) if neither of the above conditions apply then exchange a[i] and
a[j] and increase i and decrease j.
Implement this algorithm.
63. Given a randomly ordered array of n elements determine the kth smallest
element in the set.
64. Merge two arrays of integers, both with their elements in ascending order,
into a single ordered array.
65. Design and implement a merging algorithm that uses only two arrays. It can be
assumed that the sizes of the two data sets are known in advance. An
interesting way to do this is to place the array with the biggest element so that
it fills up the output (merged) array. The following diagram illustrates the idea
(the b array has the largest element).
A manufacturer would like to have a device for a car that will turn on a light when the
temperature is between 34 and 40 degrees Fahrenheit (F) and sound a warning signal
when the outside temperature is 34 degrees F or below. The light and the sound are
never going simultaneously. Write a solution to this problem.