Lab: Arrays

Learning Objectives

Exercise 1: Finding the maximum value (5 points)

int mymaximum(int a[], int numberOfElements);

Provide an implementation of the mymaximum function whose declaration is shown above. The first argument of the function is an array of int and the second argument is the number of ints that are in the array. The function returns the largest int that is in the array.

Write test code that thoroughly tests the function. The test code should use assertions.

Exercise 2: Appending integers (5 points)

void append(vector<int> & v, int n);

Provide an implementation of the function whose declaration is shown above. The first argument of the function is a vector of int and the second argument is the number of consecutive integers to append to the vector starting from 0 and ending at n - 1. The function appends onto the vector v the values 0, 1, 2, ..., n - 1. (Hint: use the push_back function of vector to append these values to v.) (5 points)

Write test code that thoroughly tests the function. The test code should use assertions.

Exercise 3: Appending vectors (5 points)

void appendVector(vector<int> & v, const vector<int> & w);

Provide an implementation of the appendVector function whose declaration is shown above. Both arguments of the function are vectors of int. The function should modify vector v by appending to it all the elements of w. For example, if v = (4, 2, 5) and w = (11, 3), then v will become (4, 2, 5, 11, 3) as a result of calling the function. Hint: the vector class has a function called push_back that appends values passed into it.

Write test code that thoroughly tests the function. The test code should use assertions.

Exercise 4: Vector insertion (5 points)

Suppose a vector v contains a sequence of integers ordered from lowest to highest. For example, v might contain the values 3, 6, 7, 12. Write a function called insert that takes this vector v as a first argument and an integer k as the second argument. The function inserts k into the vector so that it preserves the order of elements. For example, if v were (3, 6, 7, 12) and k were 5, then after calling the function, v would be (3, 5, 6, 7, 12). In your implementation of insert, do not use the insert function of the vector class (v.insert).

Write test code that thoroughly tests the function. The test code should use assertions.

Exercise 5: Counting occurrences (5 points)

int countOccurrences(const vector<int> & v, int k);

Implement the function countOccurrences whose declaration appears above. The first argument of the function is a vector v of integers and the second argument is an integer k. The function returns the number of times k occurs in v.

Write test code that thoroughly tests the function. The test code should use assertions.

Exercise 6: Two-dimensional arrays (5 points)

bool isAllZeros(int a[50][100]);

Implement the function isAllZeros that uses a loop nested inside another loop to determine whether a 2-dimensional array contains only zeros. A declaration of the function is shown above. The array passed into the function will have 50 rows and 100 columns.

Write test code that thoroughly tests the function. The test code should use assertions.

Exercise 7: Identical Vectors (5 points)

bool areIdentical(const vector<int> & a, const vector<int> & b);

Write a predicate function that checks whether two vectors are identical (contain exactly the same elements in the same order). A declaration of the function is shown above. The function returns true if the two vectors are identical; otherwise it returns false.

Write test code that tests every statement in the function. Use assertions in your test code.

Exercise 8: Unlucky Vectors (5 points)

bool isUnlucky(vector<int> & v);

Implement a function that determines if the unlucky number 13 appears in a vector. The function returns true if 13 appears in the vector at least once; otherwise it returns false. Write test code that tests every statement in the function. Use assertions in your test code.

Exercise 9: Ordered Vectors (5 points)

bool isStrictlyIncreasing(const vector<int> & v);

Write a predicate function called isStrictlyIncreasing that checks whether a vector of integers contains values that are in strictly increasing order. A declaration of the function is shown above. The function returns true if the elements are in strictly increasing order; otherwise it returns false. For example, it will return true for v = (-2, 4, 5, 6, 8) and it will return false for (3, 4, 6, 6, 9).

Write test code that tests every statement in the function. Use assertions in your test code.

Exercise 10: Flatten (5 points)

vector<int> flatten(int a[100][200]);

Write a function named flatten that takes a 2-dimensional array of integers with 100 rows and 200 columns and returns a vector that contains all of the array's elements. Copy the values a row at a time. In other words, first copy row 0 into the vector, then row 1, then row 2, and so on. The declaration of flatten is given above.

You don't need to write test code for this problem.