Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tenCpart1.c
- // ENCM 335 Fall 2019 Lab 10 Exercise E Part 1
- #include <stdio.h>
- int sum(const int *a, int lo, int hi)
- {
- // Return sum of a[lo], a[lo + 1], ..., a[hi - 1]
- if (lo == hi - 1)
- return a[lo];
- return a[lo] + sum(a, lo + 1, hi);
- }
- int is_sorted(const int *a, int lo, int hi)
- {
- // Return 1 if the number of elements under consideration is 0 or 1.
- // Return 0 if a[lo] <= a[lo + 1], ...,
- // a[hi - 2] <= a[hi - 1] are all true.
- // Otherwise return false.
- if (lo == hi-1)
- return 1;
- if (a[lo] > a[lo+1])
- return 0;
- else
- return is_sorted(a, lo + 1, hi);
- }
- int same_in_reverse(const int *a, int lo, int hi)
- {
- // Return 1 if the number of elements under consideration is 0 or 1.
- // Return 1 if the values of a[lo] ... a[hi - 1] are the same
- // sequence first to last and last to first. Otherwise return 0.
- if (lo+1 == hi || lo+1 > hi)
- return 1;
- if (a[lo] != a[hi-1])
- return 0;
- else
- return same_in_reverse(a, lo+1, hi-1);
- }
- int count_matches(const int *a, int lo, int hi, int key)
- {
- // Return the count of elements a[lo] ... a[hi-1] that are
- // equal to key.
- int count = 0;
- if (lo+1 == hi)
- {
- if (a[lo] == key)
- return count++;
- else
- return count;
- }
- if (a[lo] == key)
- {
- count++;
- return count += count_matches(a, lo+1, hi, key);
- }
- else
- return count += count_matches(a, lo+1, hi, key);
- //~ int count = 0;
- //~ for (int i = lo; i < hi; i++)
- //~ if (a[i] == key)
- //~ count++;
- //~ return count;
- }
- int main(void)
- {
- int a1[] = { 10, 20, 30, 40, 50 };
- printf("sum for a1 is %d (expect 150)\n", sum(a1, 0, 5));
- int a2[] = { 11, 11, 11, 11, 11, 11 };
- int a3[] = { 10, 31, 30, 40, 50 };
- int a4[] = { 10, 9, 30, 40, 50 };
- int a5[] = { 10, 20, 30, 40, 50, 49 };
- printf("\nis_sorted for a1 is %d (expect 1)\n", is_sorted(a1, 0, 5));
- printf("is_sorted for a2 is %d (expect 1)\n", is_sorted(a2, 0, 6));
- printf("is_sorted for a3 is %d (expect 0)\n", is_sorted(a3, 0, 5));
- printf("is_sorted for a4 is %d (expect 0)\n", is_sorted(a4, 0, 5));
- printf("is_sorted for a5 is %d (expect 0)\n", is_sorted(a5, 0, 6));
- int a7[] = { 42 };
- int a8[] = { 10, 9, 11, 9, 10 };
- int a9[] = { 10, 8, 11, 11, 8, 10 };
- int a10[] = { 10, 9, 11, 7, 10 };
- int a11[] = { 10, 8, 11, 12, 8, 10 };
- printf("\nsame_in_reverse for zero items is %d (expect 1)\n",
- same_in_reverse(a7, 0, 0));
- printf("same_in_reverse for a7 is %d (expect 1)\n",
- same_in_reverse(a7, 0, 1));
- printf("same_in_reverse for a8 is %d (expect 1)\n",
- same_in_reverse(a8, 0, 5));
- printf("same_in_reverse for a9 is %d (expect 1)\n",
- same_in_reverse(a9, 0, 6));
- printf("same_in_reverse for a10 is %d (expect 0)\n",
- same_in_reverse(a10, 0, 5));
- printf("same_in_reverse for a11 is %d (expect 0)\n",
- same_in_reverse(a11, 0, 6));
- printf("\ncount_matches for zero items with key 11 is %d (expect 0)\n",
- count_matches(a7, 0, 0, 11));
- printf("count_matches for a7 with key 42 is %d (expect 1)\n",
- count_matches(a7, 0, 1, 42));
- printf("count_matches for a9 with key 8 is %d (expect 2)\n",
- count_matches(a9, 0, 6, 8));
- printf("count_matches for a2 with key 11 is %d (expect 6)\n",
- count_matches(a2, 0, 6, 11));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement