CS110 Lab 14

Introduction to Arrays







PART I

  1. Write a definition for an array called nums that will store 4 integers. Initialize this array so that all elements are set to 0.
  2. Write a definition for an array called realNums that will store fivefloating-point values. Write additional statements that will assign the value -12.999 to the first and last element in this array.
  3. Write a definition for an array called letters that will store 200 characters. Write an additional statement that will assign the value 'H' to the 89th element in this array.
  4. Write a definition for an array called grades that will store the grades of 25 students.



PART II




    For the following questions, refer to the definitions below.

    final int MAX = 10;
    int [] sample = {2, 3, 5, 1, 8, 4, 9, -3, 6, 3};
    int x;





  1. What value is x assigned in the code below?
    x = sample[0];


  2. What value is x assigned in the code below?
    x = sample[MAX - 1];


  3. What value is x assigned in the code below?
    x = sample[3] * sample[5];


  4. What value is x assigned in the code below?
    x = 2 *sample[2] - 3 * sample[7];


  5. What does the array sample look like after the following code segment is executed?
    sample[0] = sample[MAX - 1];
    sample[MAX - 3] = 7;


  6. What does the array sample look like after the following code segment is executed?
    int i = 0;
    sample[i] = sample[i + 1];


  7. What does the array sample look like after the following code segment is executed?
    for (int i = 0; i < sample.length; i++)
    sample[i]++;


  8. What does the array sample look like after the following code segment is executed?
    for (int i = 0; i < MAX - 2; i++)
    sample[i] = sample[i + 1];


  9. Write a segment of code that displays all the values in sample in reverse. Assume you don't know what the stored values in the array are.

  10. Write a segment of code that replaces all odd integer values with the number 7. Assume you don't know what the stored values in the array are.

  11. Write a segment of code that finds the smallest number in the sample array. Assume you don't know what the stored values in the array are.

  12. Write a segment of code that requires the user to input a list of 10 valid test scores in sample. Reject scores that are outside the valid range of 0-100. Compute the average test score and display all the scores that are above the average.




PART III Passing Arrays to methods




Show the output produced for the following programs.
NOTE: Arrays.toString(arr) is an easy way to display the contents of an integer array.








PART IV Two-dimensional Arrays

A two-dimensional array is a collection of data in a table, with rows and columns.

Example 1: An array containing 5 rows with 6 columns.
int [][] myTable = new int [5][6];

Example 2: This example shows that a two-dimensional array is actually an array containing arrays. The result of the code below is an array containing 3 rows with 2 columns.
String [][] dictionary =
      {
        {"pumpkin pie", "A dessert served on Thanksgiving"},
        {"FF0000", "The HEX 24-bit value equivalent to red"},
        {"pencil", "A tool for writing"}
      };



Exercises:
  1. Write a method makeSmallest() that receives a two-dimensional array containing integers. Create and return an array containing the smallest number in each row.
  2. Write a method makeAverages() that receives a two-dimensional array containing integers. Create and return an array containing the averages of the rows.
  3. Write a method matrixMultiply() that receives two integer matrices. Return the result of matrix multiplication.