0

the command line yells at me but I cannot find out why. Any ideas what could be wrong?

commald line

Here is my code: /** * helpers.c * * Helper functions for Problem Set 3. */

include

include "helpers.h"

/** * Returns true if value is in array of n values, else false. */ bool search(int value, int values[], int n)

// BINARY search function. { int start; int mid; int end;

if (n <= 0)
{
    return false;
}

// value is in values -> true
do
{
    // look at middle of list
    start = 0;
    end = n - 1;
    mid = end / 2; // ?? CORRECT?

    if (values[mid] == value)
    {
        return true;
    }

    // search left
    else if (values[mid] > value)
    {
        end = mid - 1;
    }

    // search right
    else if (values[mid] < value)
    {
        start = mid + 1;
    }

}
while (start + 1 < end);

// value is not in values -> false.
return false;

}

/** * Sorts array of n values. */ void sort(int values[], int n)

{ int i; int min; int j; int temp;

// selection sort.

for (i = 0; i < n - 2; i++)
{
    // define minimum at first element.
    min = i;

    for (j = i + 1; j < n; j++)
    {
        if (values[j] < values[min])
        {
            min = j;
        }

        // swapping of values trough rotation: new ram in temp. new found min gets into temp. New min gets old i. i gets new min from temp.
        if (min != i)
        {
            temp = values[min];
            values[min] = values[i];
            values[i] = temp;
        }
    }

}

}

1 Answer 1

1

In my opinion, sort should use i < n - 1 not -2.

In search, start = 0; end = n - 1; needs to go before the while loop, otherwise you would reset the interval each time, creating an infinite loop. Use while, not do..while, and use the right condition (loop as long as interval has elements, which is slightly different from your condition). mid should stay in the loop and be the average of start and end.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .