[英]I need help creating a function that can search an array
Here is the question:这是问题:
I have the table made I just don't know how to make a function that can search it.我制作了表格,我只是不知道如何制作可以搜索它的函数。
#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;
}
The idea behind generating the array is that all multiplication possibilities are stored in a table for easy lookup.生成数组背后的想法是所有乘法的可能性都存储在一个表中以便于查找。 There is no need to search all you need to do is lookup the value at the index like so:
无需搜索所有您需要做的就是在索引处查找值,如下所示:
int result = product[9][2];
The order of indicies doesn't matter either since 2*9 is the same as 9*2.索引的顺序也无关紧要,因为 2*9 与 9*2 相同。
I would suggest you to read functions from here or any other good source you find.我建议您阅读此处的函数或您找到的任何其他好的来源。
Make a function with any name as follow and pass the two values and the 2D Array and use this simple way to extract the value创建一个任意名称的函数如下,并传递两个值和二维数组,并使用这种简单的方法来提取值
int productvalue(int a,int b, int product[][10])
{
return product[a][b];
}
Complete Code:完整代码:
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.