二进制码错误帮助
Binary code Error help
我忘记插入我的代码了,对不起。
我的程序中有一个二进制搜索,但是当我在数组中输入10个学生记录并对它们进行排序时,student 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<<" nt 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 Numbern " <<key<< "exists at the position n" << k
<< " Student Numbern" << 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
实际上是一个排他的边界,所以将你的第6行替换为:
k=binarySearch(record, 0, N_STUDENT, key); // Serach Array
一般来说,在不理解代码的情况下复制代码是一种糟糕的做功课的方式。总有一天,你的粘贴会被StackOverflow用户忽略,你将无法完成它。
相关文章:
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- C++:需要帮助了解运算符重载错误
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 需要一些帮助来查找我的程序中的逻辑错误
- 我用C++做了一个计算器,但我对"type of calculator"的要求循环了两次。有人可以帮助指出我的错误吗?
- 霍纳斯定律需要代数帮助,显示错误的答案 c++
- 错误:E0018 需要支架 - 需要帮助
- 任何人都可以在带有JNI的Android Studio上帮助我解决此错误,是否缺少一些内容?
- 需要帮助修复分段错误(核心转储)
- 需要帮助识别简单多线程代码中的错误
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- 需要帮助以了解错误消息
- 功能错误中的表达语法帮助!C/C
- For-Loop帮助获得错误的输出
- 帮助错误 C2259:无法实例化抽象类
- 调试帮助:C++错误
- 链接器错误-请帮助:错误LNK2001:无法解决的外部符号
- OpenCV帮助-错误:没有匹配' operator[] '(操作数类型是' cv::Mat '和' int ').&l
- 投票程序CPP帮助(错误输出)