C++调用-搜索功能
C++ Calling - search function
我想知道如何完成这个程序。它是对用户输入的项目It的列表"ll"(长度为31)进行线性搜索,如果找到,则返回用户输入的数字及其位置。
问题:我不知道如何在这个特定的场景中调用函数,我真的不需要使用指针或传递值,所以缺少这些实际上让我更困惑,因为这些都是相当常见的场景。
#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
:的每个参数都有问题
i
的传递值在使用之前会被覆盖,因此应该是一个局部变量it
也是如此ll
应该是int
的数组- 根本没有使用
z
printit
的情况更糟:4个参数中的3个被忽略。
如果已经打印出结果,则搜索和打印不需要返回int。此外,一些声明的变量也是无用的。以下代码将起作用:
#include <iostream> //enables usage of cin and cout
#include <cstring>
using namespace std;
void search (int ll[]);
void printit (int n);
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
search(ll);
return 0;
}
void search (int ll[])
{
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
printit(i);
}
}
// return 0;
}
void printit (int n)
{
cout << "Item is found at location " << n+1 << endl;
// return 0;
}
相关文章:
- 防弹站树搜索功能C++
- 迭代向量以获得搜索功能
- Sinlge 链表,C++,删除所有和搜索功能的问题
- 二叉搜索树 - 使用 Linux 在虚拟机中制作删除功能
- 如何将搜索功能添加到数组中
- 搜索非二进制树C 的功能
- 搜索功能使程序崩溃
- 替罪羊搜索功能
- 二叉搜索功能始终返回 -1 值而不是索引
- 用于二分搜索的错误简单功能(C++)
- 如何改进我的矢量搜索功能
- 按元素的功能搜索upper_bound
- 简单的二进制搜索树非递归添加功能
- 链接列表中的搜索功能问题
- 如何将搜索功能添加到WXWIDGET C 中的TextCtrl
- 二进制搜索功能不起作用
- 二叉搜索树在插入功能方面有问题
- 这个树搜索功能让我发疯.它返回 NULL,但不知何故主函数中的值发生了变化
- 看不懂二叉搜索功能
- C++ 二叉搜索功能不打印