Skip to main content

Section 1.4 Homework 4 -- Arrays & Strings

The goal of this assignment is to give you some practice writing simple C++ programs and to check your knowledge of some of the main concepts from:Β  arrays and strings.

Exercises Exercises

1.

Which of the following is the correct way to declare an array called ’my_bools’ with the capacity to store 20 values of type bool?
  • bool my_bools[20];
  • bool[20] my_bools;
  • bool[] my_bools = new bool[20];
  • bool[] = my_bools[20]

2.

What is the INDEX of the FIRST element of an array of length 5 in C++?
  • 0
  • 1
  • -1
  • 5

3.

What is the INDEX of the LAST element of an array of length 5 in C++?
  • 4
  • 5
  • -1
  • 6

4.

Which of the following correctly accesses the fourth element stored in ’arr’, an array with ten elements?
  • arr[3];
  • arr[4];
  • arr(3);
  • arr(4);

5.

6.

The following code has an error -- it declares an array to store 10 integers and attempts to write to offset 10:
BAD CODE
int arr[10];
for (int i = 0; i <= 10; i++) { arr[i] = i * i ; }
When the program which includes this code is built, at what stage of the build process will an error message be generated?
  • during preprocessing
  • during compilation
  • during linking
  • none of the above

7.

Which of the following is the correct way to declare a two-dimensional array called "my_grid" with the capacity to store a 3 by 5 table of integer values?
  • int my_grid[3][5];
  • int[3][5] my_grid;
  • int[][] my_grid = new int[3][5];
  • int[][] = my_grid[3][5];

8.

Which of the following correctly declares a function ’my_fun’ which takes a 2-dimensional array of bool values and returns an integer?
  • int my_fun( bool x[ ][ 20 ] );
  • int my_fun( bool x[ 10 ][ 20 ] );
  • int my_fun( bool x[ ][ ] );
  • int my_fun( bool x[ ] );
  • int my_fun( bool array x );

9.

Write a function array_sum that takes two arguments: an integer array called ’nums’ and an integer called ’size’ representing the number of values stored in the array. The function accumulates the sum of the values in the ’nums’ array and returns that sum. If size < 1, the function returns 0.
An outline of the function definition is provided to help you get started.

10.

Write a function array_max that takes one argument: a two-dimensional array of integers ’arr’ which will always have 3 rows and 5 columns. The functionΒ returns the largest element in the array.

11.

Write a function unique_elements that takes an array of integers ’arr’ and an integer ’size’ (the size of ’arr’). The function returns true if there are no repeated elements in arr, otherwise it returns false.
Note: If size < 2, the function returns true since you need at least 2 elements to have a repeated element.

12.

Write a function count_evens that takes an array of integers ’arr’ and an integer ’n’. The function returns an integer indicating the number of even elements among the first n elements of ’arr’.

13.

Write a function count_occurrences that takes an array of integers ’arr’, an integer ’size’, and an integer ’n’. The function returns an integer indicating the number of occurrences of the value ’n’ among the first size-many elements in ’arr’.
For example, if arr = [3, 3, 5, 3, 3, 2, 8], then count_occurrences(arr, 3, 5) returns 1 since there is one ’5’ in [3, 3, 5], and count_occurrences(arr, 6, 3) returns 4 since there are four 3’s in [3, 3, 5, 3, 3, 2].

14.

Write a function last_index that takes an array of integers ’arr’, an integer ’size’, and an integer ’n’. The function returns an integer indicating the LARGEST index i such that i < size and arr[i] equals n.Β  If there are no occurrences of ’n’ among the first size-many elements of ’arr’, the function returns -1 to indicate "n not found".
For example, if arr = [3, 3, 5, 3, 3, 2, 8], then last_index(arr, 7, 3) returns 4 since 4 is the largest index i (i < 7) such that arr[i] equals 3.

15.

The ’cin’ method is really an OBJECT.Β  What is the data type of ’cin’?
  • output writer
  • input reader
  • output stream
  • input stream

16.

17.

Suppose we have included the string class header file (#include <string>), declared a variable ’s’ of type string, and assigned a value to s. For example:
string s = "Marie Curie's century-old notebooks are still radioactive.";
Which of the following function calls will correctly return the number of characters in ’s’?
  • s.length();
  • s.size();
  • s.num_chars();
  • s.len();

18.

Suppose ’mystring’ is been declared to have type ’string’ and has been initialized.
Suppose ’i’ is an integer, where i >= 0 and i is less than the length of mystring.
True, or false?
mystring[i] returns the char in ’mystring’ at index ’i’.
  • true
  • false

19.

20.

Write a complete C++ program that reads a line of input and prints it out; however, if the string that was read is more than 20 characters long (including whitespace characters), the message "too long" is printed instead.
The end of the input is signaled by the user hitting the "enter" key, which creates a ’\n’ character at the end of the input. Note that the newline character ’\n’ is discarded, it does not become part of the string which is read.

21.

The ’find’ method in the string class takes a substring and a position in the original string and finds the first occurrence of the substring, starting from the given index.Β  If the substring is not found, what is returned by the ’find’ method?
  • string::npos
  • -1
  • nothing is returned, it creates an error message
  • None

22.

23.

24.

Write a complete C++ program that reads from a comma-separated string of name values (entered by standard input) and prints each name on its own line. The end of the CSV input will be signaled by two commas in a row.
For example, if the CSV-formatted input string is "Aaron Rodgers,J. J. Watt,Luke Kuechly,," then the output should be:
Aaron Rodgers
J. J. Watt
Luke Kuechly

25.

Write a function second_half that has one argument, ’input_string’, of type string and returns a string that is the second half of ’input_string’. Whether ’input_string’ has an even length, 2n, or an odd length, 2n+1, the return string will always have length n. (The center character is not included in the second half for odd-length strings.)
  • second_half("Piggy") returns "gy" (input length is 2*2 + 1, output length is 2).
  • second_half("COFFEE MUG") returns "E MUG" (input length is 2 * 5, output length is 5).
You have attempted of activities on this page.