Week 06 practice Get familiar with functions and arrays Note: In - - PDF document

week 06 practice
SMART_READER_LITE
LIVE PREVIEW

Week 06 practice Get familiar with functions and arrays Note: In - - PDF document

CS31: Introduction to Computer Science I Week 06 practice Get familiar with functions and arrays Note: In this practice, you can assume all the parameters passed into these functions are valid (i.e., size is greater than 0, array is large


slide-1
SLIDE 1

1

CS31: Introduction to Computer Science I

Week 06 practice

Get familiar with functions and arrays Note: In this practice, you can assume all the parameters passed into these functions are valid (i.e., size is greater than 0, array is large enough)

Problem 1

Implement getAverage() function.

double getAverage(double a[], int n) { }

Have you tried the following test cases:

a = {1.0, 2.0, 3.0}, n = 3?

Problem 2

Implement getVariance() function. The definition of variance is:

double getVariance(double a[], int n) { }

Have you tried the following test cases:

a = {1.0, 2.0, 3.0}, n = 3?

slide-2
SLIDE 2

2

Problem 3

Implement countNumNegativeNumbers() function, which returns the amount of negative numbers in an array.

int countNumNegativeNumbers(int a[], int n) { }

Have you tried the following test cases:

a = {5, 2, -1, 6, 3, -7, -4}, n = 7? a = {0}, n = 1?

Problem 4

Implement getMinValue() function, which returns the minimum value in the array

int getMinValue(int a[], int n) { }

Have you tried the following test cases:

a = {1, 2, 3, 4}, n = 4? a = {4, 3, 2, 1}, n = 4? a = {2, 5, 9, 1, 3}, n = 5?

Problem 5

Implement getGreatCommonDivisor() function. Note that the parameters a and b are guaranteed to be positive.

int getGreatCommonDivisor(int a, int b) { }

slide-3
SLIDE 3

3 Have you tried the following test cases:

a = 25, b = 100? a = 18, b = 48? a = 1, b = 1?

Problem 6

Implement getGreatCommonDivisorV2() function. The difference is that you are going to get an array of integers instead of just 2 numbers.

int getGreatCommonDivisorV2(int a[], int n) { }

Have you tried the following test cases:

a = {25, 50, 100}, n = 3? a = {48, 18, 54, 66}, n = 4? a = {10, 10}, n = 2? a = {1000}, n = 1?

Problem 7

Implement isArrayNonDecreasing() function.

bool isArrayNonDecreasing(int a[], int n) { }

slide-4
SLIDE 4

4 Have you tried the following test cases:

a = {20, 30, 40}, n = 3? a = {40, 30, 20, 10}, n = 4? a = {23, 59, 42}, n = 3? a = {10, 10, 10}, n = 3? a = {1000}, n = 1?

Problem 8

Implement isSubArray() function to test if a2 is a subarray of a1. X is a subarray of Y if a (contiguous) segment of array Y is identical to array X.

bool isSubArray(int a1[], int n1, int a2[], int n2) { }

Have you tried the following test cases:

a1 = {20, 30, 40}, n1 = 3, a2 = {20, 40}, n2 = 2? a1 = {20, 30, 40}, n1 = 3, a2 = {20, 30}, n2 = 2? a1 = {20, 30}, n1 = 2, a2 = {20, 30, 40}, n2 = 3? a1 = {20, 30, 40}, n1 = 3, a2 = {40, 30, 20}, n2 = 3? a1 = {20, 30, 40}, n1 = 3, a2 = {20}, n2 = 1?

Problem 9

Implement isSubSequence() function to test if a2 is a subsequence of a1. X is a subsequence of Y if we delete some elements from array Y, without changing the

  • rder of the remaining values, are identical to X.
slide-5
SLIDE 5

5

bool isSubSequence(int a1[], int n1, int a2[], int n2) { }

Have you tried the following test cases:

a1 = {20, 30, 40}, n1 = 3, a2 = {20, 40}, n2 = 2? a1 = {20, 30, 40}, n1 = 3, a2 = {20, 30}, n2 = 2? a1 = {20, 30}, n1 = 2, a2 = {20, 30, 40}, n2 = 3? a1 = {20, 30, 40}, n1 = 3, a2 = {40, 30, 20}, n2 = 3? a1 = {20, 9, 30, 40, 8}, n1 = 5, a2 = {20, 30, 40}, n2 = 3? a1 = {20, 30, 40}, n1 = 3, a2 = {20}, n2 = 1?

slide-6
SLIDE 6

6

Problem 10

Implement sort() function to rearrange elements in the array in a non-decreasing

  • rder.

void sort(int a[], int n) { }

Have you tried the following test cases:

a = {20, 30, 40}, n = 3? a = {40, 30, 20}, n = 3? a = {20, 40, 30}, n = 3? a = {1, 9, 2, 8, 3, 7, 4, 6, 5}, n = 9? a = {1000}, n = 1?