UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

6.8. Automatic self-assessment

Check with these questions if you understood this document

  1. We would like to convert a function that returns the sum of two numbers, in another that returns the sum and, in an additional parameter, the subtraction. Choose the correct prototype

    • int sum_subs(int a, int b, int subs);

    • int *sum_subs(int a, int b, int *subs);

    • int sum_subs(int a, int b, int *subs);

    • void sum_subs(int a, int b, int subs);

    • void sum_subs(int a, int b, int *subs);

  2. We would like to use the function multiple that returns the multiplication of two numbers in an additional parameter. This function assumes the previous allocation of the result parameter. Choose the correct function call, without using malloc in the caller program

    • int a=5,b=6,resultado;
      resultado=multiple(a,b);

    • int a=5,b=6,*resultado;
      multiple(a,b,resultado);

    • int a=5,b=6,resultado;
      multiple(a,b,&resultado);

    • int a=5,b=6,resultado;
      multiple(a,b,*resultado);

    • int a=5,b=6,*resultado;
      multiple(a,b,&resultado);

  3. We would like to use the function multiple that returns the multiplication of two numbers in an additional parameter. This function assumes the previous allocation of the result parameter. Choose the correct function call, using malloc in the caller program

    • int a=5,b=6,resultado;
      resultado=(int)malloc(1);
      multiple(a,b,resultado);

    • int a=5,b=6,*resultado;
      resultado=(int *) malloc(sizeof(int));
      multiple(a,b,resultado);

    • int a=5,b=6,*resultado;
      resultado=(int *) malloc(sizeof(int));
      multiple(a,b,&resultado);

    • int a=5,b=6,resultado;
      resultado=(int) malloc(sizeof(int));
      multiple(a,b,*resultado);

    • int a=5,b=6,*resultado;
      resultado=(int *)malloc(sizeof(int));
      multiple(a,b,*resultado);

  4. We would like to implement the function multiple that returns the multiplication of two numbers in an additional parameter. This function assumes the previous allocation of the result parameter. Choose the correct function code

    • void multiple(int a, int b, int *result)
      {
       result=a*b;
      }

    • void multiple(int a, int b, int *result)
      {
       return a*b;
      }

    • void multiple(int a, int b, int result)
      {
       result=a*b;
      }

    • void multiple(int a, int b, int *result)
      {
       &result=a*b;
      }

    • void multiple(int a, int b, int *result)
      {
       *result=a*b;
      }

  5. We would like to define a function, which given a number of students, returns a vector of integers initialized to the zero value. Choose the correct code.

    • void set_up(int number_students, int *vector)
      {
       *vector = (int *) calloc (number_students,sizeof(int));
      }

    • void set_up(int number_students, int *vector)
      {
       *vector = (int *) calloc (number_students*sizeof(int));
      }

    • int *set_up(int number_students)
      {
       int *vector = (int *) calloc (number_students,sizeof(int));
       return &vector;
      }

    • int *set_up(int number_students)
      {
       int *vector = (int *) calloc (number_students,sizeof(int));
       return vector;
      }

    • int *set_up(int number_students)
      {
      int *vector = (int *) calloc (number_students*sizeof(int));
      return vector;
      }
      	      

  6. We would like to define a function, which given a number n, returns in a vector (given as parameter) of integers the first n natural numbers, starting in the 0. The function must allocate space for the vector. Choose the correct code.

    • void first_n(int number, int **vector)
      {
          int i;
          *vector = (int *)malloc (number * sizeof(int));
          for (i = 0;i < number; i++) {
              (*vector)[i]=i;
          }
      }

    • void first_n(int number, int *vector)
      {
          int i;
          vector = (int *) malloc (number*sizeof(int));
          for (i = 0;i <= number; i++)
          {
             vector[i]=i;
          }
      }

    • void first_n(int number, int *vector)
      {
          int i;
          vector = (int *)malloc(number, sizeof(int));
          for (i = 0; i < number; i++)
          {
              vector[i]=i;
          }
      }

    • int *first_n(int number)
      {
          int i;
          int *vector = (int *)malloc(number * sizeof(int));
          for (i = 0;i < number; i++) {
              vector[i]=i;
          }
          return vector;
      }