9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit 991015b3c594ed5d219617e65ec8528031ebd85b
parent 706b793a291556587d1be8adff14d6bb3d6ce5a9
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Wed, 24 Mar 2021 17:05:52 -0300

Update: ex23.c added isSortedBothDirections() function which returns a bool value whether the int array pointed by is sorted or not from greater to less or viceversa

Diffstat:
Mguia03/ex34.c | 18++++++++++++++++++
1 file changed, 18 insertions(+), 0 deletions(-)
diff --git a/guia03/ex34.c b/guia03/ex34.c
@@ -4,16 +4,34 @@
 #define N 10
 
 bool isSorted(int *vector, size_t size);
+bool isSortedBothDirections(int *vector, size_t size);
 
 int main (void) 
 {
     int vector[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+    int vectorInv[N] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
 
     printf("The vector is sorted: %s\n", isSorted(vector, N) ? "Yes" : "No");
+    printf("The vector is sorted: %s\n", isSortedBothDirections(vector, N) ? "Yes" : "No");
 
     return 0;
 }
 
+
+bool isSortedBothDirections(int *vector, size_t size)
+{
+    size_t i, j, k;
+    i = j = k = 1;
+
+    while(i < size) {
+        j += (vector[i] >= vector[i - 1]);
+        k += (vector[i] <= vector[i - 1]);
+        i++;
+    }
+
+    return true * (j == size) + true * (k == size);
+}
+
 bool isSorted(int *vector, size_t size)
 {
     size_t i;