C examples for Array:Array Element
Set each element in an array to the sum of the corresponding elements in two other arrays
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10//w ww. j a va 2 s .com void add_arrays(int *addend1, int *addend2, int *sum, int array_length); int main(void) { srand(time(NULL)); int array1[SIZE]; int array2[SIZE]; int sum[SIZE]; // initialize arrays with random ints for (int i = 0; i < SIZE; i++) { array1[i] = rand() % 20; array2[i] = rand() % 20; } // get sum of arrays add_arrays(array1, array2, sum, SIZE); printf("%8s %8s %8s\n", "Array 1", "Array 2", "Sum"); for (int i = 0; i < SIZE; i++) printf("%8d %8d %8d\n", array1[i], array2[i], sum[i]); return 0; } // calculate elementwise sum of two arrays void add_arrays(int *addend1, int *addend2, int *sum, int array_length){ for (int *tar = sum; tar < sum + array_length; tar++, addend1++, addend2++) *tar = *addend1 + *addend2; }