Skip to main content

Section 10.23 Arrays-WE9-P1

Subgoals for Evaluating Arrays.

  1. Set up array from 0 to size-1
  2. Evaluate data type of statements against array
  3. Trace statements, updating slots as you go
    1. Remember assignment subgoals

Subsection 10.23.1

Exercises Exercises

1.
    Q48: What does the following code do?
    int [] alpha = {10, 20, 30, 40, 50};
    int [] beta = new int[alpha.length+1];
    for (int i = 0; i < alpha.length; i++)
       beta[i] = alpha[i];
    beta[beta.length-1] = 42;
    alpha = beta;
    
  • Inserts a new value into the first position of the array
  • Inserts a new value into the middle position of the array
  • Inserts a new value into the last position of the array
  • deletes a value from a specific position of the array
  • finds and deletes a value from the array
2.
    Q49: What does the following code do?
    int target = /*  some value */ ;
    int [] delta = {10, 20, 30, 40, 50, 60, 70, 80, 90};
    boolean found = false;
    int i, j;
    for (i = 0; i < delta.length && !found; i++)
       if (delta[i] == target)
          found = true;
    if (found) {
       for (j = i-1; j < delta.length-1; j++)
          delta[j] = delta[j+1];
       delta[delta.length-1] = -999;
    }
    
  • Inserts a new value into the first position of the array
  • Inserts a new value into the middle position of the array
  • Inserts a new value into the last position of the array
  • deletes a value from a specific position of the array
  • finds and deletes a value from the array
3.
    Q50: What does the following code do?
    int [] alpha = {10, 20, 30, 40, 50};
    int [] beta = new int[alpha.length+1];
    for (int i = 0; i < alpha.length; i++)
       beta[i] = alpha[i];
    beta[0] = 99;
    alpha = beta;
    
  • Inserts a new value into the first position of the array
  • Inserts a new value into the middle position of the array
  • Inserts a new value into the last position of the array
  • deletes a value from a specific position of the array
  • finds and deletes a value from the array
4.
    Q51: What does the following code do?
    int pos = /* some value */;
    int [] rho = {10, 20, 30, 40, 50, 60, 70, 80, 90};
    for (int m = pos; m < rho.length-1; m++)
       rho[m] = rho[m+1];
    rho[rho.length-1] = -999;
    
  • Inserts a new value into the first position of the array
  • Inserts a new value into the middle position of the array
  • Inserts a new value into the last position of the array
  • deletes a value from a specific position of the array
  • finds and deletes a value from the array
5.
    Q52: What does the following code do?
    int [] alpha = {10, 20, 30, 40, 50};
    int [] gamma = new int[alpha.length+1];
    for (int i = 0; i < alpha.length; i++)
       gamma[i] = alpha[i];
    gamma[gamma.length/2] = 11;
    alpha = gamma;
    
  • Inserts a new value into the first position of the array
  • Inserts a new value into the middle position of the array
  • Inserts a new value into the last position of the array
  • deletes a value from a specific position of the array
  • finds and deletes a value from the array
You have attempted of activities on this page.