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?
//////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.
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;
What is the question, and how is this a hard assignment? We don't do homework assignments for you, we answer questions to help people make some cool programs.
Well, excuse me...I apologize if I'm not as smart as you. I believe the question is quite clear. Can anyone assist me in figuring out how to pass array elements to a function? As far as helping someone with an assignment, how is that a problem? I've seen many posts where people are asking help with assignments. I am learning...hence the reason for a question. I have searched these forums and the web, and still am unable to understand how to do this. Once again, I am very sorry that I'm not a genius. I've only been doing this for a couple weeks. If you can't help me, just leave it at that.
This has nothing to do with how smart you or I am, it has to do with you learning how to code the program without us giving you the code, if you got help before then way-to-go, but you have to learn how to do it. Based on this I will show you how to pass an array to another function, but I wont incorporate it into your code for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>// Used for the output
usingnamespace std;// We want to specify the standard namespace
// Outputs the nth element in an array
int Output(int* Y, int N) {// Look up
/*
You define the variable tha array is being sent to
as a pointer, then you use it just like the original
array.
*/
cout << Y[N-1] << endl;// Output that element
}// End of Output(int*,int)
// The start of the sample program
int main() {// Look up
int X[] = {2,4,6};// Create an arraywith 3 values in it
Output(X,3);// Output the first element in the array
}// End of main(void)