简体   繁体   English

c++ 程序返回“0 0”而不是返回素数。 为什么它没有从“素数”function 中得到正确的 output?

[英]c++ program return “0 0” instead of returning prime numbers. Why does it not get the correct output from the “prime” function?

My function "prime" takes a dynamic array and returns a dynamically allotted array consisting of only prime numbers.我的 function “素数”采用动态数组并返回一个仅由素数组成的动态分配数组。 But when it is returned to main, it gives the wrong output and print但是当它返回到main时,它给出了错误的output并打印

Output: Output:

0 0

Expected Output:预期 Output:

2 3

My Code:我的代码:

#include<stdio.h>
#include<math.h>
#include <cstdio>
#include<iostream>
using namespace std;
const int capacity = 10000;
int * prime(int numbers[capacity])
{
    int *primeArray;
    primeArray = new int[capacity];
    int lenghtOfArray=sizeof(primeArray)/sizeof(int); 
    int counter = 0;
    for(int index=0;index<lenghtOfArray;index++){
    // 0 and 1 are not prime numbers
    if (numbers[index] == 0 || numbers[index] == 1) {
        continue;
    }
    else {
        for (int i = 2; i <= numbers[index] / 2; ++i) {
            if (numbers[index] % i == 0) {
                primeArray[counter] = numbers[index];
                counter = counter + 1;
                break;
            }
        }
    }
    }
    return primeArray;
}


main()
{
    int *mynumbers;
    mynumbers = new int[capacity];
    mynumbers[0] = 1;
    mynumbers[1] = 2;
    mynumbers[2] = 3;
    mynumbers[3] = 4;
    mynumbers[4] = 5;
    mynumbers[5] = 6;
    int* myprime = prime(mynumbers);
    cout << myprime[0] << " " << myprime[1];
}

What went wrong?什么地方出了错?

    int lenghtOfArray=sizeof(primeArray)/sizeof(int); 

is an wrong way to get the length of dynamically allocated array.获取动态分配数组的长度是错误的方法。

sizeof(primeArray) is not the allocated size but the size of pointer. sizeof(primeArray)不是分配的大小,而是指针的大小。

You already have the number of elements of the array, so use that to get the length of the array:您已经有了数组元素的数量,因此使用它来获取数组的长度:

    int lenghtOfArray=capacity;

But this doesn't make your code correct because now your code will use indeterminate values of uninitialized elements of int array.但这不会使您的代码正确,因为现在您的代码将使用int数组的未初始化元素的不确定值。 What you have to do is to pass the number of valid elements to the function.您要做的就是将有效元素的数量传递给 function。

Points to change:改变点:

// add argument to pass the number of data
// int * prime(int numbers[capacity])
int * prime(int numbers[capacity], int num_numbers)
{
    int *primeArray;
    primeArray = new int[capacity];
    // use the passed number
    // int lenghtOfArray=sizeof(primeArray)/sizeof(int); 
    int lenghtOfArray=num_numbers; 
    // pass the number of data
    // int* myprime = prime(mynumbers);
    int* myprime = prime(mynumbers, 6);
    cout << myprime[0] << " " << myprime[1];
}

Another point is that your code is storing numbers that are not primes instead of prime numbers.另一点是您的代码正在存储不是素数而不是素数的数字。

Instead of this而不是这个

        for (int i = 2; i <= numbers[index] / 2; ++i) {
            if (numbers[index] % i == 0) {
                primeArray[counter] = numbers[index];
                counter = counter + 1;
                break;
            }
        }

you should use this你应该用这个

        // add the number for now
        primeArray[counter] = numbers[index];
        counter = counter + 1;
        for (int i = 2; i <= numbers[index] / 2; ++i) {
            if (numbers[index] % i == 0) {
                // it is not actually a prime, rollback
                counter = counter - 1;
                break;
            }
        }

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

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