How do I pass array elements to a function?

Hello, I have created the following code for an assignment. The instructions are commented at the beginning. It does work as "required", but I need it to work in such a way as to get the bonus points. Basically, the function needs access to the array elements that have not been simply filled with a 2. For instance, if I were to be testing a candidate of 31, it needs to run the function with divisors from the array up thru 29, and stop. I don't know how to send it the array contents. It tells me that the array has not been defined. Can anyone assist?
Basically, I have tried multiple ways to pass the array values, but each time I do, the function can't seem to read it. I have tried to search for an answer and have been unsuccessful so far. I'm not asking someone to "fix it" and send it to me. I am simply asking for a little help understanding how this works. I have only been doing this for a few weeks and have no friends to call on. Thanks in advance for any advice.


//////1. Create an array of 50 integer values.
//////2. Initialize the first element of the array to a value of 2 (the first prime number).
//////3. Generate the next 49 prime numbers, and store them in consecutive values of the array. Divide each candidate number by the whole numbers from 2 to 1/2 the candidate, looking for any that divide evenly. If none divide evenly, the candidate number is prime.
//////4. Display the contents of the array.
//////5. 10pt BONUS - Use the previously-found prime numbers as divisors to evaluate the next candidate number.


#include "stdafx.h"
#include <iostream>
#include <string>


using namespace std;

string divide ( int a ) { //// 'a' is the candidate
int test = a;
int divisor = 2;
int half = test / 2;
string s = " ";

while ( test % divisor != 0 && divisor <= half ) //This will run as long as the divisor from 2 thru half the candidate has been checked.
{
divisor = divisor + 1;

}
if ( test % divisor == 0 ) {
s = "no";
return (s);
}
else if (test % divisor != 0 ) {
s = "yes";
return (s);
}


}








int main()
{

int primes[50] = {2}; //Array of 50 values to hold the first 50 prime numbers.
int cand = 3; //Candidate # to be tested
string answer;

for (int j = 1; j < 50; j++){ //Filling the entire array with 2's.
primes[j] = 2;
}

while ( primes[49] == 2 ) { //This will run until the final element of the array has been changed from a 2 to the actual 50th prime number.


for ( int i=1; i<50; i++ ) {
answer = divide ( cand ); // Calling function called "divide"
if ( answer == "yes" ) {
primes[i] = cand;
cand = cand + 1;
}
else if ( answer == "no" ) {
cand = cand + 1;
i = i-1;
}

}
}







cout << "The first 50 Prime numbers are: " << endl;
for ( int i=0; i<=8; i++ )
cout << i + 1 << ". " << primes[i] << endl;
for ( int i=9; i<50; i++ )
cout << i + 1 << ". " << primes[i] << endl;

system ("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.