Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要帮助创建一个可以搜索数组的函数_C++_Arrays - Fatal编程技术网

C++ 我需要帮助创建一个可以搜索数组的函数

C++ 我需要帮助创建一个可以搜索数组的函数,c++,arrays,C++,Arrays,问题是: 通过在数组中查找乘积,创建一个函数,返回1到9之间两个数字的乘积 示例:如果用户输入9和2,程序将在二维数组中查找答案并显示18 我制作了表格,但我不知道如何制作一个可以搜索它的函数 #include <string> #include <iostream> #include <iomanip> using namespace std; int main() { const int numRows = 10; const

问题是:

  • 通过在数组中查找乘积,创建一个函数,返回1到9之间两个数字的乘积
  • 示例:如果用户输入9和2,程序将在二维数组中查找答案并显示18
我制作了表格,但我不知道如何制作一个可以搜索它的函数

#include <string> 
#include <iostream>
#include <iomanip>  
using namespace std;

int main()
{
    const int numRows = 10;
    const int numCols = 10;

    int product[numRows][numCols] = { 0 };

    for (int row = 0; row < numRows; ++row)
        for (int col = 0; col < numCols; ++col)
            product[row][col] = row * col;

    for (int row = 1; row < numRows; ++row)
    {
        for (int col = 1; col < numCols; ++col)
            cout << product[row][col] << "\t";
        cout << '\n';
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
常数int numRows=10;
常量int numCols=10;
int乘积[numRows][numCols]={0};
对于(int行=0;行cout生成数组背后的想法是,所有乘法可能性都存储在一个表中,以便于查找。无需搜索,只需在索引处查找值,如下所示:

int result = product[9][2];

标记的顺序也不重要,因为2*9与9*2相同。

我建议您阅读来自或任何其他您找到的好来源的函数

使用如下任意名称创建一个函数,并传递两个值和2D数组,然后使用此简单方法提取值

int productvalue(int a,int b, int product[][10])
{
    return product[a][b];
}
完整代码:

#include <string> 
#include <iostream>
#include <iomanip>  
using namespace std;

int productvalue(int a,int b, int product[][10])
{
    return product[a][b];
}
int main()
{
    const int numRows = 10;
    const int numCols = 10;

    int product[numRows][numCols] = { 0 };

    for (int row = 0; row < numRows; ++row)
        for (int col = 0; col < numCols; ++col)
            product[row][col] = row * col;

    for (int row = 1; row < numRows; ++row)
    {
        for (int col = 1; col < numCols; ++col)
            cout << product[row][col] << "\t";
        cout << '\n';
    }
    int a,b;
    cin>>a>>b;
    //Example Call to the function
    int x = productvalue(a,b,product);
    cout<<x<<endl;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int productvalue(int a、int b、int product[][10])
{
退货产品[a][b];
}
int main()
{
常数int numRows=10;
常量int numCols=10;
int乘积[numRows][numCols]={0};
对于(int行=0;行b;
//函数的示例调用
int x=产品价值(a、b、产品);

选择堆栈溢出。请选择页面。选择一种语言-使用C++的C++适合的是完全不适合C,反之亦然。……并且正确的编辑,删除C,已经完成了;代码使用显式的C++ I/O,所以它不是C代码…我如何将其添加到may代码中,以便程序调用它,然后返回您列出的产品。@mikeg4523,我已添加了完整的代码,并进行了示例调用。请看一看。谢谢,但我想我想说的是,如何从cin>>中执行它需要用户input@mikeg4523我添加了用户输入