我在想如何完成这个程序。在用户输入的项目列表“ll”(长度为31)上执行线性搜索,如果找到,则返回用户输入的号码及其位置。C++调用 - 搜索功能
问题:我不确定如何调用这个特定场景中的函数,我并不需要使用指针或传递值,所以缺乏这些让我更加困惑,因为那些是相当常见的情况。
#include <iostream> //enables usage of cin and cout
#include <cstring>
using namespace std;
int search (int i, int it, int ll, int z);
int printit (int i, int it, int ll, int z);
int main()
{
int i,it,z;
int ll[] = {2,3,4,5,6,2,3,44,5,3,5,3,4,7,8,99,6,5,7,56,5,66,44,34,23,11,32,54,664,432,111}; //array hardwired with numbers
//call to search
return 0;
}
int search (int i, int it, int ll, int z)
{
cout << "Enter the item you want to find: "; //user query
cin >> it; //"scan"
for(i=0;i<31;i++) //search
{
if(it==ll[i])
{
//call to printit
}
}
return 0;
}
int printit (int i, int it, int ll, int z)
{
cout << "Item is found at location " << i+1 << endl;
return 0;
}
除非您以某种方式告诉它,否则'search'应该知道'll'中的内容?另外,'ll'是一个可怕的变量名 - 避免使用'l','O'和'I'。 – Barry 2015-02-09 02:45:30