在数组中搜索值
Search value within array
在我的代码中,我试图搜索整个数组以找到一个值。我正在添加细节。很多细节。我是个初学者。
这是我在UTD的计算机科学课程。
// 2DimensionalArrayExp1
// This program is part of Project 2
// As part of your assignment on this program, you will turn the process through the array
// into a function and add a third dimension
#include <iostream>
#include <cstdlib> // Needed for rand and srand
#include <ctime> // Needed for the time function
#include <math.h> // sqrt function
using namespace std;
void processThrough(int array[10][10][10]);
void searchArray(int array[10][10][10]);
int main()
{
// estabish array and set all values to 0
int myArray[10][10][10] = { 0 };
int myArray2[10][10][10] = { 0 };
// establish x and y position markers
int x = 0;
int y = 0;
int z = 0;
// establish input for x and y from the user
int xInput = 0;
int yInput = 0;
int zInput = 0;
int x2Input = 0;
int y2Input = 0;
int z2Input = 0;
// variable for value entered
int inputValue = 0;
int inputValue2 = 0;
double distance = 0;
// 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;
cout << "nPlease enter the x coordinate ";
cin >> x2Input;
cout << "nPlease enter the y coordinate ";
cin >> y2Input;
cout << "nPlease enter the z coordinate ";
cin >> z2Input;
cout << "nPlease enter the value to place in " << x2Input << "," << y2Input << ", " << z2Input << " ";
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[x2Input][y2Input][z2Input] = inputValue2;
cout << "nYou have successfully placed the value " << inputValue2 << " in coordinate " << x2Input << ", " << y2Input << ", " << z2Input << " ";
// Process through the array
processThrough(myArray);
// indicate end of array processing
cout << "nArray Processed" << endl;
distance = sqrt(((x2Input - xInput) * (x2Input - xInput)) + ((y2Input - yInput) * (y2Input - yInput)) + ((z2Input - zInput) * (z2Input - zInput)));
cout << "nThe distance between the two points is: " << distance;
searchArray(myArray);
cin.get();
cin.get();
return 0;
}
void processThrough(int array[10][10][10]) {
// Process through the array
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
for (int z = 0; z < 10; z++) {
// Display the value of the coordinate
cout << "nCoordinate " << x << ", " << y << ", " << z << " " << "value is " << array[x][y][z];
}
}
}
}
void searchArray(int array[10][10][10])
{
int searchValue, x, y, z;
cout << "nEnter value: ";
cin >> searchValue;
for (int x = 0; x < 10; x++)
{
if (array[x][y][z] == searchValue)
{
}
for (int y = 0; y < 10; y++)
{
for (int z = 0; z < 10; z++) {
}
}
}
}
也许最后?请告诉我怎么了!
首先,您在searchArray
函数中定义了两个变量x, y, z
。然后在未定义的索引上比较array[x][y][z]
中的值,因为您没有初始化这些变量。正确的代码应该是这样的:
void searchArray(int array[10][10][10])
{
int searchValue;
cout << "nEnter value: ";
cin >> searchValue;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
for (int z = 0; z < 10; z++)
{
if (array[x][y][z] == searchValue)
{
//Whatever you want to do
}
else
{
cout << "Value not found";
}
}
}
}
}
searchArray函数有两个大问题。
-
您可以定义
x
、y
和z
两次。循环中充当索引的变量会覆盖您在函数顶部声明的变量。 -
出于某种原因,您在循环的最深部分之外搜索
array[x][y][z]
。
试试这个:
std::tuple<int,int,int> searchArray(int array[][][])
{
int searchValue;
cout << "nEnter value: ";
cin >> searchValue;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
for (int z = 0; z < 10; z++)
{
if (array[x][y][z] == searchValue)
{
return std::make_tuple(x, y, z);
}
}
}
}
return std::make_tuple(-1,-1,-1); // Or something to indicate no match found
}
这将为在数组中找到匹配项的索引返回(x,y,z)
的tuple
。
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在动态数组上使用搜索函数
- 在 C++ 中搜索动态分配的数组中的出现次数
- 当您在此单词中搜索单词时调整字符数组的大小?
- 在对象数组中搜索字符串并返回相应值的函数
- C++线性搜索算法,确定数组中元素的数量
- 递归二进制搜索与字符串数组
- 运行搜索数组
- 比较/搜索数组中多个整数的最佳解决方案
- C 搜索数组以获取一个数字,但如果找不到该数组,则不会触发其他语句
- 更高效的是,每次都读取文件或搜索数组中的数据
- 二叉搜索数组溢出 C++
- 在c++中搜索数组中的素数
- 性能问题C++ - 搜索数组
- C++搜索数组中最大的数字
- 我需要帮助创建一个可以搜索数组的函数
- 我想要一个递归函数来检查使用二进制搜索数组的顺序
- c#到c++的转换:搜索数组
- 搜索数组以查找单词的单独实例
- 搜索数组并比较它们