add-arrays.cpp

Up: Array Notation
/*
 * add-arrays.cpp
 *
 * Demonstrate two arrays to produce a third using both for loop and
 * Array Notation
 */

#include <stdio.h>
#include <string.h>

const int array_size=10;

int main(int argc, char **argv)
{
    int a[array_size], b[array_size], c[array_size], d[array_size];

    // Initialize arrays a & b using for loop
    for (int i = 0; i < array_size; i++)
    {
        a[i] = i;
        b[i] = i + array_size;
    }

    // Add arrays using for loop
    for (int i = 0; i < array_size; i++)
        c[i] = a[i] + b[i];

    // Add the arrays using Array Notation.  Since the array is statically
    // allocated, we can use default values for the start index (0) and
    // number of elements (all of them).
    d[:] = a[:] + b[:];

    // Verify the results - The arrays should be identical
    if (0 == memcmp(c, d, sizeof(c)))
        printf("Success\n");
    else
        printf("Failed\n");

    return 0;
}
Up: Array Notation