简体   繁体   中英

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. But when it is returned to main, it gives the wrong output and print

Output:

0 0

Expected 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.

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. What you have to do is to pass the number of valid elements to the 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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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