私の研究の一環として、私たちは「ヒープ」の使用について学んでおり、ヒープを参照および参照するためのポインターを使用して短い数学プログラムを作成することを任されました。ちょっとした個人的な学習として、配列を作成してバイナリ検索を使用することで、これを配列で複製しようとしました。しかし、それは単に機能しません。
これが私のコードです:
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
//creating pointers
int* ii = new int;
int* top = new int;
int* bottom = new int;
int* uArray = new int[12];
int* uChoice = new int;
//assigning values in location of pointer
*ii = 5;
*top = 11;
*bottom = 0;
cout<<"Please input a value between 1 and 12 to find in the array: \t";
cin >> *uChoice;
for (int x = 0; x<12; x++) //adding values into the array
{
uArray[x] = x;
cout<<x;
Sleep(1000);//checking loop works
}
while (uArray[*ii] != *uChoice)
{
if (uArray[*ii] > *uChoice)
{
*bottom = *ii;
*ii = (*top + *bottom)/2;
}
else
{
*top = *ii;
*ii = (*top + *bottom) /2;
}
if (*uChoice == *ii)
{
break;
}
}
//clearing pointers.
delete ii;
delete top;
delete bottom;
delete uArray;
ii = 0;
top = 0;
bottom = 0;
uArray = 0;
cout<<uChoice<<" Found at position: \t"<< *ii;
Sleep(10000);
return 0;
}
よろしくお願いします。
[編集:] while ループ内でエラーが発生します。配列を正しく検索していないことを意味する何かが起こっています。申し訳ありませんが、これを明確にしませんでした。