Programming Fundamentals

Download largest_z_sum.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity largest_z_sum

Your task is to add code to this function in largest_z_sum.c:

// Return the largest sum of numbers in a z shape.
int largest_z_sum(int size, int array[MAX_SIZE][MAX_SIZE]) {
    // Put your code here.
    return 42;
}

You are to implement the largest_z_sum function which should return the sum of values forming the shape of the letter 'Z' in a square 2D array.

A Z shape is made up of three lines of equal length. Two of these lines are horizontal and one is diagonal. The length of the three lines must be equal but can range from 3 up to the size of the array. Only correctly oriented Z shapes are valid - Z shapes with a northwest/southeast diagonal are not valid.

The 2D square array may contain any positive or negative integers.

You can assume that the side length of the 2D square array will always be greater than or equal to 3.

You can assume that the side length of the 2D array will never be greater than 100.

The file largest_z_sum.c contains a main function which reads values into a square 2D array and calls largest_z_sum.

Examples

dcc largest_z_sum.c -o largest_z_sum
./largest_z_sum
Enter 2D array side length: 3
Enter 2D array values:
1 1 1
1 1 1
1 1 1
The largest z sum is 7.
./largest_z_sum
Enter 2D array side length: 5
Enter 2D array values:
1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
The largest z sum is 169.
./largest_z_sum
Enter 2D array side length: 5
Enter 2D array values:
 28 -47 -40  29  49
 26 -42 -37  48  1
-36  50  41 -24 -33
 41  25 -39  39  48
 14 -26 -46 -3  -29
The largest z sum is 153.
./largest_z_sum
Enter 2D array side length: 5
Enter 2D array values:
1  1  1  1  1
1  1  1  1  1
99 99 99 1  1
1  99 1  1  1
99 99 99 1  1
The largest z sum is 693.

In the first example, there is only one possible Z sum of size 3.

The Z in the example input is underlined below for your reference:

1 1 1
1 1 1
1 1 1

In the second example, the Z of size 5 starting from (0, 0) is used to form the largest sum of:

1 + 2 + 3 + 4 + 5 + 9 + 13 + 17 + 21 + 22 + 23 + 24 + 25 = 169

The Z in the example input is underlined below for your reference:

1  2  3  4  5
6  7  8  9  10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

In the third example, the Z of size 4 starting from (0, 1) is used to form the largest sum of:

-47 - 40 + 29 + 49 + 48 + 41 + 25 - 39 + 39 + 48 = 153

The Z in the example input is underlined below for your reference:

 28 -47 -40  29  49
 26 -42 -37  48  1
-36  50  41 -24 -33
 41  25 -39  39  48
 14 -26 -46 -3  -29

In the fourth example, the Z of size 3 starting from (2, 0) is used to form the largest sum of:
99 + 99 + 99 + 99 + 99 + 99 + 99 = 693
The Z in the example input is underlined below for your reference:

1  1  1  1  1
1  1  1  1  1
99 99 99 1  1
1  99 1  1  1
99 99 99 1  1