






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.
If you are writing in Java or C#, the function signature is int largestAdjacentSum(int[ ] a)
If you are writing in C or C++, the function signature is int largestAdjacentSum(int a[ ], int len) where len is the number of elements in a
Examples:
if a is return
{1, 2, 3, 4} 7 because 3+4 is larger than either 1+2 or 2+
{18, -12, 9, -10} 6 because 18-12 is larger than -12+9 or 9-
{1,1,1,1,1,1,1,1,1} 2 because all adjacent pairs sum to 2
{1,1,1,1,1,2,1,1,1} 3 because 1+2 or 2+1 is the max sum of adjacent pairs
The function returns 1 if n is equal to the sum of each of its digits contenated catlen times. Otherwise, it returns 0. You may assume that n and catlen are greater than zero
Hint: Use integer and modulo 10 arithmetic to sequence through the digits of the argument.
Examples:
if n is and catlen is return reason
198 2 1 because 198 == 11 + 99 + 88
198 3 0 because 198 != 111 + 999 + 888
2997 3 1 because 2997 == 222 + 999 + 999 + 777
2997 2 0 because 2997 != 22 + 99 + 99 + 77
13332 4 1 because 13332 = 1111 + 3333 + 3333 + 3333 + 2222
9 1 1 because 9 == 9
Write a method named isSequencedArray that returns 1 if its argument is a m-n sequenced array, otherwise it returns 0.
If you are writing in Java or C# the function signature is int isSequencedArray(int[ ] a, int m, int n)
If you are writing in C or C++ the function signature is int isSequencedArray(int a[ ], int len, int m, int n) where len is the number of elements in the array a.
You may assume that m<=n
Examples
if a is
and m is
and n is return^ reason
because the array contains all the numbers between 1 and 5 inclusive in ascending order and no other numbers.
{1, 3, 4, 2, 5} 1 5 0 because the array is not in ascending order.
because the array contains all the numbers between -5 and -2 inclusive in ascending order and no other numbers. Note that duplicates are allowed.
{0, 1, 2, 3, 4, 5} 1 5 0 because 0 is not in between 1 and 5 inclusive
{1, 2, 3, 4} 1 5 0 because there is no 5
{1, 2, 5} 1 5 0 because there is no 3 or 4
{5, 4, 3, 2, 1} 1 5 0
because the array does not start with a 1. Furthermore, it is not in ascending order.
There are three questions on this exam. You have 2 hours to complete it. Please indent your programs so that it is easy for the grader to read.
Examples
if n is return reason
2 {2} because 2 is prime
6 {2, 3} because 6 = 2*3 and 2 and 3 are prime.
14 {2, 7} because 14=2*7 and 2 and 7 are prime numbers.
24 {2, 2, 2, 3} because 24 = 222*3 and 2 and 3 are prime
1200 {2, 2, 2, 2, 3, 5, 5} because 1200=2222355 and those are all prime
1 null because n must be greater than 1
-18 null because n must be greater than 1
{ 1, 1, 1 , 2, 2, 1, 1, 3} { 1 , 2, 1, 3} (first 1 of pattern matches three 1s in array) {1, 1, 1, 2, 2 , 1, 1, 3} {1, 2 , 1, 3} (next element of pattern matches two 2s in array) {1, 1, 1, 2, 2, 1, 1 , 3} {1, 2, 1 , 3} (next element of pattern matches two 1s in array) {1, 1, 1, 2, 2, 1, 1, 3 } {1, 2, 1, 3 } (last element of pattern matches one 3 in array)
The pattern must be completely matched, i.e. the last element of the array must be matched by the last element of the pattern.
Here is an incomplete function that does this pattern matching. It returns 1 if the pattern matches the array, otherwise it returns 0.
static int matchPattern(int[] a, int len, int[] pattern, int patternLen) { // len is the number of elements in the array a, patternLen is the number of elements in the pattern. int i=0; // index into a int k=0; // index into pattern int matches = 0; // how many times current pattern character has been matched so far for (i=0; i<len; i++) { if (a[i] == pattern[k]) matches++; // current pattern character was matched else if (matches == 0 || k == patternLen-1) return 0; // if pattern[k] was never matched (matches==0) or at end of pattern (k==patternLen-1) else // advance to next pattern character { !!You write this code!! } // end of else } // end of for
// return 1 if at end of array a (i==len) and also at end of pattern (k==patternLen-1) if (i==len && k==patternLen-1) return 1; else return 0; }
Please finish this function by writing the code for the last else statement. Your answer just has to include this code, you do not have to write the entire function.
Hint: You need at least 4 statements (one of them an if statement)
Examples
if a is
and pattern is return^ reason
{1, 1, 1, 1, 1} {1} 1 because all elements of the array match the pattern element 1
{1} {1} 1 because all elements of the array match the pattern element 1
{1, 1, 2, 2, 2, 2} {1, 2} 1 because the first two 1s of the array are matched by the first pattern element, last four 2s of array are matched by the last pattern element
{1, 2, 3} {1, 2} 0 because the 3 in the array is not in the pattern.
{1, 2} {1, 2, 3} 0 because the 3 in the pattern is not in the array
{1, 1, 2, 2, 2, 2, 3} {1, 3} 0 because at least one 3 must appear after the sequence of 1s.
{1, 1, 1, 1} {1, 2} 0 because the array ends without matching the pattern element 2.
{1, 1, 1, 1, 2, 2, 3, 3} {1, 2} 0 because the element 3 of the array is not matched
{1, 1, 10, 4, 4, 3} {1, 4, 3} 0
because the 10 element is not matched by the 4 pattern element. Be sure your code handles this situation correctly!
There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.
the 4-based rounding of 5 is 4 because 5 is closer to 4 than it is to 8, the 5-based rounding of 5 is 5 because 5 is closer to 5 that it is to 10, the 4-based rounding of 6 is 8 because 6 is equidistant from 4 and 8, so the greater one is used, the 13-based rounding of 9 is 13, because 9 is closer to 13 than it is to 0,
Write a function named doIntegerBasedRounding that takes an integer array and rounds all its positive elements using n-based integer rounding.
A negative element of the array is not modified and if n <=0, no elements of the array are modified. Finally you may assume that the array has at least two elements.
Hint: In integer arithmetic, (6/4) * 4 = 4
If you are programming in Java or C#, the function signature is void doIntegerBasedRounding(int[ ] a, int n) where n is used to do the rounding
If you are programming in C or C++, the function signature is void doIntegerBasedRounding(int a[ ], int n, int len) where n is used to do the rounding and len is the number of elements in the array a.
Examples
if a is and n is then a becomes reason
because the 2-based rounding of 1 is 2, the 2-based rounding of 2 is 2, the 2-based rounding of 3 is 4, the
Write a function named isCubePowerful that returns 1 if its argument is cube-powerful; otherwise it returns 0.
The function prototype is int isCubePowerful(int n);
Hint: use modulo 10 arithmetic to get the digits of the number.
Examples:
if n is return because
153 1 because 153 = 1^3 + 5^3 + 3^3
370 1 because 370 = 3^3 + 7^3 + 0^3
371 1 because 371 = 3^3 + 7^3 + 1^3
407 1 because 407 = 4^3 + 0^3 + 7^3
87 0 because 87 != 8^3 + 7^3
0 0 because n must be greater than 0.
-81 0 because n must be greater than 0.
The number is positive because the first element of the array is >= 0.
If you are programming in Java or C#, the function prototype is int decodeArray(int[ ] a)
If you are programming in C or C++, the function prototype is int decodeArray(int a[ ], int len) where len is the length of array a;
You may assume that the encoded array is correct, i.e., the absolute value of the difference of any two adjacent elements is between 0 and 9 inclusive and the array has at least two elements.
Examples
a is
then function returns
reason
{0, -3, 0, -4, 0} 3344 because abs(0-(-3)=3, abs(-3-0)=3, abs(0-(-4))=4, abs(-4-0)=
because abs(-1-5)=6, abs(5-8)=3, abs(8-17)=9, abs(17-15)=2; the number is negative because the first element of the array is negative
because abs(1-5)=4, remaining digits are the same as previous example; the number is positive because the first element of the array is >=0.
because abs(111-115)=4, abs(115-118)=3, abs(118-127)=9, abs(127-125)=2; the number is positive because the first
{} 0 because there must be at least one 0.
Write a function called isDigitIncreasing that returns 1 if its argument is digit-increasing otherwise, it returns 0.
The signature of the method is int isDigitIncreasing(int n)
Examples
if n is then function returns reason
7 1 because 7 = 7 (here n is 7)
36 1 because 36 = 3 + 33
984 1 because 984 = 8 + 88 + 888
7404 1 because 7404 = 6 + 66 + 666 + 6666
There is one other encoding rule: if the number is negative, the first element of the encoded array must be - 1, so -201 is encoded as {-1, 0, 0, 1, 1, 0, 1}. Note that the 0 digit is represented by no zeros, i.e. there are two consecutive ones!
Write a method named decodeArray that takes an encoded array and decodes it to return the number.
You may assume that the input array is a legal encoded array, i.e., that -1 will only appear as the first element, all elements are either 0, 1 or -1 and that the last element is 1.
If you are programming in Java or C#, the function prototype is int decodeArray(int[ ] a)
If you are programming in C or C++, the function prototype is int decodeArray(int a[ ], int len);
Examples
a is
then function returns
reason
because the digit 0 is represented by no zeros followed by a one.
because the digit 1 is represented by one zero followed by a one.
because the encoding of a negative number begins with a -1 followed by the encoding of the absolute value of the number.
because the encoding of the first 1 is 0, 1, the encoding of each of the four 0s is just a 1 and the encoding of the last 1 is 0, 1.
because each 9 digit is encoded as 0,0,0,0,0,0,0,0,0,1.
This exam consists of three questions. You have two hours in which to complete it.
Write a function named isOnionArray that returns 1 if its array argument is an onion array and returns 0 if it is not.
Write a method named encodeArray that takes an integer as an argument and returns the encoded array.
If you are programming in Java or C#, the function prototype is int[] encodeArray(int n)
If you are programming in C or C++, the function prototype is int * encodeArray(int n);
Hints
Use modulo 10 arithmetic to get digits of number Make one pass through the digits of the number to compute the size of the encoded array. Make a second pass through the digits of the number to set elements of the encoded array to 1.
Examples
The first six (there are over 65,000 of them) systematically increasing arrays are:
{1} {1, 1, 2} {1, 1, 2, 1, 2, 3} {1, 1, 2, 1, 2, 3, 1, 2, 3, 4}
n is then function returns reason
because the digit 0 is represented by no zeros and the representation of each digit ends in one.
because the digit 1 is represented by one zero and the representation of each digit ends in one.
because the encoding of a negative number begins with a -1 followed by the encoding of the absolute value of the number.
because the encoding of the first 1 is 0, 1, the encoding of each of the four 0s is just a 1 and the encoding of the last 1 is 0, 1.
999 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
because each 9 digit is encoded as 0,0,0,0,0,0,0,0,0,1.
Write a function named isSystematicallyIncreasing which returns 1 if its array argument is systematically increasing. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is int isSystematicallyIncreasing(int[ ] a)
If you are programming in C or C++, the function signature is int isSystematicallyIncreasing(int a[ ], int len) where len is the number of elements in the array a.
Examples
a is
then function returns
reason
because 1 is a sequence from 1 to 1 and is the only sequence.
{1, 2, 1, 2, 3} 0 because it is missing the sequence from 1 to 1.
because {1, 3} is not a sequence from 1 to n for any n.
because it contains more than one sequence from 1 to 2.
because it is "backwards", i.e., the sequences from 1 to n are not ordered by increasing value of n
because the sequence {1, 2} is missing (it should precede {1, 2, 3})
Recall that a prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself.
Write a method named isFactorialPrime which returns 1 if its argument is a factorial prime number, otherwise it returns 0.
The signature of the method is int isFactorialPrime(int n)
Examples
if n is
then function returns
reason
2 1 because 2 is prime and is equal to factorial(1) + 1
3 1 because 3 is prime and is equal to factorial(2) + 1
If you are programming in Java or C#, the method signature is int[] computeHMS(int seconds);
If you are programming in C or C++, the method signature is int * computeHMS(int seconds);
The returned array has 3 elements; arr[0] is the hours, arr[1] is the minutes and arr[2] is the seconds contained within the seconds argument.
Recall that there are 3600 seconds in an hour and 60 seconds in a minute. You may assume that the numbers of seconds is non-negative.
Examples
If seconds
is
then function returns
reason
because 3735 = 1 *3600 + 2 *60 + 15. In other words, 3,735 is the number of seconds in 1 hour 2 minutes and 15 seconds
380 {0, 6, 20} because 380 = 0 *3600 + 6 *60 + 20
3650 {1, 0, 50} because 3650 = 1 *3600 + 0 *60 + 50
55 {0, 0, 55} because 55 = 0 *3600 + 0 *60 + 55
0 {0, 0, 0} because 0 = 0 *3600 + 0 *60 + 0
If you are programming in Java or C#, the function signature is int isMartian(int[ ] a)
If you are programming in C or C++, the function signature is int isMartian(int a[ ], int len) where len is the number of elements in the array a.
There are two additional requirements.
a is then function returns reason
There is one 1 and zero 2s, hence the number of 1s is greater than the number of 2s. Also, no adjacent elements have the same value (1 does not equal 3)
There are five 1s and four 2s, hence the number of 1s is greater than the number of 2s. Also, no two adjacent elements have the same value.