9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia10/ex11.c (624B)
   1 #include <stdio.h>
   2 
   3 int dot_product(int *v1, int *v2, size_t l);
   4 
   5 void test(int *v1);
   6 
   7 int main (void)
   8 {
   9     int v1[4] = { 1,2,3,4 };
  10     int v2[4] = { 1,2,3,4 };
  11     printf("%d\n", dot_product(v1,v2,4));
  12 
  13     test(v1);
  14 
  15     printf("v1 elements: %ld\n", sizeof(v1) / sizeof(int));
  16     return 0;
  17 }
  18 
  19 /* int dot_m(int *v1, int *v2, size_t index) */
  20 /* { */
  21 /*     if(index == 0) return v1[0] * v2[0]; */
  22 /*     return v1[index] * v2[index] + dot_product(v1, v2, index - 1); */
  23 /* } */
  24 
  25 void test(int *v1)
  26 {
  27     printf("%ld\n", sizeof(*v1));
  28 }
  29 
  30 int dot_product(int *v1, int *v2, size_t l)
  31 {
  32     return l ? v1[l-1] * v2[l-1] + dot_product(v1, v2, l-1) : 0;
  33 }