Difference between revisions of "More With Arrays Review Questions and Exercises"

From CompSciWiki
Jump to: navigation, search
Line 15: Line 15:
 
#Why can it be useful to overwrite the default values when working with partially filled arrays?
 
#Why can it be useful to overwrite the default values when working with partially filled arrays?
 
#What purpose does using a counter to count the number of filled index values serve?
 
#What purpose does using a counter to count the number of filled index values serve?
 +
 +
[[More with Arrays Solutions|Solutions]]
  
 
===Arrays of Strings===
 
===Arrays of Strings===

Revision as of 23:14, 5 December 2007

COMP 1010 Home > More With Arrays > Review Questions and Exercises


{{{Body}}}

Review Questions

Passing Arrays using Methods

  1. What do you have add to the method parameter if you are passing it an array instead of a single variable?
  2. How would you get the length of a Character Array?
  3. How would you get the length of a String?
  4. How would you get the length of a String Array?

Solutions

Working with Partially Filled Arrays

  1. Why can it be useful to overwrite the default values when working with partially filled arrays?
  2. What purpose does using a counter to count the number of filled index values serve?

Solutions

Arrays of Strings

Searching Arrays

  1. Does the array have to be sorted to use a linear search?
  2. Does the array have to be sorted to use a binary search?
  3. Which algorithm is more efficient: linear search, or binary search?
  4. Referring to the "phone book" example, which search algorithm uses the "cut the phone book in half" method - linear or binary search?

https://webmail.cs.umanitoba.ca/mediawiki/index.php?title=More_With_Arrays_Review_Questions_and_Exercises&action=edit&section=1 Editing More With Arrays Review Questions and Exercises (section) - CompSciWiki Solutions

Sorting Arrays

Parallel Arrays

  1. How are parallel arrays tied together?
  2. When sorting a parallel array, should you use the same method as for a regular array?

Solutions

Exercises

Passing Arrays using Methods

Write a small method that accepts an array of characters, converts the characters into their ascii equivalent, then returns those values in an array of integers.

Solution

Apply the Binary Search

Modify the binarySearch() algorithm to keep count of how many elements the algorithm checks to find the desired element. Print out each checked element's value (in other words, the value compared with the search value.)

Solution

Work with an Array of Strings

Create a program that uses an array of Strings to print out the names of five major Canadian cities. For simplicity, the names of these cities may be hard-coded (eg. you don't need to get user input). Bonus points for using other structures in Java to make things even easier. (Just kidding.)


Solution

Parallel Arrays

Write a program that uses a set of parallel arrays to store the name, age and addresses of 5 people you know (Just hard-code these). Have the program. Have the program do the following printouts separately: name and age, age and address and name and address.