我的代码,我正在尝试搜索整个数组以查找值。 我正在添加细节。很多细节。我是初学者。
这是我在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++) {
}
}
}
}
也许到最后?请让我知道什么是错的!
答案 0 :(得分:1)
首先,您要在x, y, z
函数中定义两次变量searchArray
。
然后比较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";
}
}
}
}
}
答案 1 :(得分:0)
您的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
}
对于在数组中找到匹配项的索引,这将返回tuple
(x,y,z)
。