搜索三维阵列
Search a 3d Array
我需要帮助输入搜索数组。我试着进行二进制搜索,但无法使其工作。其他一切都正常,直到我把我要搜索的值放在数组中,然后它就崩溃了。
它的工作原理:输入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;
}
您将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)
所以,让我们拿出一张纸和一支铅笔。由于"first"为0,"last"为999,因此将"middle"设置为449。
因此,我们有两个问题:
1) 您使用"="运算符进行赋值,而不是使用"=="进行比较,这似乎是您的意图。
2) 您正在为myArray[449][449][449]分配一个值。不幸的是,你的阵列要小得多:
int myArray[10][10][10] = { 0 };
尝试访问由十个元素组成的第449个阵列中的第449's个元素,以及由十个元件组成的第448's个阵列,不会很好地工作。即使你在比较或分配一些东西,无论哪种方式,这都是未定义的行为,几乎可以保证崩溃。
相关文章:
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 如何使用qt缩放三维网格
- 三维数组中的C/C++DWORD到BYTE和BYTE到DWORD的转换
- 使用vtkImageReslice重新切片三维原始图像
- 特征:创建一个具有函数的三维阵列
- 扩充矩阵的行缩减-三维样条曲线计算
- 使用pcl transformcloud将三维点平移并旋转到原点
- 如何将X 2维数组连接到一个三维阵列中
- 如何将 HDC 位图快速复制到三维阵列?
- PyArray_New或PyArray_SimpleNewFromData指定三维阵列的尺寸
- 三维阵列遍历
- 将三维阵列(矢量)转换为vtk非结构化网格
- 为三维阵列中的特定图元创建对象
- 正在处理将三维阵列保存到文件并加载
- 从阵列中查找重复的三维顶点
- 搜索三维阵列
- 使用C++从MAT文件导入三维阵列
- 三维阵列偏移量计算
- 三维阵列动态分配问题
- 动态三维阵列上的OpenGL glDrawPixels