-2

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

编写一个程序,将在数组 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 search\n";
   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

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

4

1 回答 1

0

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

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

2)我强烈建议您清理您的输入,以确保在应用程序执行恢复之前只接受一个 int 作为有效输入。下面的代码改编自How to make cin take only numbers,只是去掉了break语句并使用了布尔变量。这是一个重要的编程最佳实践,因为假设用户或调用应用程序将提供有效输入是不安全的。

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

4) 第一个 while 循环是完全没有必要的,除了对所请求的内容不正确之外,并且可能打算完成。

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

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

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

8)最后,确定大小的逻辑使用num[20]。请注意,数组中不存在 num[20],因此,num[20] 的值是未定义的,可能会导致访问冲突或意外的应用程序后果,尽管在这种情况下我没有找到。相反,应该使用 20 来表示存储在 num 数组中的值的数量。

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

#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
于 2014-07-06T06:49:09.883 回答