简体   繁体   中英

CS50x pset3 (Why doesn't my program pass Check50?)

I completed my Problem Set 3 helpers.c program and it works perfectly up to a total of 10 of haystack but stops working when I press Control-D with less than 10 in haystack. Instead the program skips a line and I can freely write like in a not pad. Since, it can not pass 3 or 4 in haystacks, my program can't pass Check50. Does anybody have a solution to this problem?

In case you need my code, here it is:

bool search(int value, int values[], int n)
{
     if(value < 0)
     {
         return false;
     }

     for(int i = 0; i < n; i++)
     {
         if (value == values[i])
         {
            return true;
         }
     }

     return false;

 }

 /**
 * Sorts array of n values.
 */
 void sort(int values[], int n)
 {
     bool tf;
     do
     {
         tf = false;
         for(int i=0; i < n-1; i++)
         {
             if(values[i] > values[i+1])
             {
                 int temp = values[i];
                 values[i] = values[i+1];
                 values[i+1] = temp;
                 tf = true;
             }
         }
     }
     while(tf == false);
     return;
 }

Your sorting algorithm is flawed. It just takes one round of the whole array and arranges it partially in order but not fully.

You can use bubble sort (refer to https://en.wikipedia.org/wiki/Bubble_sort or the lecture of cs50) of which I cannot provide code as it is against the honour code of the course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM