Thread: binary array: won't print results out correctly

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    13

    binary array: won't print results out correctly

    I meant to type in "binary search" not binary array whoops

    hey, just wanted some help with this program. it's not my program, it was copied from a PowerPoint as notes for me to study off of, just putting the disclaimer here (it belongs to Pearson).

    without further ado, here's the code:
    Code:
    #include <stdio.h>#define SIZE 15
    
    
    size_t binarySearch(const int b[], int searchKey, size_t low, size_t high);
    void printHeader(void);
    void printRow(const int b[], size_t low, size_t mid, size_t high);
    
    
    int main(void)
    {
        int a[ SIZE ];
        size_t result;
        int key;
        size_t i;
    
    
    
    
        for (i = 0; i < SIZE; ++i)
        {
            a[ i ] = (2 * i);
        }
    
    
        printf("%s", "Enter a number between 0 and 28:\n");
        scanf("%d", &key);
    
    
        result = binarySearch(a, key, 0, SIZE - 1);
    
    
        if (result != -1)
        {
            printf("\n%d found in array element %d.\n", key, result);
        }
        else
        {
            printf("\n%d not found.\n", key);
        }
    }
    
    
    size_t binarySearch(const int b[], int searchKey, size_t low, size_t high)
    {
        int middle;
    
    
        while (low <= high)
        {
            middle = (low + high) / 2;
    
    
            printRow(b, low, middle, high);
    
    
            if (searchKey == b[ middle ])
            {
                return middle;
            }
            else if (searchKey < b[ middle ])
            {
                high = middle - 1;
            }
            else
            {
                low = middle + 1;
            }
        }
    
    
        return -1;
    }
    
    
    void printHeader(void)
    {
        unsigned int i;
    
    
        puts("\nSubscripts:\n");
    
    
        for (i = 0; i < SIZE; ++i)
        {
            printf("%3u", i);
        }
    
    
        puts("");
    
    
        for (i = 1; i <= 4 * SIZE; ++i)
        {
            printf("\n%s\n", "-");
        }
    
    
        puts("");
    }
    
    
    void printRow(const int b[], size_t low, size_t mid, size_t high)
    {
        size_t i;
    
    
        for (i = 0; i < SIZE; ++i)
        {
            if (i < low || i > high)
            {
            printf("%s", "    ");
            }
            else if (i == mid)
            {
            printf("%3d", b[ i ]);
            }
            else
            {
            printf("%3d", b[ i ]);
            }
        }
    
    
    
    
        puts("");
    }
    What it should print (when you input 8, for example):
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
    ---------------------------------------
    0 2 4 6 8 10 12 14 16 18 20 22 24
    0 2 4 6* 8 10 12
    8 10* 12
    8*

    8 found in array element 4.
    (pretend that the numbers are aligned, the forum won't accept empty spaces)

    Unfortunately, the hyphens and the 0-14 digits don't show up. The asterisks don't, either. According to the PowerPoint, it should work, yet it doesn't. Is it an issue with the compiler I have or the code itself? Thanks in advance!!!
    Last edited by TahiriS719; 04-08-2017 at 04:47 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The header isn't printing because printHeader is never called. And printHeader doesn't print the hyphens properly anyway since it prints a newline after each hyphen. No asterisk is printed because there's no code to print an asterisk.
    Code:
    #include <stdio.h>
    
    #define SIZE 15
    
    size_t binarySearch(const int b[], int key, size_t low, size_t high);
    void printHeader(void);
    void printRow(const int b[], size_t low, size_t mid, size_t high);
    
    int main(void)
    {
        int a[SIZE];
        int key;
        size_t result, i;
    
        for (i = 0; i < SIZE; ++i)
            a[i] = 2 * i;
    
        printf("Enter a number between 0 and %d: ", (SIZE - 1) * 2);
        scanf("%d", &key);
    
        result = binarySearch(a, key, 0, SIZE - 1);
    
        if (result != (size_t)-1)
            printf("\n%d found in array element %zu.\n", key, result);
        else
            printf("\n%d not found.\n", key);
    }
    
    size_t binarySearch(const int a[], int key, size_t low, size_t high)
    {
        printHeader();
    
        while (low <= high)
        {
            int mid = (low + high) / 2;
    
            printRow(a, low, mid, high);
    
            if (key == a[mid])
                return mid;
            else if (key < a[mid])
                high = mid - 1;
            else
                low = mid + 1;
        }
    
        return (size_t)-1;
    }
    
    void printHeader(void)
    {
        size_t i;
    
        for (i = 0; i < SIZE; ++i)
            printf("%3zu ", i);
        putchar('\n');
    
        for (i = 0; i < SIZE; ++i)
            printf("----");
        putchar('\n');
    }
    
    void printRow(const int a[], size_t low, size_t mid, size_t high)
    {
        size_t i;
    
        for (i = 0; i < SIZE; ++i)
        {
            if (i < low || i > high)
                printf("   ");
            else
               printf("%3d", a[i]);
            putchar(i == mid ? '*' : ' ');
        }
        putchar('\n');
    }

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    Quote Originally Posted by algorism View Post
    The header isn't printing because printHeader is never called. And printHeader doesn't print the hyphens properly anyway since it prints a newline after each hyphen. No asterisk is printed because there's no code to print an asterisk.
    Is there a way of saying "putchar(i == mid ? '*' : ' ');" but with printf? I tried placing a printf("*"); under the else if statement, but that messed up the arrangement/alignment of the numbers on the bottom 2 rows. everything else worked great!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You could replace this
    Code:
    putchar(i == mid ? '*' : ' ');
    with this
    Code:
    if (i == mid)
        printf("*");
    else
        printf(" ");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-23-2015, 03:35 PM
  2. Replies: 6
    Last Post: 05-05-2015, 10:57 AM
  3. Print out results in array
    By Archie Brady in forum C Programming
    Replies: 2
    Last Post: 04-21-2013, 10:45 AM
  4. print ut my results
    By vikingcarioca in forum C Programming
    Replies: 7
    Last Post: 04-22-2009, 03:29 AM
  5. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM

Tags for this Thread