如何为数组元素执行所需的搜索过程

How to perform the needed search process for array elements?

本文关键字:搜索 过程 执行 数组元素      更新时间:2023-10-16

请帮我修改或改进程序,因为它有一个逻辑错误。

编写程序,查找数组num[20]={23,45,1,23,5,78,6,13,1,4,78,18,3,5,26,4,5,10,3,45}中某项的序号。如果搜索项在数组中,则计算该数字在数组中出现的次数,并确定它们各自的数组位置。如果搜索项不在数组中,则算法必须确定给定数组中最大和最小的元素。

这是我需要修改的代码,请帮助我…

#include<iostream>
using namespace std;
int main()
 {
   int num[20]={23,45,1,23,5,78,6,13,1,4,78,18,3,5,26,4,5,10,3,45};
   int search,c,n,big,small;
   float m,oc;
   //search the item in the array
   cout<<"Enter the number to searchn";
   cin>>search;
   while(search>0)
    {
     m=search%10;
     if(m==c)
     oc++;
     search=search/10;
    }//show the occurrence
   cout<<"Digit occurred "<<oc<<" times";
   //show respective array locations
   for(c=0; c<20; c++)
    {
     if(num[c]==search)
      {
       cout<<search<<" is present at location"<<c+1;
       break;
      }
    }
   //show the largest and the smallest element  
   if(c==num[20])
    {
     big=num[0];
     for(c=1; c<num[20]; c++)
      {
       if(big<num[c])
        {
         big=num[c];
        }
      }
    }
    cout<<"Largest element : "<<big;
    small=num[0];
    for(c=1; c<num[20]; c++)
     {
      if(small>num[c])
      small=num[c];
     }
    cout<<"Smallest element : "<<small;
  }
  return 0;
 }

这应该是输出

Enter the number to search : 23
Digit occurred : 1
23 is present at location 1
if the searched Item is not in the array
Largest element : 78
Smallest element : 3

但是我的程序的输出是错误的,请帮助我的家伙。

您建议的代码有许多逻辑问题,我将首先指定,然后是实现这些注释的代码,最后,我使用此更正代码获得的结果:

1) oc不需要是浮点数,也不需要m,相反,将oc定义为int型,并使用它来存储出现的次数。

2)我强烈建议您对输入进行消毒,以确保在应用程序继续执行之前,只接受int类型的输入作为有效输入。下面的代码改编自如何使cin只接受数字,只是取消了break语句并使用了布尔变量。这是一个重要的编程最佳实践,因为假设用户或调用应用程序将提供有效的输入是不安全的。

3)虽然并不总是必要的,但用初始值初始化所有变量是一个好习惯。它;但是,必须在定义值之前为使用的变量定义初始值(否则可能发生其他应用程序错误)。我已经修复了这个问题,以及它导致0代码警告。

4)第一个while循环是完全没有必要的,而且对于所请求的内容来说是不正确的,并且可能已经打算完成。

5)第一个for循环中的逻辑,减去不正确的break语句(特别是因为对出现的次数感兴趣,而不仅仅是第一个),然后是while循环,可以使用和组合,一旦删除break语句,计算出现的次数,并报告要搜索的数字已找到的位置。

6)我留下了位置参考几乎完全一样,你有它;但是,请注意,您在问题中提到的位置1实际上是num数组的第0个元素。如果代码不向位置添加1可能会更好,除非这正是需要的。我认为这是需要的,所以我保留了那部分原封不动。

7)根据上述要求,在确定大小数字之前,搜索号的出现次数应为0,并显示出来,否则在程序输出中不需要提供它们的输出。

8)最后,确定大小数字的逻辑使用num[20]。注意,num[20]在数组中不存在,因此,num[20]的值是未定义的,可能会导致访问冲突或意外的应用程序后果,尽管我在本例中没有发现这两种情况。对于存储在num数组中的值的个数,应该使用计数20。

这是我使用的代码,根据上面的更正:

#include <iostream>
#include <limits>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char ** argv)
{
    int num[20]={23,45,1,23,5,78,6,13,1,4,78,18,3,5,26,4,5,10,3,45};
    int search = 0, c = 0, n = 0, big = 0, small = 0;
    int oc = 0;
    bool isint = false;
    string line = "";
    //search the item in the array
    cout << "Enter the number to search: ";
    while (!isint)
    {
        getline(std::cin, line);
        stringstream ss(line);
        if (ss >> search)
        {
            if (ss.eof())
            {   // Success
                isint = true;
            }
        }
        if(!isint)
        {
            cout << "Please enter a valid number to search: ";
        }
    }
    //show respective array locations
    for(c = 0; c < 20; c++)
    {
        if(num[c] == search)
        {
            cout << search << " is present at location " << c + 1 << endl;
            oc++;
        }
    }
    cout << "Digit occurred " << oc << " times" << endl;
    //show the largest and the smallest element  
    if(oc == 0)
    {
        big=num[0];
        for(c = 1; c < 20; c++)
        {
            if(big < num[c])
            {
                big = num[c];
            }
        }
        cout << "Largest element : " << big << endl;
        small = num[0];
        for(c = 1; c < 20; c++)
        {
            if(small > num[c])
                small = num[c];
        }
        cout << "Smallest element : " << small << endl;
    }
    return 0;
}

数字23的输出:

Enter the number to search: 23
23 is present at location 1
23 is present at location 4
Digit occurred 2 time(s)

数字25的输出:

Enter the number to search: 25
Digit occurred 0 time(s)
Largest element : 78    
Smallest element : 1