非工作时循环&弄乱了二进制搜索

时间:2014-12-11 05:08:32

标签: c++ loops search while-loop binary

对于这个程序,我必须用20个随机数(1-100)填充一个数组,对数组进行排序(降序),然后搜索一个随机键值,并输出该值在数组中的位置。我有两个问题。首先,我必须退出程序的while循环不起作用,我无法弄清楚原因。其次,我的二进制搜索没有返回位置值,我不知道为什么。这段代码将编译。

#include <iostream> 
#include <ctime> 

using namespace std;

int printArray(int *arr, int arraySize);
int fillArrayWithRandomNumbers(int *arr, int arraySize);
int bubbleSortDesc(int *arr, int arraySize);
int binarySearch(int *arr, int arraySize, int key);
int main()
{
    int const arraySize = 20;
    int arr[arraySize];

    cout << "CMPSC 201-Extra Credit\n" << "This program fills an array, and then searches for a random key value." << endl <<endl;
    char stopTheProgram = 'n';

    do {
        int key, result;
    cout << "Unordered array:" << endl;
    fillArrayWithRandomNumbers(arr, arraySize);
    printArray(arr, arraySize);
    cout << endl << "Array after a bubble sort : " <<endl;
    bubbleSortDesc(arr, arraySize);
    printArray(arr, arraySize);
    key = rand()%100;
    cout << endl <<"Searching for " << key << endl;
    result = binarySearch(arr, key, arraySize);
    if (result == -1)
    {
        cout << "Key " << key << " not found in the array" << endl;
    }
    else
    {
        cout << "Key " << key << " found at position " << result << endl;
    }
    cout << "Stop the program? (y/n) ";
    cin >> stopTheProgram;
    cout << endl;
} while (!(stopTheProgram == 'Y' || stopTheProgram == 'y'));

    return 0;
}

int fillArrayWithRandomNumbers(int *arr, int arraySize)
{
    srand((unsigned)time(NULL));
    for (int i = 0; i<arraySize; i++)
    {
        arr[i] = (rand() % 100) + 1;
    }
    return *arr;
}

int printArray(int *arr, int arraySize)
{
    for (int i = 0; i<arraySize; i++)
    {
        cout << arr[i] << " ";
    }
    return *arr;
}

int bubbleSortDesc(int *arr, int arraySize){
    int i, j;
    int temp = 0;
    for (i = 0; i < arraySize; i++)
    {
        for (j = 0; j < arraySize - 1; j++)
        {
            if (arr[j] < arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    return *arr;
}

int binarySearch(int arr[], int key, int arraySize)
{
int i, j;
int temp = 0;
for (i = 0; i < arraySize; i++)
{

    for (j = 0; j < arraySize - 1; j++)
    {
        if (arr[j] > arr[j + 1])
        {
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}

int position, lowerBound = 0;
position = (lowerBound + arraySize) / 2;

while ((arr[position] != key) && (lowerBound <= arraySize))
{
    if (arr[position] > key)
    {
        arraySize = position - 1;
    }
    else
    {
        lowerBound = position + 1;
    }
    position = (lowerBound + arraySize) / 2;
}
if (lowerBound <= arraySize)
{
    position = 19 - position;
    return position;
}
else {
    return -1;
}
}

解决:我现在已经修复了二进制搜索的问题,并退出了我的while循环。我将把这个留在这里以防万一有人跟我一起教授。所以回顾一下,这个程序用20个随机数(范围1-100)填充数组,按降序对数组进行排序,然后创建一个随机键值(1-100之间)。然后它对数组进行冒泡排序,使其按升序排列,使用二进制搜索在数组中查找键值,最后输出该键值在数组中的位置。

1 个答案:

答案 0 :(得分:0)

我可以看到您的代码存在两个问题。首先,你正在影响stopTheProgram变量。您可以在do / while循环之前定义一次,并将其初始化为&#39; n&#39 ;;然后一旦进入do / while,你定义另一个stopTheProgram。由于范围界定这是一个问题。在do / while循环内部,来自用户的输入被分配给本地stopTheProgram(在do / while中定义)但是不再在循环外部退出。因此,始终使用全局范围的stopTheProgram评估while循环表达式,该值设置为&#39; n&#39;。所以删除第二个定义。第二个是控制while循环的表达式的问题。它总是评估真实。如果你无法想象它,请绘制一个真值表。如果stopTheProgram =&#39; Y&#39;那么stopTheProgram!=&#34; Y&#34; || stopTheProgram!=&#39; y&#39;是0 || 1总是如此。如果stopTheProgram =&#39; y&#39;然后停止TheProgram!=&#34; Y&#34; || stopTheProgram!=&#39; y&#39;是1 || 0总是如此。这有效:while(!(stopTheProgram ==&#39; Y&#39; || stopTheProgram ==&#39; y&#39;)