A5 Arrays - sheerazwalid/COMP-I GitHub Wiki

Assignment Folder

Place source code files for this assignment in a folder named arrays within your Cloud9 workspace. The files should be named as ex1.cpp for exercise 1, ex2.cpp for exercise 2, and so on.

Exercise 1: Finding the maximum value (10 points)

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

Provide an implementation of the maximum 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: Counting occurrences (10 points)

int countOccurrences(int a[], int len, int k);

Implement the function countOccurrences whose declaration appears above. The first argument of the function is an array a of integers, the second argument len is the number of elements in the array, and the third argument is an integer k. The function returns the number of times k occurs in a.

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

Exercise 3: Two-dimensional arrays (10 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 4: Identical Arrays (10 points)

bool areIdentical(int a[], int b[], int len);

Write a predicate function that checks whether two arrays 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 arrays are identical; otherwise it returns false.

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

Exercise 5: Unlucky Arrays (10 points)

bool isUnlucky(int a[], int len);

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

Exercise 6: Ordered Arrays (10 points)

bool isStrictlyIncreasing(int a[], int len);

Write a predicate function called isStrictlyIncreasing that checks whether an array 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 (-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.

Hint: Compare a[i] with a[i + 1] and remember to stop at i = n - 1 so you don't access a value past the end of the array.

Submit Your Work

Submit your work by sending the url of your Cloud9 workspace to the teaching assistant and CC the instructor. The subject line of your email should be 201 Assignment: Arrays.

Example quiz questions

  1. The above problems may appear on a quiz.

  2. Implement a function that counts the number of negative integers in a two-dimensional array with 100 rows and 100 columns. The function takes a two-dimensional array of int and returns the number of negative numbers in it. A declaration of the function is shown below.

int countNegatives(int a[100][100])Íž
  1. The function minValue given below is supposed to return the smallest value that appears in an array; however, there is a logic error in the code. Write test code that exposes the error. Your test code should use the assert function to terminate the program.
int minValue(int a[], int len) {
    int smallest = 0;
    for (int i = 0; i < len, ++i) {
        If (a[i] < smallest) smallest = a[i];
    }
    return smallest;
}
  1. Provide an implementation of the maximum function whose declaration is shown below. 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.
int maximum(int a[], int numberOfElements)
  1. Implement the function countOccurrences whose declaration appears below. The first argument of the function is an array a of integers, the second argument len is the number of elements in the array, and the third argument is an integer k. The function returns the number of times k occurs in a.
int countOccurrences(int a[], int len, int k)
  1. 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.
bool isAllZeros(int a[50][100])
  1. Write a function that checks whether two arrays are identical (contain exactly the same elements in the same order). A declaration of the function is shown below. The function returns true if the two arrays are identical; otherwise it returns false.
bool areIdentical(int a[], int b[], int len)
  1. Write a function called isStrictlyIncreasing that checks whether an array of integers contains values that are in strictly increasing order. A declaration of the function is shown below. The function returns true if the elements are in strictly increasing order; otherwise it returns false. For example, it will return true for (-2, 4, 5, 6, 8) and it will return false for (3, 4, 6, 6, 9).
bool isStrictlyIncreasing(int a[], int len)
  1. Implement a function that determines if an array of int is lucky. The function returns true if the array passed into it contains at least one 7 and no 13's; otherwise it returns false. A declaration of the function is shown below.
bool isLucky(int a[], int len)
  1. Implement a function that determines if an array of int is lucky. The function returns true if the array passed into it contains more 7's than 13's; otherwise it returns false. A declaration of the function is shown below.
bool isLucky(int a[], int len)
  1. Implement a function that computes the total sum of all values in a 2-dimensional array with 100 rows and 100 columns. The function takes a two-dimensional array of int and returns the total sum of all its values. A declaration of the function is shown below.
int total(int a[100][100])