PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
Use the array [-4, -2, -7, -1] with n = 4. A single for loop can inspect each index, but the loop does not decide what the answer means. You must give each result variable one job before the first iteration. A running sum adds every value, while an even-value counter increases only when the current value has remainder 0 after division by 2.
int[] a = {-4, -2, -7, -1};
int n = 4;
int sum = 0;
int evenCount = 0;
for (int i = 0; i < n; i++) {
sum = sum + a[i];
if (a[i] % 2 == 0) {
evenCount = evenCount + 1;
}
}| INDEX | VALUE | SUM | EVENCOUNT |
|---|---|---|---|
| 0 | -4 | -4 | 1 |
| 1 | -2 | -6 | 2 |
| 2 | -7 | -13 | 2 |
| 3 | -1 | -14 | 2 |
At index 0, adding -4 changes sum to -4, and -4 is even, so evenCount becomes 1. At index 1, sum becomes -6 and the counter becomes 2 because -2 is also even. At index 2, sum becomes -13, but the counter stays 2 because -7 is odd. At index 3, sum becomes -14 and the counter stays 2 because -1 is odd.
What are the values of sum and evenCount after indices 0-2 of [-4, -2, -7, -1] have been processed?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
sum = 0 is correct before you have added any values because zero is the additive identity. Adding a value to zero gives that value without changing it. evenCount = 0 is also correct because no even values have been counted before the loop starts. These variables represent a total and a number of matches, so zero describes their empty starting state.
int sum = 0;
int evenCount = 0;
int max = 0;
for (int i = 0; i < 4; i++) {
sum = sum + a[i];
if (a[i] % 2 == 0) {
evenCount = evenCount + 1;
}
if (a[i] > max) {
max = a[i];
}
}max = 0 is not a valid starting state for this array. Every value in [-4, -2, -7, -1] is less than 0, so the comparison a[i] > max never succeeds. The loop therefore produces max = 0, even though 0 is not present in the array. The sum still becomes -14 and the even-value count still becomes 2 because their starting values have meanings that do not depend on the array containing zero.
For a maximum, the starting value should already be a value that the loop has seen. In this array, set max = a[0], which is -4, then start the comparison loop at index 1. The first element has already supplied the initial answer, so the loop only needs to test the remaining values.
int max = a[0];
for (int i = 1; i < 4; i++) {
if (a[i] > max) {
max = a[i];
}
}| INDEX COMPARED | VALUE | MAX BEFORE | MAX AFTER |
|---|---|---|---|
| 1 | -2 | -4 | -2 |
| 2 | -7 | -2 | -2 |
| 3 | -1 | -2 | -1 |
The initial maximum is -4 from index 0. At index 1, -2 is greater, so max becomes -2. At index 2, -7 is not greater, so max stays -2. At index 3, -1 is greater, so max becomes -1. At every point, max is one of the values already seen, which makes it impossible for the answer to be an invented value such as 0.
Replace max = 0 and the matching loop start so the maximum is found correctly for [-4, -2, -7, -1].
int max = 0;
for (int i = 0; i < 4; i++) {
if (a[i] > max) {
max = a[i];
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The three calculations can share one index-based loop because each one has its own update rule. sum changes for every value, evenCount changes only when the value is even, and max changes only when the value is greater than the current maximum. The updates are independent, so changing one result does not replace or reset another.
int[] a = {-4, -2, -7, -1};
int sum = 0;
int evenCount = 0;
int max = a[0];
for (int i = 0; i < 4; i++) {
sum = sum + a[i];
if (a[i] % 2 == 0) {
evenCount = evenCount + 1;
}
if (a[i] > max) {
max = a[i];
}
}| RESULT VARIABLE | MEANING | FINAL VALUE |
|---|---|---|
| sum | total of all four values | -14 |
| evenCount | number of even values | 2 |
| max | greatest value in the array | -1 |
The initial states are sum = 0, evenCount = 0, and max = a[0]. After all four indices are processed, sum is -14 because -4 + -2 + -7 + -1 equals -14. evenCount is 2 because -4 and -2 are even. max is -1 because it is greater than -4, -2, and -7. Each final answer follows from the meaning of its own variable.