我在想如何完成這個程序。在用戶輸入的項目列表「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