Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.28 KB | None | 0 0
  1. // ATTENTION: The definitions given below for is_sorted and
  2. // all_different are DEFECTIVE!
  3.  
  4. #include <assert.h>
  5.  
  6. #include "array-utils5A.h"
  7.  
  8. int is_sorted(const int *a, int n)
  9. {
  10.   assert (n >= 1);
  11.  
  12.   if (n == 1)
  13.     return 1;
  14.   else
  15.   {
  16.     int result = 0, i;
  17.     for (i = 1; i < n; i++)
  18.     {
  19.         if (a[i - 1] <= a[i])
  20.             result = 1;
  21.         else
  22.             return 0;
  23.     }
  24.   return result;
  25.    }
  26. }
  27. int all_different(const int *a, int n)
  28. {
  29.   assert(n > 1);
  30.  
  31.   int i, j, result;
  32.   for (i = 0; i < n; i++)
  33.   {
  34.     for (j = i+1; j < n; j++)
  35.     {
  36.       if (a[i] != a[j])
  37.         result = 1;
  38.       else
  39.         return 0;
  40.     }
  41.   }
  42.   return result;
  43. }
  44.  
  45. int is_alternating(const int *a, int n)
  46. {
  47. //  assert(n > 1);
  48.    
  49.     if (n == 1)
  50.         return 1;
  51.     else
  52.     {
  53.         int i, result;
  54.    
  55.         for (i = 0; i < n; i+=2)
  56.         {
  57.             if (a[0] > 0)
  58.             {
  59.                 if (a[i] > 0 && a[i+1] < 0)
  60.                 {
  61.                     if (i == (n-1))
  62.                     {  
  63.                         if (a[i] > 0)
  64.                             result = 1;
  65.                         else
  66.                             return 0;
  67.                     }
  68.                     result = 1;
  69.                 } else
  70.                     return 0;
  71.             }
  72.             if (a[0] < 0)
  73.             {
  74.                 if (a[i] < 0 && a[i+1] > 0)
  75.                 {
  76.                     if (i == (n-1))
  77.                     {  
  78.                         if (a[i] < 0)
  79.                             result = 1;
  80.                         else
  81.                             return 0;
  82.                     }
  83.                     result =1;
  84.                 } else
  85.                     return 0;
  86.             }
  87.         }
  88.     return result;
  89.     }
  90. }
  91.  
  92. #ifdef UNIT_TESTS
  93. #include <stdio.h>
  94.  
  95. // This macro works for variables declared to be arrays. (DON'T try to
  96. // use it for function parameters declared to be arrays!)
  97. #define COUNT(x) (sizeof(x) / sizeof(x[0]))
  98.  
  99. void test_is_sorted(const char *tag,
  100.                             const int *a, int n, int expected_rv);
  101.  
  102. void test_all_different(const char *tag,
  103.                         const int *a, int n, int expected_rv);
  104.                        
  105. void test_is_alternating(const char *tag,
  106.                         const int *a, int n, int expected_rv);                    
  107.  
  108. int main(void)
  109. {
  110.   int test_01[] = {10, 10, 10, 10, 10};
  111.   int test_02[] = {11};
  112.   int test_03[] = {11, 12, 13, 14, 15};
  113.   int test_04[] = {16, 17, 18, 17, 18, 19};
  114.   int test_05[] = {20, 22, 24, 26, 25};
  115.   test_is_sorted("test_01", test_01, COUNT(test_01), 1);
  116.   test_is_sorted("test_02", test_02, COUNT(test_02), 1);
  117.   test_is_sorted("test_03", test_03, COUNT(test_03), 1);
  118.   test_is_sorted("test_04", test_04, COUNT(test_04), 0);
  119.   test_is_sorted("test_05", test_05, COUNT(test_05), 0);
  120.   fputc('\n', stdout);
  121.  
  122.   int test_06[] = { 30, 31 };
  123.   int test_07[] = { 32, 35, 34, 36, 31 };
  124.   int test_08[] = { 40, 41, 42, 43, 44, 40 };
  125.   int test_09[] = { 50, 50, 51, 52, 53};
  126.   int test_10[] = { 60, 61, 62, 63, 64, 64 };
  127.   test_all_different("test_06", test_06, COUNT(test_06), 1);
  128.   test_all_different("test_07", test_07, COUNT(test_07), 1);
  129.   test_all_different("test_08", test_08, COUNT(test_08), 0);
  130.   test_all_different("test_09", test_09, COUNT(test_09), 0);
  131.   test_all_different("test_10", test_10, COUNT(test_10), 0);
  132.   fputc('\n', stdout);
  133.  
  134.   int test_11[] = {-4};
  135.   int test_12[] = {1, -5, 9, -3, 2, 65};
  136.   int test_13[] = {4, -78};
  137.   int test_14[] = {1, -2, 6, 0, 23, -6 };
  138.   int test_15[] = {-4, 5, -2, 9, -3, 56, -34};
  139.   test_is_alternating("test_11", test_11, COUNT(test_11), 1);
  140.   test_is_alternating("test_12", test_12, COUNT(test_12), 0);
  141.   test_is_alternating("test_13", test_13, COUNT(test_13), 1);
  142.   test_is_alternating("test_14", test_14, COUNT(test_14), 0);
  143.   test_is_alternating("test_15", test_15, COUNT(test_15), 1);
  144.   fputc('\n', stdout);
  145.  
  146.   return 0;
  147. }
  148.  
  149. void test_is_sorted(const char *tag,
  150.                      const int *a, int n, int expected_rv)
  151. {
  152.   printf("Testing is_sorted for case with tag \"%s\":", tag);
  153.   if (expected_rv == is_sorted(a, n))
  154.     printf(" Pass.\n");
  155.   else
  156.     printf(" FAIL!\n");
  157. }
  158.  
  159. void test_all_different(const char *tag,
  160.                         const int *a, int n, int expected_rv)
  161. {
  162.   printf("Testing all_different for case with tag \"%s\":", tag);
  163.   if (expected_rv == all_different(a, n))
  164.     printf(" Pass.\n");
  165.   else
  166.     printf(" FAIL!\n");
  167. }
  168.  
  169. void test_is_alternating(const char *tag,
  170.                         const int *a, int n, int expected_rv)
  171. {
  172.   printf("Testing all_different for case with tag \"%s\":", tag);
  173.   if (expected_rv == is_alternating(a, n))
  174.     printf(" Pass.\n");
  175.   else
  176.     printf(" FAIL!\n");
  177. }
  178.  
  179. #endif // #ifdef UNIT_TESTS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement