C++ Determine Prime Numbers in Array

email me

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>    // std::min_element, std::max_element

using namespace std;

int primeReturn(int arr[], int n)
{
int max_val = *max_element(arr, arr + n);

vector<bool> prime(max_val + 1, true);

prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= max_val; p++) {

if (prime[p] == true) {
for (int i = p * 2; i <= max_val; i += p)
prime[i] = false;
}
}

// Find primes
int primes;
string x;

for (int i = 0; i < n; i++)
if (prime[arr[i]])
x = x + std::to_string(arr[i]);
primes = std::stoi(x);
return primes;
}

int main(void)
{
// the array
int arr[] = { 9,8,5,3,2,4 };

// determine size of array
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Find primes in: 985324";

// output primes
cout << "\nPrimes: " << primeReturn(arr, n);

//system("pause"); // for visual studio
getchar();
}


Output