Past Microprocessor Question

Topics: Integer, Addition, Prime number Pages: 15 (4111 words) Published: March 11, 2013
There are 3 questions on this exam. You have 2 hours to complete it. Please do your own work.

1. The number 124 has the property that it is the smallest number whose first three multiples contain the digit 2. Observe that 124*1 = 124, 124*2 = 248, 124*3 = 372 and that 124, 248 and 372 each contain the digit 2. It is possible to generalize this property to be the smallest number whose first n multiples each contain the digit 2. Write a function named smallest(n) that returns the smallest number whose first n multiples contain the digit 2. Hint: use modulo base 10 arithmetic to examine digits.

Its signature is
int smallest(int n)

You may assume that such a number is computable on a 32 bit machine, i.e, you do not have to detect integer overflow in your answer.

Examples
|If n is |return |because | |4 |624 |because the first four multiples of 624 are 624, 1248, 1872, 2496 and they all contain the | | | |digit 2. Furthermore 624 is the smallest number whose first four multiples contain the digit 2. | |5 |624 |because the first five multiples of 624 are 624, 1248, 1872, 2496, 3120. Note that 624 is also | | | |the smallest number whose first 4 multiples contain the digit 2. | |6 |642 |because the first five multiples of 642 are 642, 1284, 1926, 2568, 3210, 3852 | |7 |4062 |because the first five multiples of 4062 are 4062, 8124, 12186, 16248, 20310, 24372, 28434. | | | |Note that it is okay for one of the multiples to contain the digit 2 more than once (e.g., 24372). |

2. Define a cluster in an integer array to be a maximum sequence of elements that are all the same value. For example, in the array {3, 3, 3, 4, 4, 3, 2, 2, 2, 2, 4} there are 5 clusters, {3, 3, 3}, {4, 4}, {3}, {2, 2, 2, 2} and {4}. A cluster-compression of an array replaces each cluster with the number that is repeated in the cluster. So, the cluster compression of the previous array would be {3, 4, 3, 2, 4}. The first cluster {3, 3, 3} is replaced by a single 3, and so on.

Write a function named clusterCompression with the following signature

If you are programming in Java or C#, the function signature is int[ ] clusterCompression(int[ ] a)

If you are programming in C++ or C, the function signature is int *clusterCompression(int a[ ], int len) where len is the length of the array.

The function returns the cluster compression of the array a. The length of the returned array must be equal to the number of clusters in the original array! This means that someplace in your answer you must dynamically allocate the returned array.

In Java or C# you can use
int[ ] result = new int[numClusters];

In C or C++ you can use
int *result = (int *)calloc(numClusters, sizeof(int));

Examples
|a is |then function returns | |{0, 0, 0, 2, 0, 2, 0, 2, 0, 0} |{0, 2, 0, 2, 0, 2, 0} | |{18} |{18} | |{} |{} | |{-5, -5, -5, -5, -5} |{-5} | |{1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1} |{1, 2, 1} | |{8, 8, 6, 6, -2, -2, -2} |{8, 6, -2} |

3. Define an array to be a railroad-tie array if the following three conditions hold

a. The array contains at least one non-zero element
b. Every non-zero element has exactly one non-zero neighbor
c. Every zero element has two non-zero neighbors.

For example,...