Search code examples
carrayssortingsearchbubble-sort

search and sorting in c program?


I am writing a program which receives some numbers from the client and then sort them with Bubble sorting function and the other function which receive one number from client and then search it between the other number with binary search function .Please inform me what the problem of this program is?

#include <stdio.h>

int bobsort (int);
int searchi (int);

void main ()
{

    int num, i;
    printf ("Enter Count Number \n");
    scanf ("%d", &num);
    int array[num];
    for (i = 1; i <= num; i++) {
        printf ("Enter Number %d \n", i);
        scanf ("%d", &array[i - 1]);
    }

    bobsort (num);
    searchi (num);

    getch ();

//return 0;
}

//**** function bobsort
void bobsort (int n)
{
    int c, d, swap;
    for (c = 0; c < (n - 1); c++) {
        for (d = 0; d < n - c - 1; d++) {
            if (array[d] > array[d + 1]) {      /* For decreasing order use < */
                swap = array[d];
                array[d] = array[d + 1];
                array[d + 1] = swap;
            }
        }
    }

    printf ("Sorted list in ascending order:\n");

    for (c = 0; c < n; c++)
        printf ("%d\n", array[c]);

    // return 0;
}

//**** function search
int searchi ()
{
    int c, first, last, middle, n, search;

    printf ("Enter value to find\n");
    scanf ("%d", &search);

    first = 0;
    last = n - 1;
    middle = (first + last) / 2;

    while (first <= last) {
        if (array[middle] < search)
            first = middle + 1;
        else if (array[middle] == search) {
            printf ("%d found at location %d.\n", search, middle + 1);
            break;
        } else
            last = middle - 1;

        middle = (first + last) / 2;
    }
    if (first > last)
        printf ("Not found! %d is not present in the list.\n", search);

    return 0;
}

Solution

  • You had several compilation errors, below I rewrote your code with comments so that it can compile. The actual functions seem to work just fine

    The most important change was passing array to both functions. It is passed as int* so that the functions can modify array in the context of main

    #include <stdio.h>
    
    // Neither one of these need to return a value, both are now void
    // Both need to have array passed, added int*
    void bobsort(int, int*);
    void searchi(int, int*);
    
    int main() {
        int num, i;
    
        printf("Enter Count Number\n");
        scanf("%d", &num);
    
        int array[num];
        for (i = 1; i <= num; i++) {
            printf("Enter Number %d\n", i);
            scanf("%d", &array[i-1]);
        }
    
        // Pass array to both functions
        bobsort(num, array);
        searchi(num, array);
    
        // Don't know what this was supposed to be, commented out
        //getch();
    
        return 0;
    }
    
    // Receives array from main
    void bobsort(int num, int* array) {
        int c, d, swap;
        for (c = 0; c < num-1; c++) {
            for (d = 0; d < num-c-1; d++) {
                if (array[d] > array[d+1]) {
                    swap = array[d];
                    array[d] = array[d+1];
                    array[d+1] = swap;
                }
            }
        }
    
        printf("Sorted list in ascending order:\n");
        for (c = 0 ; c < num; c++)
            printf("%d\n", array[c]);
    }
    
    // Receives array from main
    void searchi(int num, int* array) {
        int c, first, last, middle, search;
    
        printf("Enter value to find\n");
        scanf("%d", &search);
    
        first = 0;
        last = num-1;
        middle = (first+last)/2;
    
        while (first <= last) {
            if (array[middle] < search)
                first = middle+1;
            else if (array[middle] == search) {
                printf("%d found at location %d\n", search, middle+1);
                break;
            }
            else
                last = middle-1;
    
            middle = (first+last)/2;
        }
        if (first > last)
            printf("Not found! %d is not present in the list\n", search);
    }