2014-10-07 116 views
1

我需要输入搜索数组的帮助。我试图把二进制搜索,但我不能得到它的工作。一切工作,直到我把我在数组中搜索的值,然后它只是崩溃。搜索3d阵列

它是如何想的工作: 输入2与坐标值为每个 然后计算它们之间的距离 那么想,让用户搜索的坐标值和状态,如果发现哪个坐标是伟大的。

感谢

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> //for setprecision 
#include <math.h> 
#include <cstdbool> 
#include <cstdlib> // Needed for rand and srand 
#include <ctime> // Needed for the time function 


using namespace std; 


//Function Prototypes 
void processThroughArray(int[][10][10], int, int, int, int, int, int); 
int searchArray(int[][10][10], int, int, int, int, int, int, int, int); 
const int SIZE = 10; 

int main() 
{ 

// establish array and set all values to 0 
int myArray[10][10][10] = { 0 }; 

// establish x and y position markers 
int x = 0; 
int y = 0; 
int z = 0; 
int x2 = 0; 
int y2 = 0; 
int z2 = 0; 


// establish input for x and y from the user 
int xInput = 0; 
int yInput = 0; 
int zInput = 0; 
int xInput2 = 0; 
int yInput2 = 0; 
int zInput2 = 0; 

// variable for value entered 
int inputValue = 0; 
int inputValue2 = 0; 
double distance = 0; 
int searchValue; 
int result; 


// Get the user's value and coordinate 
cout << "\nPlease enter the x coordinate "; 
cin >> xInput; 
cout << "\nPlease enter the y coordinate "; 
cin >> yInput; 
cout << "\nPlease enter the z coordinate "; 
cin >> zInput; 


cout << "\nPlease enter the value to place in " << xInput << "," << yInput << "," << zInput << " "; 
cin >> inputValue; 

// Get the user's ending value and coordinate 
cout << "\nPlease enter the ending x coordinate "; 
cin >> xInput2; 
cout << "\nPlease enter the ending y coordinate "; 
cin >> yInput2; 
cout << "\nPlease enter the ending z coordinate "; 
cin >> zInput2; 


cout << "\nPlease enter the value to place in " << xInput2 << "," << yInput2 << "," << zInput2 << " "; 
cin >> inputValue2; 

// place the value in the coordinate 
myArray[xInput][yInput][zInput] = inputValue; 
cout << "\nYou have successfully placed the value " << inputValue << " in coordinate " << xInput << ", " << yInput << ", " << zInput << " "; 

myArray[xInput2][yInput2][zInput2] = inputValue2; 
cout << "\nYou have successfully placed the value " << inputValue2 << " in coordinate " << xInput2 << ", " << yInput2 << ", " << zInput2 << " "; 

//Function performing for loop 
processThroughArray(myArray, x, y, z, x2, y2, z2); 

//calculate distance between the two coordinates 
distance = sqrt(pow(xInput2 - xInput, 2.0) + pow(yInput2 - yInput, 2.0) + pow(zInput2 - zInput, 2.0)); 
cout << "\nThe distance between " << xInput << "," << yInput << "," << zInput << " and " << xInput2 << "," << yInput2 << "," << zInput2 << " is "; 
cout << setprecision(4) << distance << endl; 


// indicate end of array processing 
cout << "\nArray Processed" << endl; 

//User inputs value to search for 
cout << "Enter the value you wish to look for: "; 
cin >> searchValue; 

result = searchArray(myArray, SIZE, searchValue, x, y, z, x2, y2,z2); 

//If results contains a -1 the value not found 

if (result == -1) 
{ 
    cout << "That number does not exists in the array.\n"; 
} 

else 
{ 
    cout << "\nValue " << searchValue; 
    cout << " is located at position: " << result << endl; 

} 




system("pause"); 
return 0; 
} 


//************************************************************************** 
// Definition of function processThroughArray: Process through the array * 
//the for loop                * 
//************************************************************************** 
void processThroughArray(int myArray[][10][10], int x, int y, int z, int x2, int y2, int z2) 
{ 

for (int x = 0, x2 = 0; x<10, x2 < 10; x++, x2++) 
{ 
    for (int y = 0, y2 = 0; y<10, y2 < 10; y++, y2++) 

    { 
     for (int z = 0, z2 = 0; z< 10, z2 < 10; z++, z2++) 
     { 

      // Display the value of the coordinate 
      cout << "\nCordinate " << x << ", " << y << ", " << z << " value is " << myArray[x, x2][y, y2][z, z2]; 

     } 
    } 
} 



} 

//************************************************************************** 
// Definition of function searchArray: search array for the value input * 
//                   * 
//************************************************************************** 
int searchArray(int myArray[][10][10], int size, int value,int x, int y, int z, int x2, int y2, int z2) 
{ 
int index = 0; 
int position = -1; 
bool found = false; 


while (index < size && !found) 
{ 
    if (myArray[index][index][index] == value) 
    { 
     found = true; 
     position = index; 

    } 
    index++; 

} 

return position; 
} 

回答

0

你定义SIZE为1000,而你把它传递给searchArray():

const int SIZE = 1000; 

// ... 

searchArray(myArray, SIZE, searchValue); 

你正在做searchArray()以下:

int searchArray(int myArray[][10][10], int size, int value) 
{ 
    int first = 0, 
     last = size - 1, 

由于size为1000,所以您将“first”设置为0,将“last”设置为999.

然后,你做以下(留出一些无关痛痒的东西):

middle = (first + last)/2; 
if (myArray[middle][middle][middle] = value) 

那么,让我们拿出一张纸和一支铅笔。由于“第一”为0,“最后”是999,这台“中”向449

所以,我们这里有两个问题:

1)您使用的是“=”操作符分配,而不是比较使用“==”,这似乎是你的意图。

2)您正在为myArray [449] [449] [449]赋值。不幸的是,你的阵列要小得多:

int myArray[10][10][10] = { 0 }; 

尝试访问十个元素的第449位阵列的第449位的元素,十元十个数组的第449位阵列中,不会很好地工作。即使你正在比较,或者分配某种东西,但这是未定义的行为,并且几乎可以保证崩溃。

+0

这里有一个更新。 我已经通过线性搜索,但不是很确定我在这里做错了什么。 我目前遇到的问题是它没有正确地搜索数组。 它找到第一个数字,但找不到第二个数字。 也仍然不能把它放在坐标上,但我相信我需要重点解决第一部分,然后再继续。 谢谢,安吉拉 – eDz 2014-10-08 19:19:32

+0

*更新了上面的代码。 – eDz 2014-10-08 19:29:58

+0

代码的新版本将检查myArray [0] [0] [0],然后myArray [1] [1] [1]等,直到myArray [9] [9] [9]。它将仅搜索数组中的十个元素,并完全忽略其他990.因此,除非您正在搜索只能在这10个坐标中找到的内容,否则您不会找到它。 – 2014-10-08 22:33:10