ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Simple Array Sum

C

Goal -- WPM

Ready
Exercise Algorithm Area
1#include <stdio.h>
2#include <limits.h>
3
4// Helper function to check for potential overflow before addition
5int safe_add(int a, int b, int *overflow) {
6if (a > 0 && b > 0 && a > INT_MAX - b) {
7*overflow = 1;
8return INT_MAX; // Indicate overflow
9}
10if (a < 0 && b < 0 && a < INT_MIN - b) {
11*overflow = 1;
12return INT_MIN; // Indicate underflow
13}
14return a + b;
15}
16
17// Calculates the sum of elements in an integer array.
18// Returns the sum, or INT_MAX/INT_MIN if overflow/underflow occurs.
19long long sum_array(int arr[], int size) {
20if (size <= 0) {
21return 0; // Handle empty array case
22}
23
24long long total_sum = 0;
25int overflow_flag = 0;
26
27for (int i = 0; i < size; ++i) {
28total_sum = safe_add(total_sum, arr[i], &overflow_flag);
29if (overflow_flag) {
30printf("Warning: Integer overflow detected!\n");
31// Depending on requirements, could return an error code or a specific value.
32// For this example, we'll cap at INT_MAX/INT_MIN and continue, but flag it.
33break; // Stop summing if overflow occurs to avoid further issues
34}
35}
36
37return total_sum;
38}
39
40int main() {
41int data1[] = {1, 2, 3, 4, 5};
42int size1 = sizeof(data1) / sizeof(data1[0]);
43printf("Sum 1: %lld\n", sum_array(data1, size1)); // Expected: 15
44
45int data2[] = {};
46int size2 = 0;
47printf("Sum 2: %lld\n", sum_array(data2, size2)); // Expected: 0
48
49int data3[] = {INT_MAX, 1};
50int size3 = sizeof(data3) / sizeof(data3[0]);
51printf("Sum 3: %lld\n", sum_array(data3, size3)); // Expected: INT_MAX (with overflow warning)
52
53return 0;
54}
Algorithm description viewbox

Simple Array Sum

Algorithm description:

This function computes the sum of all elements within an integer array. It includes a helper function to safely perform addition, checking for potential integer overflow or underflow before it occurs. This is crucial for applications dealing with large datasets or values where sums might exceed standard integer limits.

Algorithm explanation:

The `sum_array` function iterates through a given integer array of a specified size and accumulates the sum of its elements. A `safe_add` helper function is used to prevent integer overflow/underflow by checking if the addition of two numbers would exceed `INT_MAX` or fall below `INT_MIN`. If an overflow is detected, a flag is set, a warning is printed, and the summation stops to prevent further erroneous calculations. The function handles the edge case of an empty array by returning 0. The time complexity is O(n) where n is the size of the array, as each element is visited once. The space complexity is O(1) as only a few variables are used regardless of input size. The correctness relies on the `safe_add` function accurately detecting and signaling potential overflow conditions.

Pseudocode:

function sum_array(arr, size):
  if size is 0:
    return 0
  
  total_sum = 0
  for each element in arr:
    total_sum = safe_add(total_sum, element)
    if overflow occurred during safe_add:
      print "Overflow detected"
      break
  return total_sum

function safe_add(a, b):
  if a > 0 and b > 0 and a > MAX_INT - b:
    return MAX_INT (and set overflow flag)
  if a < 0 and b < 0 and a < MIN_INT - b:
    return MIN_INT (and set overflow flag)
  return a + b