Advertisement
informaticage

Ansi C recursion excercises for AocLeL

May 23rd, 2021
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define LEN 16
  7.  
  8. // To convert in recursive
  9. double average(int v[], size_t length);
  10. int max(int v[], size_t length);
  11. long long product(int v[], size_t length);
  12. size_t find(int v[], size_t length, int to_find);
  13.  
  14. int main(void) {
  15.   int vtest[LEN];
  16.   srand(time(NULL));
  17.   for (size_t i = 0; i < LEN; i++) {
  18.     vtest[i] = rand() % 30 + 1;
  19.   }
  20.  
  21.   // Printing the resulting array
  22.   printf("[ ");
  23.   for (size_t i = 0; i < LEN; i++) {
  24.     printf("%d ", vtest[i]);
  25.   }
  26.   printf("]\n");
  27.  
  28.   printf("Average: %lf\n", average(vtest, LEN));
  29.   printf("Max: %d\n", max(vtest, LEN));
  30.   printf("Product: %lld\n", product(vtest, LEN));
  31.  
  32.   printf("\nTo find: ");
  33.   int to_find;
  34.   scanf("%d", &to_find);
  35.   printf("Find: %d\n", find(vtest, LEN, to_find));
  36.   return 0;
  37. }
  38.  
  39. int sum(int v[], size_t length) {
  40.   int s = 0;
  41.   for (size_t i = 0; i < length; i++) {
  42.     s += v[i];
  43.   }
  44.   return s;
  45. }
  46.  
  47. int sum_rec(int v[], size_t length, size_t index) {
  48.   if (length == index)
  49.     return 0;
  50.   return v[index] + sum_rec(v, length, index + 1);
  51. }
  52.  
  53. // Loop implementation
  54. bool binary_search(int v[], size_t length, int to_find) {
  55.   size_t i = 0, j = length;
  56.  
  57.   while (j - i > 1) {
  58.     // (i + j) / 2
  59.     size_t mid = (i + (j - i) / 2);
  60.  
  61.     if (v[mid] < to_find)
  62.       i = mid;
  63.     if (v[mid] > to_find)
  64.       j = mid;
  65.     if (v[mid] == to_find)
  66.       return true;
  67.   }
  68.  
  69.   printf("i: %d, j: %d\n", i, j);
  70.   return v[i] == to_find || v[j] == to_find;
  71. }
  72.  
  73. bool binary_search_rec(int v[], size_t length, int to_find, size_t i,
  74.                        size_t j) {
  75.   if (!(j - i > 1))
  76.     return v[i] == to_find || v[j] == to_find;
  77.  
  78.   // (i + j) / 2
  79.   size_t mid = (i + (j - i) / 2);
  80.  
  81.   if (v[mid] < to_find)
  82.     i = mid;
  83.   if (v[mid] > to_find)
  84.     j = mid;
  85.   if (v[mid] == to_find)
  86.     return true;
  87.  
  88.   return binary_search_rec(v, length, to_find, i, j);
  89. }
  90.  
  91. bool binary_search_rec_2(int v[], size_t length, int to_find, size_t i,
  92.                          size_t j) {
  93.   if (!(j - i > 1))
  94.     return v[i] == to_find || v[j] == to_find;
  95.  
  96.   // (i + j) / 2
  97.   size_t mid = (i + (j - i) / 2);
  98.  
  99.   if (v[mid] < to_find)
  100.     return binary_search_rec(v, length, to_find, mid, j);
  101.   if (v[mid] > to_find)
  102.     return binary_search_rec(v, length, to_find, i, mid);
  103.   if (v[mid] == to_find)
  104.     return true;
  105. }
  106.  
  107. // To convert in recursive
  108. double average(int v[], size_t length) {
  109.   int s = 0;
  110.   for (size_t i = 0; i < length; i++) {
  111.     s += v[i];
  112.   }
  113.  
  114.   return (double)s / (double)length;
  115. }
  116.  
  117. int max(int v[], size_t length) {
  118.   int max = v[0];
  119.   for (size_t i = 0; i < length; i++) {
  120.     if (v[i] > max) {
  121.       max = v[i];
  122.     }
  123.   }
  124.  
  125.   return max;
  126. }
  127.  
  128. long long product(int v[], size_t length) {
  129.   // To convert in recursive
  130.   long long s = 1;
  131.   for (size_t i = 0; i < length; i++) {
  132.     s *= v[i];
  133.   }
  134.  
  135.   return s;
  136. }
  137.  
  138.  
  139. size_t find(int v[], size_t length, int to_find) {
  140.   for (size_t i = 0; i < length; i++) {
  141.     if (v[i] == to_find) {
  142.       return i;
  143.     }
  144.   }
  145.  
  146.   return -1;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement