Count Neutral Rows

🎯 Array Hurdle

Count Neutral Rows

Difficulty: ⚫◯◯◯

Tags: array, 2d-array, counting

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

Description

Write a function count_neutral_rows that takes a two-dimensional array with 4 rows and size columns.

The function should return the number of neutral rows in the array. A neutral row is a row where all the elements add up to 0.

Examples

Example 1

Input:
  {16, 16, 16, 16, 16}
  {2,   2,  2, -4, -2}
  {2,  -2,  1, -4,  3}
  {2,   2,  1, -3, -2}

Output: 3

Explanation:

  • Row 0: 16+16+16+16+16 = 80 (not neutral)
  • Row 1: 2+2+2+(-4)+(-2) = 0 (neutral)
  • Row 2: 2+(-2)+1+(-4)+3 = 0 (neutral)
  • Row 3: 2+2+1+(-3)+(-2) = 0 (neutral)

Three rows are neutral.

Example 2

Input:
  {17}
  {2}
  {0}
  {-4}

Output: 1

Explanation: Only the third row (containing just 0) adds up to 0.

Example 3

Input:
  {0, 0, 0}
  {0, 0, 0}
  {0, 0, 0}
  {0, 0, 0}

Output: 4

Explanation: All rows add up to 0.

Example 4

Input:
  {1, 2, 3}
  {4, 5, 6}
  {7, 8, 9}
  {10, 11, 12}

Output: 0

Explanation: No row adds up to 0.

Function Signature

int count_neutral_rows(int size, int array[NUM_ROWS][MAX_COLS]);

Constants

#define NUM_ROWS 4
#define MAX_COLS 100

Constraints

  • The array always has exactly 4 rows.
  • The array has at least 1 column.
  • Your function should return a single integer between 0 and 4 (inclusive).
  • Do not change the array provided.
  • Do not call scanf, getchar, or fgets.
  • Do not call printf (the function should only return a value).

Hints

  • Loop through each row.
  • For each row, calculate the sum of all elements.
  • If the sum equals 0, increment your counter.

Testing

./count-neutral-rows 16,16,16,16,16 2,2,2,-4,-2 2,-2,1,-4,3 2,2,1,-3,-2
3

./count-neutral-rows 17 2 0 -4
1

./count-neutral-rows 0,0,0 0,0,0 0,0,0 0,0,0
4

./count-neutral-rows 1,2,3 4,5,6 7,8,9 10,11,12
0
Code Editor
Output
Run your code to see output here...