0

When running check50, I see according to the results that continue to miss some of my loop iterations and can't figure out why. I've moved around the "returns" but still come up with missing "find"s or missing "doesn't find"s.

/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * 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)
{
    // TODO: implement a searching algorithm
   int i = 0;
   if (n < 0)
   {
       return false;
   }
    else
        {
            while (i < n)

            {
                if (values[i] == value)
                {
                    return true;
                }
                else if (values[i] != value)
                {
                    i++;
                }

            }
        }

return false;

}

linear search results

1
  • Not enough of the code to see what's happening. Please post the entire function. Also, please cut and paste the code instead of posting an image. It's impossible to copy the code to an IDE for testing when you post an image. Finally, review the following link for info on how to post code: meta.stackexchange.com/questions/22186/… – Cliff B Jul 21 '16 at 21:10
0

Your search() function works correctly and passed check50 when added to my code. That tells me that you have an issue with your sort() function or the values being passed to search. (The latter should not be a problem unless you changed the find.c file. )

Try adding some test code to the end of your sort routine to print out the sorted list. Verify that the list is being sorted and not dropping any elements.

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

2
  • Thank you, again, Cliff. Just to be clear on my end, so that I know I'm NOT changing the find.c file--Is it correct that I am saving my helpers file and then "making" my find program? ("make find"). I really hope I haven't messed with any of the bones of my files! – Karen Safrit Jul 21 '16 at 23:02
  • Yes, no changes to find.c. Update helpers.c as needed except that you can't change the function signatures, as specified in the pset. – Cliff B Jul 21 '16 at 23:14

You must log in to answer this question.

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