Skip to main content

Section 10.12 Arrays-WE4-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.12.1

Exercises Exercises

1.
Q37: Given the following code, what is the value in sum after execution?
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
int sum = 0;
for (int k = 0; k<alpha.length; k+=2)
    sum = sum + alpha[k];
2.
Q38: Given the following code, what is the value in sum after execution?
int [] beta = new int [5];
int sum = 0;
for (int k = 0; k<beta.length; k++)
    beta[k] = 2 * k + 1;
int x = 3;
for (int k = 1; k<beta.length; k++)
    sum += beta[k];
You have attempted of activities on this page.