Problems 3
  1. Theory
    1. How are parameters to functions passed in the C-language, according to the pass-by-value or the pass-by-reference strategy? And what does that mean?
    2. What will the following program 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; }
      
    3. What will the following program 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; }
      
    4. What will the following program print out?
      #include<stdio.h>
      void f(int* i){i=NULL;}
      int main(){
      	int i=1; f(&i); printf("i=%i\n",i);
      	return 0; }
      
    5. If you pass an array to a function with the signature void do_something_with_an_array(int a[]); – what is actually passed to the function (the copy of the array? the pointer to the first element? or what?).
    6. Do arrays in C know their sizes? Hint: the answer "no" is safe, although the question is actually a bit more subtle, see using sizeof with arrays.
    7. 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 = (double)rand()/RAND_MAX*10+1;
      int c[n];
      int *d=(int*)malloc(n*sizeof(int)); 
    8. If you pass these arrays to our do_something_with_an_array subroutine, will it be possible to determine the sizes of the arrays inside the subroutine?
    9. If you declare an array as int a[5]; and then try a[7]=1; what will happen? Hint: Segmentation fault / causes.
    10. If you declare a static/variable-length/dynamic array inside a function, can the function return it?
    11. 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
      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 v[]); /* 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]; }

    2. Write a main program that calls and tests 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");