2016-10-15 1 views
-1

CS50、Pset3を使い、助けや忍耐を求めています。 私は...しかし、それが接続されていません。.. find.cを呼び出すための正しい機能を有するように実装さhelpers.cにしようとしている 同じバイナリ検索は機能しませんが、別の環境で働いています

は私が testBinSearchをというタイトルの別の作品を行なったし、それが仕事をしました。同じコードで..誰かが私になぜ.. ..教えてもらえますか?ここで
/** 
* helpers.c 
* 
* Computer Science 50 
* Problem Set 3 
* 
* Helper functions for Problem Set 3. 
*/ 
#include <stdio.h>  
#include <cs50.h> 

#include "helpers.h" 

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

{ 
    // TODO: implement a Binary searching algorithm (You are welcome to take an iterative approach (as with a loop) or a recursive approach (wherein a function calls itself).) 



     //define startPoint. numberOfArrayElements(aka size) - (numberOfArrayElements(aka size) - 1) or Element[0] 

     //define endPoint. numberOfArrayElements(aka size) 
     int endPoint = n - 1; //element! we -1 because array start from 0th element. last element of array that is 5 elements big will thus be (total number of Elements - 1)th element. 

     //define midPoint. numberOfArrayElements(aka size)/2 
     int midPoint = endPoint/2; //element! 

     //while loop? 
     while(n > 0) 
      { 
       //if midPoint == needle, return 0 
       if(values[midPoint] == value) 
       { 
        return 0; 
       } 

       //////////(if midPoint is smaller(to the left) or larger(to the right) than needle) 
       //ELSE IF midPoint > than needle(look left), keep startPoint, change endPoint element to values[midPoint - 1], define midPoint again. 
       else if(values[midPoint] > value) 
       { 
        endPoint = midPoint - 1; 
        midPoint = endPoint/2; 
        n = endPoint; 
        printf("mid point is more than needle\n"); 
       } 
       //ELSE midPoint < than needle(look right), keep endPoint, change Startpoint element to values[midPoint + 1], define mindPoint again. 
       else if(values[midPoint] < value) 
       { 
        int startPoint = midPoint + 1; 

        //define midpoint again 
        midPoint = (endPoint + startPoint)/2; 
        n = endPoint - startPoint + 1; 
        printf("mid point is less than needle\n"); 
       } 


      } 



     printf("cued the while loop return 1\n"); 
     return 1; 
} 

/** 
* Sorts array of n values. Done with Insertion sort* 
*/ 
void sort(int values[], int n) 
{ 
    //declare variable 
    int element; 

    //number of iterations (or passes?). Skip first because first array is already sorted 
    for (int i = 1; i < n; i++) 
     { 
      //value of element moving into sorted portion 
      element = values[i]; 

      //declare variable 
      int j = 0; 

      //index into the unsorted portion 
      j = i; 

      //iterate sorted portion from right to left while sorted portion is greater than 'Element' being compared in this iteration of i. 
      //basically, it stops this loop once the 'Element' is placed to the left of all greater&&sorted numbers. 
      while(j > 0 && values[j - 1] > element) 
      { 
       //shift all sorted positions to the right 
       values[j] = values[j - 1]; 

       // this enables the loop to move left through the sorted portion 
       j = j - 1; 

      } 

      //insert temp holder value into the position which is now empty because all sorted&&greater number are to the right of 'Element' 
      values[j] = element; 

     } 


     for(int k = 0; k < n; k++) 
     //print to check 
      { 
       printf("{%i}<-- number in %i-th array (sorted)\n", values[k], k); 

      } 
} 

find.cコードです:

/** 
* find.c 
* 
* Computer Science 50 
* Problem Set 3 
* 
* Prompts user for as many as MAX values until EOF is reached, 
* then proceeds to search that "haystack" of values for given needle. 
* 
* Usage: ./find needle 
* 
* where needle is the value to find in a haystack of values 
*/ 

#include <cs50.h> 
#include <stdio.h> 
#include <stdlib.h> 

#include "helpers.h" 

// maximum amount of hay 
const int MAX = 65536; 

int main(int argc, string argv[]) 
{ 
    // ensure proper usage 
    if (argc != 2) 
    { 
     printf("Usage: ./find needle\n"); 
     return -1; 
    } 

    // remember needle 
    int needle = atoi(argv[1]); 

    // fill haystack 
    int size; 
    int haystack[MAX]; 
    for (size = 0; size < MAX; size++) 
    { 
     // wait for hay until EOF 
     printf("\nhaystack[%i] = ", size); 
     int straw = GetInt(); 
     if (straw == INT_MAX) 
     { 
      break; 
     } 

     // add hay to stack 
     haystack[size] = straw; 
    } 
    printf("\n"); 

    // sort the haystack 
    sort(haystack, size); 

    // try to find needle in haystack 
    if (search(needle, haystack, size)) 
    { 
     printf("\nFound needle in haystack!\n\n"); 
     return 0; 
    } 
    else 
    { 
     printf("\nDidn't find needle in haystack.\n\n"); 
     return 1; 
    } 

} 

そして最後に、ここで私は1つのファイルにそれらをすべてキーイングする際、別々に働いていた(あるいは少なくとも動作しているようです)のコードだ...題しtestBinSearch以下

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

void sort(int array[], int NumberOfElements); 
bool search(int value, int values[], int n); 

int main(void) 


{ 
    //decalre variable 
    int NumberOfElements; 

    printf("how many Element would you like in this array?\n"); 
    NumberOfElements = GetInt(); 

    //declare variable for array 
    int array[NumberOfElements]; 


    for(int i = 0; i < NumberOfElements; i++) 
     { 
      printf("alright, please key in value of each element\n"); 
      array[i] = GetInt(); 
     } 

    sort(array, NumberOfElements); 

    for (int i = 0; i < NumberOfElements; i++) 
     { 
      printf("alright, here is your array sorted, element %i is %i\n", i, array[i]); 
     } 

    printf("value ot search for?\n"); 
    int value = GetInt(); 
    search(value, array, NumberOfElements); 
} 


//---------- 
void sort(int array[], int NumberOfElements) 
{ 
    //declare variable 
    int element; 

    //number of iterations (or passes?). Skip first because first array is already sorted 
    for (int i = 1; i < NumberOfElements; i++) 
     { 
      //value of element moving into sorted portion 
      element = array[i]; 

      //declare variable 
      int j = 0; 

      //index into the unsorted portion 
      j = i; 

      //iterate sorted portion from right to left while sorted portion is greater than 'Element' being compared in this iteration of i. 
      //basically, it stops this loop once the 'Element' is placed to the left of all greater&&sorted numbers. 
      while(j > 0 && array[j - 1] > element) 
      { 
       //shift all sorted positions to the right 
       array[j] = array [j - 1]; 

       // this enables the loop to move left through the sorted portion 
       j = j - 1; 

      } 

      //insert temp holder value into the position which is now empty because all sorted&&greater number are to the right of 'Element' 
      array[j] = element; 

     } 

} 

//-------------- 
bool search(int value, int values[], int n) 

{ 
    // TODO: implement a Binary searching algorithm (You are welcome to take an iterative approach (as with a loop) or a recursive approach (wherein a function calls itself).) 

    //variables declaration 
    //int startPoint; 
    //int endPoint; 
    //int midPoint; 

     //define startPoint. numberOfArrayElements(aka size) - (numberOfArrayElements(aka size) - 1) or Element[0] 

     //define endPoint. numberOfArrayElements(aka size) 
     int endPoint = n - 1; //element! 

     //define midPoint. numberOfArrayElements(aka size)/2 
     int midPoint = endPoint/2; //element! 

     //while loop? 
     while(n > 0) 
      { 
       //if midPoint == needle, return 0 
       if(values[midPoint] == value) 
       { 
        printf("found it!\n"); 
        return 0; 
       } 

       //////////(if midPoint is smaller(to the left) or larger(to the right) than needle) 
       //ELSE IF midPoint > than needle(look left), keep startPoint, change endPoint element to values[midPoint - 1], define midPoint again. 
       else if(values[midPoint] > value) 
       { 
        endPoint = midPoint - 1; 
        midPoint = endPoint/2; 
        n = endPoint; 
       } 
       //ELSE midPoint < than needle(look right), keep endPoint, change Startpoint element to values[midPoint + 1], define mindPoint again. 
       else if(values[midPoint] < value) 
       { 
        int startPoint = midPoint + 1; 

        //define midpoint again 
        midPoint = (endPoint + startPoint)/2; 
        n = endPoint - startPoint + 1; 
       } 


      } 


     printf("could not find it\n"); 
     return 1; 
} 

誰かが私を助けて、どこが間違っているのか教えてください。私はコードを思いついてすぐにコピーしましたが、(testBinSearch)とか1つは(helpers.c)..でした。

+1

デバッグヘルプ(「なぜこのコードは機能していませんか?」)には、目的の動作、特定の問題またはエラー、および質問自体に再現するのに必要な最短コードが含まれている必要があります。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:最小、完全、および検証可能な例を作成する方法。 – Olaf

+1

バイナリサーチ関数の酸性テストでは、N個のエントリ(Nのさまざまな値、たとえば1. 129)の配列を作成し、配列Dに値D [n] = n * 2;をロードしますnを0 .. N-1(ソートされたデータの配列)に変換し、-1 .. 2N-1のすべての値に対して値Vの検索が正しいことをチェックします。奇数の場合、検索は失敗します。偶数値については、正しい値(テストで確認できる値)を見つけるはずです。あなたのコードはそのようなテストに合格しません。提案されたテストハーネスはユーザーとのやりとりを必要としないので、フラットアウトすることができます。 –

答えて

2

私はこの計算これは問題全体をカバーする場合はわからないが、とにかく...

midPoint = endPoint/2; 

は間違っています。

100個の要素の配列があるとします。このコードでは、中間点(たとえば87)でインデックス75〜99を表示する状況、つまりsmaller thanのパスを2〜3回撮影した状況になる場合があります。あなたは中点(例えば43)は、さらに関心

の範囲外にある計算greater than参加する場合

次に、始点変数はsmaller thanケース内部変数ではないことです。エンドポイントと同じレベルでなければなりません。各ループで、開始点またはエンドポイントを変更する必要があります。中点の計算は常に始点と終点の両方に依存する。

+0

ありがとう!私はそれをすべて整理することができました。 @olaf申し訳ありませんが、私は間違いなく次回より注意します。ここで大いに助けてくれてありがとう、本当に愛しいコミュニティがあるのが大好きです:D – olafironfoot

関連する問題