Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

2.

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.

2. Design an algorithm that makes the following exchanges: . The arrows


indicate that b is to assume the value of a, c the value of b, and so on.

3. Design an algorithm that makes the following exchanges:

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.

SUMMATION OF A SET OF NUMBERS

8. Design an algorithm to compute the average of n numbers.


9. Redesign the algorithm so that it only needs to perform n-1 additions to sum n numbers.
10. Design an algorithm to compute the sum of the squares of n numbers. That is,

11. The harmonic mean defined by


is sometimes used as a mean of central tendency. Develop an algorithm to compute the
harmonic mean of n data values.

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

16. For a given n, design an algorithm to compute l/n!.


17. For a given x and a given n, design an algorithm to compute x n/n!.
18. Design an algorithm to determine whether or not a number n is a factorial
number.
19. Design an algorithm which, given some integer n, finds the largest factorial
number present as a factor in n.
20. Design an algorithm to simulate multiplication by addition. Your program
should accept as input two integers (they may be zero, positive, or negative).
21. The binomial theorem of basic algebra indicates that the coefficient "c, of the
rth power of x in the expansion of (x+ 1)" is given by

Design an algorithm that evaluates all coefficients of x for a given value of n.

GENERATION OF THE FIBONACCI SEQUENCE

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.

27. Design an algorithm that counts the number of digits in an integer.


28. Design an algorithm to sum the digits in an integer.
29. Design an algorithm that reads in a set of n single digits and converts them
into a single decimal integer. For example, the algorithm should convert the set
of 5 digits {2,7,4,9,3) to the integer 27493.

Base Conversion

30. Design an algorithm that converts binary numbers to octal.


31. Design an algorithm to convert binary numbers to decimal.
32. Design an algorithm that accepts as input a decimal number and converts it to
the binary-coded decimal (bcd) representation. In the bcd scheme each digit is
represented by a 4-bit binary code.
33. Design an algorithm that accepts as input a decimal fraction and converts it to
the corresponding binary fraction of a fixed accuracy

FACTORING METHODS
34. The geometric mean is used to measure central tendency. It is defined as

Develop an algorithm to input n numbers and compute their geometric mean.


35. Given an integer n devise an algorithm that will find its smallest exact divisor
other than one.
36. Design an algorithm to produce a list of all exact divisors of a given positive
integer n.
37. Design and implement an algorithm that finds the smallest positive integer
that has n or more divisors.
38. For the integers in the range 1 to 100 find the number that has the most
divisors.
39. Given two positive non-zero integers n and rn design an algorithm for finding
their greatest common divisor (usually abbreviated as gcd).
40. Design an algorithm to establish all the primes in the first n positive integers.
41. Every integer can be expressed as a product of prime numbers. Design an
algorithm to compute all the prime factors of an integer n.
42. A perfect number is one whose divisors add up to the number. Design and
implement an algorithm that prints all perfect numbers between 1 and 500.
43. Given some integer x, compute the value of xn where n is a positive integer
considerably greater than 1.

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.

MERGING, SORTING AND SEARCHING

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).

This simplifies the merge because when the merging of a is completed


the remaining elements of b will be in place.

66. Design an algorithm for merging three arrays.


67. Given a randomly ordered set of n integers, sort them into non-descending
order using the selection sort.
68. Implement a selection sort that removes duplicates during the sorting
process.
69. Given a randomly ordered set of n numbers sort them into non-descending
order using an exchange method.
70. Given a randomly ordered set of n numbers sort them into non-descending
order using an insertion method.
71. Given a randomly ordered set of n numbers sort them into non-descending
72. order using Shell's diminishing increment insertion method.
73.

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.

You might also like