在数组中搜索值

Search value within array

本文关键字:搜索 数组      更新时间:2023-10-16

在我的代码中,我试图搜索整个数组以找到一个值。我正在添加细节。很多细节。我是个初学者。

这是我在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函数有两个大问题。

  1. 您可以定义xyz两次。循环中充当索引的变量会覆盖您在函数顶部声明的变量。

  2. 出于某种原因,您在循环的最深部分之外搜索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