C ++不需要返回值

臭鼬

我正在尝试一些练习,以学习如何将指针与数组和函数一起使用。因此,我尝试编写一种“奇怪的方式”以找出一定范围内的质数。

问题是输出总是将函数的返回值与素数算法相加。如果我省略它,则显示为“ 32767”,如果我编写return *pt,则它会添加范围的最后一个数字,即使它不是素数也是如此!

刚尝试使用数字6:它不是素数,但会弹出!

#include <iostream>
int show_primes(const int * begin, const int * end);
int main()
{
    using namespace std;
    int i = 0;
    int End_Array = 0;
    cout << "Write the last number in your range (it always start from number 2)";
    cin >> End_Array;
    i=End_Array;
    int cookies[i];
    for(i=-1; i<End_Array; i++)
       cookies[i] = i+1;
    cout << show_primes(cookies, cookies + End_Array-1);
}
int show_primes (const int * begin, const int * end)
{
    using namespace std;
    const int * pt;
    int z = 0;
    for (pt = begin; pt < end; pt++, z=0)
    {
        for (int n=2; n<=*pt; n++)
        if ( *pt%n == 0 )
            ++z;
        if (z==1)
            cout << *pt <<endl;
    }
    return *pt ;
}
P0W
 for(i=0; i<End_Array; i++) // Start from zero
    cookies[i] = i; //Use i

 // Don't use cout
 show_primes(cookies, cookies + End_Array-1); 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章