Sum After Even

🎯 Array Hurdle

Sum After Even

Difficulty: ⚫◯◯◯

Tags: array, 2d-array, traversal, parity

Hurdle: Passing this question is sufficient to pass the arrays hurdle.

Description

Write a function sum_after_even that is given a two-dimensional array of integers with num_rows rows and NUM_COLS columns, where NUM_COLS is #defined to 5.

The function should return a total sum calculated from the position of the first even number in each row.

For each row, the function must:

  1. Find the first even number in that row.
  2. Add every value that appears after that first even number to the total.
  3. If a row has no even numbers, it contributes 0 to the total.
  4. If the first even number is the very last element in the row, that row contributes 0 to the total.

Note that 0 is even, and that a negative number can be even (-2) or odd (-3).

Examples

Example 1

[1, 3, 2, 5, 7],
[1, 1, 1, 1, 1]

Output: 12

Explanation:

  • Row 0: the first even number is 2 at index 2. The values after it are 5 and 7, which sum to 12.
  • Row 1: there are no even numbers, so this row contributes 0.
  • Total: 12 + 0 = 12.

Example 2

[4, 1, 1, 1, 1],
[3, 5, 7, 9, 2]

Output: 4

Explanation:

  • Row 0: the first even number is 4 at index 0. The values after it are 1, 1, 1 and 1, which sum to 4.
  • Row 1: the first even number is 2 at index 4. There are no values after index 4, so this row contributes 0.
  • Total: 4 + 0 = 4.

Example 3

[16, 12, 8,  3, 1],
[ 2,  0, 10, 1, 4],
[ 1,  1,  1, 13, 1],
[ 5,  5,  5,  8, 2],
[ 5,  5,  5,  5, 5]

Output: 41

Explanation:

  • Row 0: first even is 16 at index 0, so 12 + 8 + 3 + 1 = 24.
  • Row 1: first even is 2 at index 0, so 0 + 10 + 1 + 4 = 15.
  • Row 2: no even numbers, so 0.
  • Row 3: first even is 8 at index 3, so 2.
  • Row 4: no even numbers, so 0.
  • Total: 24 + 15 + 0 + 2 + 0 = 41.

Example 4

[-3, -5, 4, 1, 1]

Output: 2

Explanation: Both -3 and -5 are odd, so the first even number is 4 at index 2. The values after it are 1 and 1, which sum to 2.

Function Signature

int sum_after_even(int num_rows, int array[][NUM_COLS]);

Constraints

  • sum_after_even should return only a single integer.
  • You can assume the array contains at least one row.
  • Every row has exactly NUM_COLS (5) elements.
  • Array elements may be negative. A negative even number such as -2 is still even, and a negative odd number such as -3 is still odd.
  • 0 is considered even.
  • The total may be negative.
  • sum_after_even should not change the array it is given.
  • Do not call scanf, getchar, or fgets.
  • Do not call printf (the function should only return a value).

Hints

  • In C, -3 % 2 evaluates to -1, not 1. Testing x % 2 == 0 correctly identifies even numbers including negative ones, but testing x % 2 == 1 does not correctly identify odd numbers.
  • You need two pieces of information: one that describes the whole array, and one that describes a single row. Think carefully about which loop each one should be declared inside.
  • Anything that describes a single row must be reset at the start of every row.
  • Once you have found the first even number in a row, stop searching that row. Otherwise you will end up with the last even number instead.
  • Recording the index of the first even number, rather than just a yes or no flag, makes step 2 much easier.
  • Use a value such as -1 to mean "this row has no even number", and check for it before you start adding.

Testing

Each command line argument is one row, written as 5 comma-separated values.

./sum-after-even 1,3,2,5,7 1,1,1,1,1
12

./sum-after-even 4,1,1,1,1 3,5,7,9,2
4

./sum-after-even 16,12,8,3,1 2,0,10,1,4 1,1,1,13,1 5,5,5,8,2 5,5,5,5,5
41

./sum-after-even 1,1,1,1,1
0

./sum-after-even 2,1,1,1,1
4

./sum-after-even 1,1,1,1,2
0

./sum-after-even 0,5,5,5,5
20

./sum-after-even -3,-5,4,1,1
2

./sum-after-even -2,-4,-6,-8,-10
-28
Code Editor
Output
Run your code to see output here...