Docsity

Network security with fortigate, Study notes of Network security

Toggle sidebar
bg1
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.
1. Write a function named largestAdjacentSum that iterates through an array computing the sum of
adjacent elements and returning the largest such sum. You may assume that the array has at least 2
elements.
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+3
{18, -12, 9, -10} 6 because 18-12 is larger than -12+9 or 9-10
{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
2. The number 198 has the property that 198 = 11 + 99 + 88, i.e., if each of its digits is concatenated twice
and then summed, the result will be the original number. It turns out that 198 is the only number with this
property. However, the property can be generalized so that each digit is concatenated n times and then
summed. For example, 2997 = 222+999+999+777 and here each digit is concatenated three times. Write a
function named checkContenatedSum that tests if a number has this generalized property.
The signature of the function is
int checkConcatenatedSum(int n, int catlen) where n is the number and catlen is the number of times to
concatenate each digit before summing.
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
3. Define an m-n sequenced array to be an array that contains one or more occurrences of all the integers
between m and n inclusive. Furthermore, the array must be in ascending order and contain only those
integers. For example, {2, 2, 3, 4, 4, 4, 5} is a 2-5 sequenced array. The array {2, 2, 3, 5, 5, 5} is not a 2-5
sequenced array because it is missing a 4. The array {0, 2, 2, 3, 3} is not a 2-3 sequenced array because the
0 is out of range. And {1,1, 3, 2, 2, 4} is not a 1-4 sequenced array because it is not in ascending order.
Write a method named isSequencedArray that returns 1 if its argument is a m-n sequenced array,
otherwise it returns 0.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Network security with fortigate and more Study notes Network security in PDF only on Docsity!

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.

  1. Write a function named largestAdjacentSum that iterates through an array computing the sum of adjacent elements and returning the largest such sum. You may assume that the array has at least 2 elements.

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

  1. The number 198 has the property that 198 = 11 + 99 + 88, i.e., if each of its digits is concatenated twice and then summed, the result will be the original number. It turns out that 198 is the only number with this property. However, the property can be generalized so that each digit is concatenated n times and then summed. For example, 2997 = 222+999+999+777 and here each digit is concatenated three times. Write a function named checkContenatedSum that tests if a number has this generalized property. The signature of the function is int checkConcatenatedSum(int n, int catlen) where n is the number and catlen is the number of times to concatenate each digit before summing.

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

  1. Define an m-n sequenced array to be an array that contains one or more occurrences of all the integers between m and n inclusive. Furthermore, the array must be in ascending order and contain only those integers. For example, {2, 2, 3, 4, 4, 4, 5} is a 2-5 sequenced array. The array {2, 2, 3, 5, 5, 5} is not a 2- sequenced array because it is missing a 4. The array {0, 2, 2, 3, 3} is not a 2-3 sequenced array because the 0 is out of range. And {1,1, 3, 2, 2, 4} is not a 1-4 sequenced array because it is not in ascending order.

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.

  1. Allocate an array to hold the prime factors. Do not hard-code the size of the returned array!!
  2. Populate the allocated array with the prime factors. The elements of the array when multiplied together should equal the number.

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. Consider a simple pattern matching language that matches arrays of integers. A pattern is an array of integers. An array matches a pattern if it contains sequences of the pattern elements in the same order as they appear in the pattern. So for example, the array {1, 1, 1, 2, 2, 1, 1, 3} matches the pattern {1, 2, 1, 3} as follows:

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

  1. Define the n-based integer rounding of an integer k to be the nearest multiple of n to k. If two multiples of n are equidistant use the greater one. For example

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

  1. A number n>0 is called cube-powerful if it is equal to the sum of the cubes of its digits.

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.

  1. A number can be encoded as an integer array as follows. The first element of the array is any number and if it is negative then the encoded number is negative. Each digit of the number is the absolute value of the difference of two adjacent elements of the array. The most significant digit of the number is the absolute value of the difference of the first two elements of the array. For example, the array {2, -3, -2, 6, 9, 18} encodes the number 51839 because
    • 5 is abs(2 - (-3))
    • 1 is abs(-3 - (-2))
    • 8 is abs(-2 - 6)
    • 3 is abs(6-9)
    • 9 is abs(9-18)

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.

  1. A number is called digit-increasing if it is equal to n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-increasing because it equals 2 + 22 (here n = 2)

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

  1. An integer number can be encoded as an array as follows. Each digit n of the number is represented by n zeros followed by a 1. So the digit 5 is represented by 0, 0, 0, 0, 0, 1. The encodings of each digit of a number are combined to form the encoding of the number. So the number 1234 is encoded as the array {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. The first 0, 1 is contributed by the digit 1, the next 0, 0, 1 is contributed by the digit 2, and so on.

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.

  1. An onion array is an array that satisfies the following condition for all values of j and k: if j>=0 and k>=0 and j+k=length of array and j!=k then a[j]+a[k] <= 10

Write a function named isOnionArray that returns 1 if its array argument is an onion array and returns 0 if it is not.

  1. An integer number can be encoded as an array as follows. Each digit n of the number is represented by n zeros followed by a 1. So the digit 5 is represented by 0, 0, 0, 0, 0, 1. The encodings of each digit of a number are combined to form the encoding of the number. So the number 1234 is encoded as the array {0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}. The first 0, 1 is contributed by the digit 1, the next 0, 0, 1 is contributed by the digit 2, and so on. 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 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

  1. An array is called systematically increasing if it consists of increasing sequences of the numbers from 1 to n.

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

  1. A positive, non-zero number n is a factorial prime if it is equal to factorial(n) + 1 for some n and it is prime. Recall that factorial(n) is equal to 1 * 2 * ... * n-1 * n. If you understand recursion, the recursive definition is factorial(1) = 1; factorial(n) = nfactorial(n-1). For example, factorial(5) = 1234*5 = 120.

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 C or C++, the function signature is

int areAnagrams(a1 char[ ], a2 char[ ], int len) where len is the length of a1 and a2.

Hint: Please note that "pool" is not an anagram of "poll" even though they use the same

letters. Please be sure that your function returns 0 if given these two words! You can use

another array to keep track of each letter that is found so that you don't count the same

letter twice (e.g., the attempt to find the second "o" of "pool" in "poll" should fail.)

Hint: do not modify either a1 or a2, i.e., your function should have no side effects! If

your algorithm requires modification of either of these arrays, you must work with a copy

of the array and modify the copy!

Examples

if input arrays are return

{'s', 'i', 't'} and {'i', 't', 's'} 1

{'s', 'i', 't'} and {'i', 'd', 's'} 0

{'b', 'i', 'g'} and {'b', 'i', 't'} 0

{'b', 'o', 'g'} and {'b', 'o', 'o'} 0

{} and {} 1

{'b', 'i', 'g'} and {'b', 'i', 'g'} 1

3. The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The first and

second numbers are 1 and after that ni = ni-2 + ni-1, e.g., 34 = 13 + 21. A number in the

sequence is called a Fibonacci number. Write a method with signature int

closestFibonacci(int n) which returns the largest Fibonacci number that is less than or

equal to its argument. For example, closestFibonacci(13) returns 8 because 8 is the

largest Fibonacci number less than 13 and closestFibonacci(33) returns 21 because 21 is

the largest Fibonacci number that is <= 33. closestFibonacci(34) should return 34. If the

argument is less than 1 return 0. Your solution must not use recursion because unless you

cache the Fibonacci numbers as you find them, the recursive solution recomputes the

same Fibonacci number many times.

1. A number n is vesuvian if it is the sum of two different pairs of squares. For example, 50 is

vesuvian because 50 == 25 + 25 and 1 + 49. The numbers 65 (1+64, 16+49) and 85 (4+81,

36+49) are also vesuvian. 789 of the first 10,000 integers are vesuvian.

Write a function named isVesuvian that returns 1 if its parameter is a vesuvian number, otherwise

it returns 0. Hint: be sure to verify that your function detects that 50 is a vesuvian number!

2. Define an array to be one-balanced if begins with zero or more 1s followed by zero or more

non-1s and concludes with zero or more 1s. Write a function named isOneBalanced that returns

1 if its array argument is one-balanced, otherwise it returns 0.

If you are programming in Java or C#, the function signature is

int isOneBalanced(int[ ] a)

If you are programming in C or C++, the function signature is

int isOneBalanced(int a[ ], int len) where len is the number of elements in the array a.

Examples

if a is

then

function

returns

reason

because it begins with three 1s, followed by four non-1s and

ends with one 1 and 3+1 == 4

because the 0 starts another sequence of non-1s. There can be

only one sequence of non-1s.

because there are two sequences of non-1s

({2, 3} and {-18, 26}

because 0 (# of beginning 1s) + 0 (# of ending 1s) =

0 (# of non-1s)

because 0 (# of beginning 1s) + 2 (# of ending 1s) =

2 (# of non-1s)

because 2 (# of beginning 1s) + 0 (# of ending 1s) =

2 (# of non-1s)

because 0 (# of beginning 1s) + 0 (# of ending 1s) !=

6 (# of non-1s)

because 6 (# of beginning 1s) + 0 (# of ending 1s) !=

0 (# of non-1s)

3. The Fibonacci sequence of numbers is 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The first and second

numbers are 1 and after that ni = ni-2 + ni-1, e.g., 34 = 13 + 21. Write a method with signature

int isFibonacci(int n) which returns 1 if its argument is a number in the Fibonacci sequence,

otherwise it returns 0. For example, isFibonacci(13) returns a 1 and isFibonacci(27) returns a 0.

Your solution must not use recursion because unless you cache the Fibonacci numbers as you

find them, the recursive solution recomputes the same Fibonacci number many times.

1. A number n is triangular if n == 1 + 2 +...+j for some j. Write a function

int isTriangular(int n)

that returns 1 if n is a triangular number, otherwise it returns 0. The first 4 triangular

numbers are 1 (j=1), 3 (j=2), 6, (j=3), 10 (j=4).

If you are writing in C or C++, the function signature is

int is235Array(int a[ ], int len) where len is the length of a

Hint: remember that a number can be divisible by more than one number

Examples

In the following: < a, b, c, d > means that the array has a elements that are divisible by 2,

b elements that are divisible by 3, c elements that are divisible by 5 and d elements that

are not divisible by 2, 3, or 5.

if a is return reason

because one element is divisible by 2 (a[0]), one is

divisible by 3 (a[1]), one is divisible by 5 (a[2]) and two

are not divisible by 2, 3, or 5 (a[3] and a[4]). So we

have <1, 1, 1, 2> and 1+1+1+2 == the number of

elements in the array.

because two elements are divisible by 2 (a[0] and a[2]),

two are divisible by 3 (a[1] and a[2]), none are divisible

by 5 and two are not divisible by 2, 3, or 5 (a[3] and

a[4]). So we have <2, 2, 0, 2> and 2 + 2 + 0 + 2 == 6 !=

the number of elements in the array.

because <5, 3, 2, 1> and 5 + 3 + 2 + 1 == 11 != the

number of elements in the array.

because <5, 0, 0, 0> and 5 + 0 + 0 + 0 == 5 == the

number of elements in the array.

because <0, 3, 0, 6> and 0 + 3 + 0 + 6 == 9 == the

number of elements in the array.

because <0, 0, 0, 4> and 0 + 0 + 0 + 4 == 4 == the

number of elements in the array.

because <1, 0, 0, 0> and 1 + 0 + 0 + 0 == 1 == the

number of elements in the array.

because <0, 0, 0, 0> and 0 + 0 + 0 + 0 == 0 == the

number of elements in the array.

because <4, 1, 0, 6> and 4 + 1 + 0 + 6 == 11 == the

number of elements in the array.

  1. Write a method named computeHMS that computes the number of hours, minutes and seconds in a given number of seconds.

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

  1. Define an array to be a Martian array if the number of 1s is greater than the number of 2s and no two adjacent elements are equal. Write a function named isMartian that returns 1 if its argument is a Martian array; otherwise it returns 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.

  1. You should return 0 as soon as it is known that the array is not a Martian array; continuing to analyze the array would be a waste of CPU cycles.
  2. There should be exactly one loop in your solution. Examples

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.