0

I thought I finally made the program find work, but it still won't function when I test it with help50. I have looked at the code the whole day and just cannot seem to figure out what to do to solve my problem.

I would like to ask if someone here can maybe have a look. This is my first post, so maybe I am doing this completely wrong. I upload the code form the helpers.c file, if more code is needed, I will upload that too.

This was the feedback I got:

:) helpers.c exists
:) helpers.c compiles.
:( sorts {5,4,3,2,1}
    expected "2", not "\n4\n5\n"
:( sorts {5,3,1,2,4,6}
    expected "3", not "\n4\n5\n6\n"
:( finds 28 in {28,29,30}
    timed out while waiting for program to exit
:) finds 28 in {27,28,29}
:( finds 28 in {26,27,28}
    timed out while waiting for program to exit
:) finds 28 in {27,28,29,30}
:( finds 28 in {26,27,28,29}
    timed out while waiting for program to exit
:( finds 28 in {25,26,27,28}
    timed out while waiting for program to exit
:( doesn't find 28 in {25,26,27}
    timed out while waiting for program to exit
:( doesn't find 28 in {25,26,27,29}
    timed out while waiting for program to exit
:( doesn't find 28 in {29,30,31,32}
    timed out while waiting for program to exit
:( doesn't find 28 in {29, 30, 31}
    timed out while waiting for program to exit
:( finds 28 in {30,27,28,26}
    timed out while waiting for program to exit

enter code here

/**
 * helpers.c
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>
#include <stdio.h>
#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    int start = 0;
    int end = n - 1;
    int mid = (start + end)/2;
    int needle = value;

    do
    {
        if (needle == values[mid])
        {
            return true;
        }
        else if (needle < values[mid])
        {
            end = mid - 1;
        }
        else
        {
            start = mid + 1;

        }
    }
    while (start <= end);

    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    int space;
    int swaps = 1;

    do
    {
        for(int i = 0; i < n - 1; i++)
        {
            swaps = 0;

            if(values[i] > values[i+1])
            {
                space = values[i+1];
                values[i+1] = values[i];
                values[i] = space;
                swaps++;
            }
        }
    }
    while (swaps != 0);

    return;
}

1 Answer 1

0

It is failing because the sort function is not working correctly, although it is close. Look at where the code sets swaps = 0. By setting it inside the for loop, it will only show that a swap occurred if the swap was the last pair of elements in the list. Any swaps earlier than that will not be recorded because swaps is reset to 0 on the next pass. Without a sorted list, the seek function won't be reliable.

Great job in resetting start or end as needed in seek. What about mid?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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