【问题标题】:Inserting "99" after every prime number in an array (with two functions)在数组中的每个素数后插入“99”(有两个功能)
【发布时间】:2014-01-09 20:43:52
【问题描述】:

在构造函数并调用它时,我的参数有些问题(这是我第一次尝试使用两个函数)。

这是调试器的错误:

I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp||In function 'bool isPrime(int*)':|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|11|error: 'i' was not declared in   this scope|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|16|error: 'i' was not declared in   this scope|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp||In function 'int main()':|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|34|error: too few arguments to  function 'bool isPrime(int*)'|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|9|note: declared here|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

这里是代码:

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;

bool isPrime(int t[])
{
    for(int j=2; j<=sqrt(t[i]); j++)
    {
        if(t[i]%j==0)
            return false;
    }

    if(t[i]<2)
        return false;

    return true;
}

int main()
{
    int t[50], n, i;
    cout<<"Size of array: ";
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        cout<<i<<". Element: ";
        cin>>t[i];
    }
    i=1;
    while(i<=n)
    {
        if(isPrime())
        {
            for(int j=n; j>=i+1; j--)
            {
                t[j+1]=t[j];
            }
            t[i+1]=99;
            n++;
            i=i+2;
        }
        else
        {
            i++;
        }
    }
    for(int i=1; i<=n; i++)
    {
        cout<<t[i]<<", ";
    }
    cout<<"\n";
    system("pause");
    return 0;
}

我知道 system("pause") 很慢,不推荐使用等等。不要怪我使用它(建议使用其他方法)。

【问题讨论】:

  • 如果这是 c++,您可能希望包含 。但这并不能解决您的问题。
  • 这段代码有很多问题。您需要做的第一件事是阅读错误消息。从顶部开始并修复它(i 甚至不应该在那里,你可能不应该传递数组)。下一个告诉你调用isPrime时需要提供一个参数,否则它不知道你在测试什么。如果你对这一切感到困惑,不妨退后一步,从一本关于 C 的好书中阅读几章。
  • 不,这是我想要包含的内容(cmath 与 math.h 的作用相同,我使用 cout 和 cin 操作数,使用 cstdlib 的编译器无法识别)。包含没有什么不好的。也许我是一个初学者,但不是这么多
  • cout 和 cin 在 iostream 标头中...不确定 cstdlib 会如何影响
  • 对不起,我后来意识到这是另一个标题。但还是没有解决。

标签: c++ primes function-call


【解决方案1】:

这是代码审查。 :)

  1. isPrime 应该采用 int t 参数,而不是数组。您还可以在此函数中使用 t[i],但 i 超出范围!你在main 中声明了i。将t[]t[i] 都替换为t。或者,更好的是,将其命名为 number,这样就非常清楚了。
  2. 您在main 的第一行声明int i,然后在它正下方的for 循环中再次声明t 数组。我很惊讶这并没有导致编译错误。
  3. 在以for(int i = 1;... 开头的for 循环中,您每次都会错过数组的第一个元素! 记住:数组是从 0 开始索引的,所以应该是 for(int i = 0;...
  4. 不对用户输入进行错误检查(main 中的声明 cin &gt;&gt; n)。如果我输入 100 会怎样? main 中的数组 t 包含 50 个整数,因此 t[100] 会导致程序因段错误而崩溃。
  5. main 的while 循环中,if(isPrime()) 行将无法编译,因为您定义了isPrime 以获取参数,但您没有使用任何参数调用它。我想你的意思是if(isPrime(i))
  6. 在下面的for循环中,初始化int j = n,然后尝试访问未分配的t[j+1]。这也会导致您的程序崩溃。
  7. 为什么在while循环中有n++?我可以看到这会导致问题/意外行为,因为这是您的 while 循环的界限。
  8. 在编写 C++ 代码时,最好使用 C++ 样式的标头。所以用#include &lt;cstdlib&gt;代替#include &lt;stdlib.h&gt;,用#include &lt;cmath&gt;代替#include &lt;math.h&gt;

可能还有一些错误/问题,但这应该会让你感动。让我们知道您的想法!

【讨论】:

  • 谢谢!这就是我一直在等待的答案。
【解决方案2】:

调试器的错误告诉你到底哪里出了问题!

  1. i 在方法 isPrime() 中不存在。仅仅因为你在 main() 中有一个 i,你就不能在另一个函数中使用它(阅读 Scope
  2. 您在没有参数的情况下调用 isPrime() 函数。

我建议你从更简单的程序开始,同时阅读functions

【讨论】:

  • 你写的都是我的错。我知道范围,但是我复制的代码是我解决问题的尝试之一
【解决方案3】:

你应该使用类似的东西

cin.ignore();

而不是system("PAUSE");

【讨论】:

  • 您应该将此作为对他的问题的评论。
猜你喜欢
  • 1970-01-01
  • 2020-08-23
  • 2018-11-19
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
相关资源
最近更新 更多