简体   繁体   English

数组上的C ++线性搜索算法

[英]C++ Linear search algorithm on arrays

I've started programming using C++ few weeks back. 几周前,我开始使用C ++进行编程。 I'm working on an application store user input data into an array list. 我正在开发一个将用户输入数据存储到数组列表中的应用程序。 When entering user data the application must be able to check whether the user already exists in the array list. 输入用户数据时,应用程序必须能够检查用户是否已存在于阵列列表中。 The program is unable to store the user input or able to check whether the user already exists in the array list.. 该程序无法存储用户输入或无法检查用户是否已存在于阵列列表中。

int linearSearch(int array[], int size, int searchValue)
{
    for (int i = 0; i < size; i++)
    {
        if (searchValue == array[i])
        {
            return i;
            break;
        }
    }

    return -1;
}



void customerReg(){

    const int Capacity = 99;
    int cnic[Capacity];
    int customerNic;                     
    int search = 0;

    cout << "Enter Customer NIC: \n";
    cin >> cnic[Capacity];


    search = linearSearch(cnic, Capacity, customerNic);

    if (search != -1){
        cout << "Customer is already registered!\n";
    }

    else {
        string customerName;
        cout << "Enter Customer Name: \n";
        cin >> customerName;

    }

What about: 关于什么:

...
cout << "Enter Customer NIC: \n";
cin >> customerNic;    // <=== instead of: cnic[Capacity];

Other remarks: 其他说明:

  • the break is not necessary: the return will already interupt the search loop 无需中断:返回将已经中断搜索循环
  • cnic[Capacity] is out of range, so puting a value in it might cause some troubles cnic [Capacity]超出范围,因此在其中输入值可能会引起一些麻烦
  • cnic[] is not initialised cnic[]未初始化
  • It is not clear how you fill cnic[] , which is by the way local to the function and be lost as soon as you return from it. 目前尚不清楚如何填充cnic[] ,这是该函数的局部方式,一旦您从函数返回就将丢失。
  • Depending how you initalize/fill cnic , it could make sense to keep track of the number of customers that are registered in the table. 根据您启动/填写cnic ,跟踪表中注册的客户数量可能很有意义。

Edit: 编辑:

I assume that you can't use vectors or maps for your exercise, and that you're right at the beginning of your learning. 我假设您不能使用矢量或地图进行锻炼,并且您在学习之初就是对的。

So I suppose that customerReg() is the first function that you are working on, and that others will follow (display, delete, modifiy...). 因此,我假设customerReg()是您正在使用的第一个函数,并且其他函数将跟随(显示,删除,修改...)。 If this is the case, you have to keep your customer data outside the functions: 在这种情况下,您必须将客户数据保留在功能之外:

const int Capacity = 99;
int cnic[Capacity] {}; 
int customer_count=0;    // counter to the last customer inserted

Then in customerReg() you should call your search function using the number of customers instead of the maximal Capacity : 然后,在customerReg() ,应使用客户数而不是最大Capacity来调用搜索功能:

search = linearSearch(cnic, customer_count, customerNic);

Later, in the else branch you have to insert the new id into the array: 稍后,在else分支中,您必须将新的id插入到数组中:

else {
    if (customer_count==Capacity) {
       cout << "Oops ! Reached max capacity"<<endl; 
    else { 
       string customerName;
       cout << "Enter Customer Name: \n";
       cin >> customerName;
       ... 
       cnic[customer_count] = customerNic; // here you store the id
       ... // store (I don't know where) the other customer elements you've asked for 
       customer_count++;  // increment the number of users that are stored. 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM