二进制代码错误帮助

| 我忘了输入代码,对不起。 我的程序中有一个二进制搜索,但是当我在数组中输入10个学生记录并对它们进行排序时,将不会从二进制搜索中获取学生ID的最后一个元素。 假设当我对数组进行排序并且232是数组中的最后一个元素时,当我转到搜索232时,二进制搜索功能会给我找回未找到的东西,并且我会在数组中查找任何其他ID并将其与记录一起返回。
  else if ( choice == 4) // Binary Search... This Also Force Array to be Sorted If Array is not Sorted. 


        {


            merge_sort(0,N_STUDENT-1);

            cout<<\" \\n\\t Enter the student number to search :\"; // Ask user to Input Student ID. 
            cin>>key;

            k=binarySearch(record, 0, N_STUDENT-1, key); // Serach Array 

            if(k>=0) 

                cout<<\"Student Details with student Number\\n \"  <<key<<  \"exists at the position \\n\"  << k
                << \" Student Number\\n\" << record->student_number << \"  \"  << \" Student Name \\n\" <<  record->studentname << \"  \" << \" Student Address \\n\" << record->Address << \"   \" << \" Course Code \\n\"  <<  record->CourseCode << \"   \"   <<  \" Course Name \\n\" << record->CourseName; //Displays Position of Student And Student Details.
            else 
                cout<< \"Not found \"; // if Record is not Found 


_____________________________________________________________________________________________________

//function binary search using the key value to serach 


int binarySearch(student  sorted[], int first, int upto, int key) {  // Sort Array if not Sorted... 

    while (first < upto) {
        int mid = (first + upto) / 2;  // Compute mid point.
        if (key<sorted[mid].student_number)
        {
            upto = mid;     // repeat search in bottom half.
        } else if (key>sorted[mid].student_number) 
        {
            first = mid + 1;  // Repeat search in top half.
        } else 
        {
            return mid;     // Found it. return position
        }
    }
    return -(first + 1);    // Failed to find key
}
已邀请:
容易,您的
upto
实际上是排他的界限,因此将您的第六行替换为:
k=binarySearch(record, 0, N_STUDENT, key); // Serach Array 
一般来说,在不了解代码的情况下复制代码是做作业的不佳方法。到时候,您看起来可怕的粘贴将被StackOverflow用户忽略,您将无法完成它。

要回复问题请先登录注册