Commits

Stephen Tanner committed 428a200

Finished array function and nothing found conditions.

  • Participants
  • Parent commits 43dc814

Comments (0)

Files changed (7)

Debug/src/array.o

Binary file modified.

Debug/src/bitwise.o

Binary file modified.

Debug/src/main.o

Binary file modified.

Debug/src/recursive.o

Binary file modified.

Debug/subc

Binary file modified.
  */
 #include "substring.h"
 
+void i_to_bin(int n, char n_array[]);
+bool compare_bins (char orig[], char comp[]);
 
 bool array_find_substring (int first_int, int second_int) {
 
+    char n1_array[SIZE];
+    char n2_array[SIZE];
+    char comp_array[SIZE];
+    int matches[SIZE];
+    int match_cnt = 0;
 
+    int i;
 
+    for (i = 0; i < SIZE; i++) {
+        n1_array[i] = '0';
+        n2_array[i] = '0';
+        comp_array[i] = '0';
+        matches[i] = 0;
+    }
 
-    return false;
+    i_to_bin(first_int, n1_array);
+    i_to_bin(second_int, n2_array);
+
+    //get the length of the sub string
+    int length = 0;
+    for (i = SIZE -1; i >= 0; i--) {
+
+        if (n2_array[i] == '1') {
+            length = i + 1;
+            break;
+        }
+    }
+    int moves = SIZE - length;
+    for (i = 0; i < moves; i ++) {
+        int j;
+        //Populate the comp_array for comparing;
+        for (j = 0; j < length; j++) {
+            comp_array[j] = n1_array[i+j];
+        }
+        bool match = compare_bins(n2_array, comp_array);
+        if (match) {
+            matches[match_cnt] = i + length;
+            match_cnt++;
+        }
+
+
+    }
+    if (match_cnt == 0) {
+        return false;
+    }
+    printf("Array: Match Found\n\n");
+    printf("Array: number of matches: %d\n\n",match_cnt);
+    printf("Array: starting bit positions: ");
+    for (i = 0; i < SIZE; i++) {
+        if (!matches[i]) {
+            break;
+        }
+        printf("%d", matches[i]);
+        if (matches[i + 1]) {
+            printf(",");
+        }
+    }
+    printf("\n\n\n");
+    return true;
 }
 
+bool compare_bins (char orig[], char comp[]) {
+
+    int i;
+    for (i = 0; i < SIZE; i++) {
+        if (orig[i] != comp[i]) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
+void i_to_bin(int n, char n_array[]) {
+
+    int i;
+    for (i = 0; i < SIZE; i++) {
+        if (n & (1 << i)) {
+            n_array[i] = '1';
+        }
+    }
+}
 #include "substring.h"
 
 int main(void) {
-    puts("Welcome to subc. The program that searches for sub-binary-c-strings."); /* prints !!!Hello World!!! */
+
+    printf("Welcome to subc. The program that searches for sub-binary-c-strings.\n");
     //puts("What is the first integer?: ");
     int first_int = 0;
     int second_int = 0;
     scanf("%d", &first_int);
     printf("What is the second integer?: ");
     scanf("%d", &second_int);
+    printf("\n");
     bool bitwise = bitwise_find_substring(first_int, second_int);
+    if (!bitwise) {
+        printf("Bitwise: No Match Found\n\n");
+    }
+    bool array = array_find_substring(first_int, second_int);
+    if (!array) {
+        printf("Array: No Match Found\n\n");
+    }
     bool recursive = recursive_find_substring(first_int, second_int);
+    if (!recursive) {
+            printf("Recursive: No Match Found\n\n");
+    }
+
+
+
     return EXIT_SUCCESS;
 }