Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define LEN 16
- // To convert in recursive
- double average(int v[], size_t length);
- int max(int v[], size_t length);
- long long product(int v[], size_t length);
- size_t find(int v[], size_t length, int to_find);
- int main(void) {
- int vtest[LEN];
- srand(time(NULL));
- for (size_t i = 0; i < LEN; i++) {
- vtest[i] = rand() % 30 + 1;
- }
- // Printing the resulting array
- printf("[ ");
- for (size_t i = 0; i < LEN; i++) {
- printf("%d ", vtest[i]);
- }
- printf("]\n");
- printf("Average: %lf\n", average(vtest, LEN));
- printf("Max: %d\n", max(vtest, LEN));
- printf("Product: %lld\n", product(vtest, LEN));
- printf("\nTo find: ");
- int to_find;
- scanf("%d", &to_find);
- printf("Find: %d\n", find(vtest, LEN, to_find));
- return 0;
- }
- int sum(int v[], size_t length) {
- int s = 0;
- for (size_t i = 0; i < length; i++) {
- s += v[i];
- }
- return s;
- }
- int sum_rec(int v[], size_t length, size_t index) {
- if (length == index)
- return 0;
- return v[index] + sum_rec(v, length, index + 1);
- }
- // Loop implementation
- bool binary_search(int v[], size_t length, int to_find) {
- size_t i = 0, j = length;
- while (j - i > 1) {
- // (i + j) / 2
- size_t mid = (i + (j - i) / 2);
- if (v[mid] < to_find)
- i = mid;
- if (v[mid] > to_find)
- j = mid;
- if (v[mid] == to_find)
- return true;
- }
- printf("i: %d, j: %d\n", i, j);
- return v[i] == to_find || v[j] == to_find;
- }
- bool binary_search_rec(int v[], size_t length, int to_find, size_t i,
- size_t j) {
- if (!(j - i > 1))
- return v[i] == to_find || v[j] == to_find;
- // (i + j) / 2
- size_t mid = (i + (j - i) / 2);
- if (v[mid] < to_find)
- i = mid;
- if (v[mid] > to_find)
- j = mid;
- if (v[mid] == to_find)
- return true;
- return binary_search_rec(v, length, to_find, i, j);
- }
- bool binary_search_rec_2(int v[], size_t length, int to_find, size_t i,
- size_t j) {
- if (!(j - i > 1))
- return v[i] == to_find || v[j] == to_find;
- // (i + j) / 2
- size_t mid = (i + (j - i) / 2);
- if (v[mid] < to_find)
- return binary_search_rec(v, length, to_find, mid, j);
- if (v[mid] > to_find)
- return binary_search_rec(v, length, to_find, i, mid);
- if (v[mid] == to_find)
- return true;
- }
- // To convert in recursive
- double average(int v[], size_t length) {
- int s = 0;
- for (size_t i = 0; i < length; i++) {
- s += v[i];
- }
- return (double)s / (double)length;
- }
- int max(int v[], size_t length) {
- int max = v[0];
- for (size_t i = 0; i < length; i++) {
- if (v[i] > max) {
- max = v[i];
- }
- }
- return max;
- }
- long long product(int v[], size_t length) {
- // To convert in recursive
- long long s = 1;
- for (size_t i = 0; i < length; i++) {
- s *= v[i];
- }
- return s;
- }
- size_t find(int v[], size_t length, int to_find) {
- for (size_t i = 0; i < length; i++) {
- if (v[i] == to_find) {
- return i;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement