Programming Practice: Arrays
Arrays of integers
Suppose elts is an array of integer values that has been declared, created,
and filled.
- Write a code fragment that computes the sum of the elements in
elts.
- Write a code fragment that counts the number of times the value 7 appears
in the array.
- Write a code fragment that finds the index of the first 3 in the array.
Arrays of strings
Suppose strArray is an array of String objects that has been declared, created, and filled.
- Write a code fragment that computes the sum of the lengths of the strings in
strArray. For example, if strArray contains { "cat", "dog", "ferret" }
then the sum of the lengths of the strings is 3 + 3 + 6 = 12.
- Write a code fragment that finds the index of the longest string in strArray.
What happens in your solution if
- there's a tie (i.e., if more that one string has the greatest length)?
Which index do you find (the index of the first string with the greatest length,
the index of the last string with the greatest length, or ...)?
- there are empty strings ("") in strArray?
- there are null elements in strArray?