# Sample solution

#### Content

#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);
}


## Compilation and Execution

theon$gcc -Wall -o example1_vector example1_vector.c theon$ ./example1_vector
initialize x as one vector
x =        1.0  2.0  3.0  4.0  5.0  6.0  7.0  8.0
using the space for two vectors
x =        1.0  2.0  3.0  4.0
y =        1.0  2.0  3.0  4.0
vector =   1.0  2.0  3.0  4.0  1.0  2.0  3.0  4.0
using the space for two interleaving vectors
x =        1.0  2.0  3.0  4.0
y =        1.0  2.0  3.0  4.0
vector =   1.0  1.0  2.0  2.0  3.0  3.0  4.0  4.0
theon\$