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

void print_vector(double* v, size_t len, ptrdiff_t incr) {
   for (size_t i = 0; i < len; ++i) {
      printf(" %4.1lf", v[i*incr]);
   }
   printf("\n");
}

#define MAX_LEN 8
double vector[MAX_LEN];

int main() {
   double* x = vector;
   printf("initialize x as one vector\n");
   size_t len = MAX_LEN;
   init_vector(x, len, 1);
   printf("x =      "); print_vector(x, MAX_LEN, 1);

   printf("using the space for two vectors\n");
   len = MAX_LEN/2;
   double* y = vector + len;
   init_vector(x, len, 1); init_vector(y, len, 1);
   printf("x =      "); print_vector(x, len, 1);
   printf("y =      "); print_vector(y, len, 1);
   printf("vector = "); print_vector(vector, MAX_LEN, 1);

   printf("using the space for two interleaving vectors\n");
   y = vector + 1;
   init_vector(x, len, 2); init_vector(y, len, 2);
   printf("x =      "); print_vector(x, len, 2);
   printf("y =      "); print_vector(y, len, 2);
   printf("vector = "); print_vector(vector, MAX_LEN, 1);
}