# articles_code / c_scientific_article / gsl_vector_ops.c

 ``` 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81``` ```/*Listing-2: gsl_vector_ops.c*/ /* Vector operations in GSL * Also uses the random number generation feature */ #include #include /*For Vectors*/ #include /* For Random numbers*/ int main () { int i,n; /* Setup the Random number generator*/ const gsl_rng_type * T; gsl_rng * r; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); printf("Number of elements in the vector:: "); scanf("%d",&n); /* Allocate the vectors of the specified size*/ gsl_vector * v1 = gsl_vector_alloc (n); gsl_vector * v2 = gsl_vector_alloc (n); /* Set the elements to a uniform random number in [0,1]*/ for (i = 0; i < n; i++) { gsl_vector_set (v1, i, gsl_rng_uniform (r)); gsl_vector_set (v2, i, gsl_rng_uniform (r)); } /* Print the vector*/ printf("V1:: "); for (i = 0; i < n; i++) { printf ("%g ", gsl_vector_get (v1, i)); } printf("\n"); printf("V2:: "); for (i = 0; i < n; i++) { printf ("%g ", gsl_vector_get (v2, i)); } printf("\n\n"); printf(">>> Vector Operations >>> \n\n"); /* v1+v2 gets stored in v1*/ gsl_vector_add(v1,v2); printf("V1+V2:: "); for (i = 0; i < n; i++) { printf ("%g ", gsl_vector_get (v1, i)); } printf("\n"); /* v1-v2 gets stored in v1*/ gsl_vector_sub(v1,v2); printf("V1-V2:: "); for (i = 0; i < n; i++) { printf ("%g ", gsl_vector_get(v1, i)); } printf("\n"); gsl_vector_free (v1); gsl_vector_free (v2); return 0; } ```