-1

I'll want to make a the Sieve of Eratosthenes, which I've made in the prime function, but the problem is that I'll want to use a vector, or something like it to store the value to later be use on the code, the rest is a binary search, but I don't know how to use the reference of the vector, I've done it on java, but the reference there is easy.

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int *primes (int size)
{
    int* primArray = new int(size);
    int notPrime[size];
    for (int i = 0; i < size; i++)
        notPrime[i]=0;
    int cont = 0;
    for(int i = 2; i < size; i++)
    {
        if(notPrime[i] == 0)
        {
            primArray[cont] = i;
            cont++;
            int sum = i;
            for(int j = 1; sum < size; j++)
            {
                notPrime[sum] = 1;
                sum = j*i;
            }
        }
    }
    return primArray;
}

the problem up here

int search(int input, vector<int> array)
{
    int min = 0;
    int max = array.size() - 1;
    int guess = (min + max)/2;
    int output = -1;
    while (true)
    {
        if (array[guess] == input)
        {
            output = guess;
            break;
        }
        if (min > max)
        {
            output = -1;
            break;
        }
        if (array[guess] < input)
            min = guess + 1;
        else
            max = guess - 1;
        guess = (min + max)/2;
    }
    return output;
}

int main()
{
    scanf("%d", &input);
    //int* array = primes(200);
    /*for (int i = 0; i < 200; i++)
    {
        printf("%d, ", array[i]);
    }*/
    int numbers[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199};
    vector<int> primeArray (numbers, numbers + sizeof(numbers) / sizeof(*numbers));
    for (int i = 0; i < primeArray.size(); i++)
    {
        printf("%d, ", primeArray[i]);
    }
    cout<< "\n";
    printf("%d", search(input, primeArray));
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
user3763927
  • 31
  • 1
  • 8

2 Answers2

0
  • The line int* primArray = new int(size); is bad because it allocates only 1 element initialized to size and it doesn't mean allocating size elements.
    To allocate size elements, use int* primArray = new int[size];
  • References can be declared using &
    like this: int search(int input, vector<int>& array)
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

First of all, a bit of advice:

do not mix C++ and C style code. if you want to use printf() and scanf() use C. if you want std::vector and std::cout then use C++. if you use C feel free to use arrays but in C++ you are almost never encouraged to be using arrays when you have access to std::vector

Mixing these programming styles is only going to complicate things for you down the line.

now as for your code:

You are not experiencing problems due to a lack of passing by reference.

Every time you do search(input, primeArray) you are passing primeArray by value. that means the copy constructor of std::vector<int> is called and a fresh copy of your vector is created before being passed to the search function. However this is not an issue because you are not modifying primearray inside your search function

If you want to pass in by reference you can change the signature of search to take a reference like this: int search(int input, vector<int>& array)

Actual problems:

In your primes function you have int* primArray = new int(size); and I suspect this line is not doing what you want.

new int (size); will allocate 1 int on the heap and initialize that value to whatever the value of size is.

If you want to allocate a block of memory for size integers you need int * primArray = new int[size];

More issues:

In the first line of your main function you have scanf("%d", &input); but input has not been initialized anywhere.

I know you commented out the section in your main where you call int* primes() but you have to remember that if you allocate dynamic memory anywhere, you MUST release that memory later. In your case because your program ends at the end of main, your dynamic memory will be released implicitly, but it is always good practice to release the memory yourself.

Failure to due so will introduce memory leaks in your program.

ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
  • Thanks a lot, I'll take your advice on not mixing languages, and well, I'd have said that the program work, but I've wanted to make better using vectors and not the array, I have made the program print the prime numbers and them copy them into the array manually and well that's not correct, but now I'll use the vector thanks – user3763927 Dec 21 '15 at 02:22