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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**********************************************************************\
 * This is an illustration created during class.  It is not complete. *|
\**********************************************************************/
#include <stdio.h>
#include <stdlib.h>

int* _make_array(size_t len, int value) {
    int* array = malloc(len * sizeof(*array));
    for(int i = 0; i < len; i++) {
        array[i] = value;
    }
    return array;
}

// SINGLE-THREADED
int* add_arrays(int* lhs, int* rhs, size_t len) {
    int* result = malloc(len * sizeof(*result));
    for(int i = 0; i < len; i++) {
        result[i] = lhs[i] + rhs[i];
    }
    return result;
}

void _print_array(int* array, size_t len, const char* name) {
    for(int i = 0; i < len; i++) {
        printf("%s[%d] == %d\n", name, i, array[i]);
    }
}

int main(int argc, char* argv[]) {
    size_t len  = 4;
    int* array5 = _make_array(len, 5);
    int* array7 = _make_array(len, 7);

    int* result = add_arrays(array5, array7, len);

    _print_array(array5, len, "array5");
    _print_array(array7, len, "array7");
    _print_array(result, len, "result");

    free(array5);
    free(array7);
    free(result);
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2019 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.