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));
}