Chapter 8 Exercise Set 2: Doctest Exercises¶
Write a function named
sum_array(int* arr, int size)that returns the sum of all the elements in the array.
TEST_CASE("sum_array adds up all elements in the array") { int a[] = {1, 2, 3, 4, 5}; CHECK(sum_array(a, 5) == 15); int b[] = {0}; CHECK(sum_array(b, 1) == 0); int c[] = {-3, 7, -2}; CHECK(sum_array(c, 3) == 2); }
Write a function
fill(int* arr, int size, int value)that sets every element of an array of integers tovaluepassed in as an argument.TEST_CASE("fill sets every element") { int a[3] = {1, 2, 3}; fill(a, 3, 0); CHECK(a[0] == 0); CHECK(a[1] == 0); CHECK(a[2] == 0); int b[10]; fill(a, 10, 42); for (int i = 0; i < 10; i++) CHECK(a[i] == 42); }
Write a function
copy_array(int* dest, int* src, int size)that copiessizeelements fromsrctodest. Use pointer syntax to walk both arrays.TEST_CASE("copy_array copies all elements") { int src[] = {10, 20, 30, 40}; int dest[4]; copy_array(dest, src, 4); for (int i = 0; i < 4; i++) CHECK(dest[i] == src[i]); // ensure src was not modified CHECK(src[0] == 10); CHECK(src[3] == 40); }
Write a function
reverse_in_place(int* arr, int size)that reverses all the elements in an array in place, without allocating any new memory. (Hint: Use two pointers, one from each end, and swap toward the middle.)TEST_CASE("reverse_in_place reverses an array") { int a[] = {1, 2, 3, 4, 5}; reverse_in_place(a, 5); CHECK(a[0] == 5); CHECK(a[1] == 4); CHECK(a[2] == 3); CHECK(a[3] == 2); CHECK(a[4] == 1); int b[] = {1, 2, 3, 4}; reverse_in_place(b, 4); CHECK(b[0] == 4); CHECK(b[1] == 3); CHECK(b[2] == 2); CHECK(b[3] == 1); }
Write a function
int* make_squares(int n)that returns a pointer to a dynamically allocated array ofnsquares from2squared ton + 1squared. The caller is responsible for preventing memory leak.TEST_CASE("make_squares allocates and fills a new array") { int* squares = make_squares(5); CHECK(squares[0] == 4); CHECK(squares[1] == 9); CHECK(squares[2] == 16); CHECK(squares[3] == 25); CHECK(squares[4] == 36); delete[] squares; int* one = make_squares(1); CHECK(one[0] == 4); delete[] one; }
Write a function
int* scaled_copy(int* src, int size, int factor)that returns a pointer to a dynamically allocated array ofsizeintegers whose elements are the product of the corresponding elements in thesrcarray andfactor.TEST_CASE("scaled_copy returns a scaled copy") { int src[] = {1, 2, 3}; int* out = scaled_copy(src, 3, 10); CHECK(out[0] == 10); CHECK(out[1] == 20); CHECK(out[2] == 30); CHECK(src[0] == 1); // original untouched delete[] out; }
Write a function
int count_words_c(char* s)that takes a C-string as an argument and returns the number of space separated “words” in the string.TEST_CASE("count_words_c counts words in a C string") { CHECK(count_words_c("") == 0); CHECK(count_words_c("Word!") == 1); CHECK(count_words_c("Thing1 and Thing2") == 3); CHECK(count_words_c(" leading and trailing ") == 3); CHECK(count_words_c("This is the song that never ends.") == 7); }
Write a function
void grow(int*& arr, int old_size, int new_size)that takes a reference to a pointer to an array and sets it to point to a new larger array with the same values in the firstold_sizevalues and0s in the newly added elements.TEST_CASE("grow reallocates and copies contents") { int* a = new int[3]{1, 2, 3}; grow(a, 3, 5); CHECK(a[0] == 1); CHECK(a[1] == 2); CHECK(a[2] == 3); CHECK(a[3] == 0); // requires new int[new_size]() CHECK(a[4] == 0); delete[] a; }