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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

void init_vector(double* v, size_t len, ptrdiff_t incr) {
   for (size_t i = 0; i < len; ++i) {
      v[i*incr] = i + 1;
   }
}

double sum(double* x, size_t len, ptrdiff_t incr) {
   double result = 0;
   for (size_t i = 0; i < len; ++i) {
      result += x[i * incr];
   }
   return result;
}

int main() {
   printf("vector length = ");
   size_t len;
   if (scanf("%zu", &len) != 1) return 1;
   double* vector = (double*) malloc(sizeof(double) * len);
   if (!vector) {
      printf("unable to allocate heap memory for %zd double values\n",
	 len);
      return 1;
   }
   init_vector(vector, len, 1);
   printf("sum = %lg\n", sum(vector, len, 1));
   free(vector);
}