Problems 3
  1. Theory
    1. How are parameters to functions passed in the C-language: i) a copy of the parameter is created and passed to the function (pass-by-value) or ii) the parameter itself is passed to the function (pass-by-reference) ? Hint: C syntax → argument passing
    2. What is *(&x)?
    3. What is NULL? Hint: null pointer.
    4. What happens to the variables, declared inside a function, when the function exits (returns)?
    5. What is a static variable? Hint: static variable.
    6. What will the following three programs print out?
      #include<stdio.h>
      void f(int i){i=0;}
      int main(){
      	int i=1; f(i); printf("i=%i\n",i);
      	return 0; }
      
      #include<stdio.h>
      void f(int* i){*i=0;}
      int main(){
      	int i=1; f(&i); printf("i=%i\n",i);
      	return 0; }
      
      #include<stdio.h>
      void f(int* i){i=NULL;}
      int main(){
      	int i=1; f(&i); printf("i=%i\n",i);
      	return 0; }
      
    7. If you pass an array to a function with the signature "void f(double a[])" – what is actually passed to the function: i) the copy of the array? ii) the pointer to the first element? iii) the copy of the pointer to the first element? iv) something else? Hint: C syntax → argument passing → array parameters.
    8. When the function with the signature "void f(double a[])" gets the array as parameter -- can it figure out the size of the array?
    9. At which stage—compilation or execution of the program—is the memory for the following arrays allocated? And which of these arrays "know" their sizes in the scope where they are declared?
      int a[] = {0,1,2,3,4};
      int b[5];
      int n = argc>1 ? atoi(argv[1]):5;
      int c[n];
      int *d=(int*)malloc(5*sizeof(int)); 
    10. If you declare an array as int a[5]; and then try a[7]=1; what will happen? Hint: Segmentation fault / causes.
    11. If you declare a static/variable-length/dynamic array inside a function, can the function return it?
    12. What will the following C-program print?
      #include<stdio.h>
      int i=2; /* file scope */
      void f(){printf("i=%i\n",i);}
      int main(){
      	int i=1; /* function scope */
      	{
      		int i=0; /* block scope */
      		printf("i=%i\n",i);
      	}
      	printf("i=%i\n",i);
      	f();
      	return 0; }
      
  2. Practice
    1. Write a set of functions to deal with 3D vectors that are represented by size-3 double arrays. The signatures should be like the following,
      
      void   tri_vector_add          (double v[], double u[]);                         /* v+=u */
      void   tri_vector_subtract     (double v[], double u[]);                         /* v-=u */
      void   tri_vector_scale        (double v[], double a);                           /* v=a*v */
      void   tri_vector_cross_product(double v1[], double v2[], double result[]);      /* v = v1 x v2 */
      double tri_vector_dot_product  (double v1[], double v2[]);                       /* returns v1.v2 */
      double tri_vector_norm         (double v[]);                                     /* returns |v| */
      void   tri_vector_print        (double v[]);                                     /* prints the components of the vector */
      void   tri_vector_set          (double v[], double v_1, double v_2, double v_3); /* v={v_1,v_2,v_3} */
      void   tri_vector_set_zero     (double v[]);                                     /* v=0 */
      void   tri_vector_set_basis    (double v[], int k);                              /* v_{k}=1, v_{i!=k}=0 */
      int    tri_vector_equal        (double v[], double u[]);                         /* returns 1 if v==u, 0 otherwise */
      
      Hint: something like
      
      void   tri_vector_add        (double v[], double u[]) { for(int i=0;i<3;i++) v[i]+=u[i]; }
      double tri_vector_dot_product(double u[], double v[]) { return u[0]*v[0]+u[1]*v[1]+u[2]*v[2]; }
      
    2. Write a main program that calls and tests (some of) your functions. Hint: something like
      double v[]={1,2,3}, u[]={3,2,1}, r[]={4,4,4};
      tri_vector_add(v,u);
      if( tri_vector_equal(v,r) )
      	printf("test 'add' passed :) \n");
      else
      	printf("test 'add' failed :( \n");