Hello! I have a programming assignment due later in the week and my professor is very cryptic with his responses so asking him for help is detrimental to my learning experience. He assigned for my class to find 100 prime numbers, which i have done below.
I now need to now modify the program to use two vectors:
--Use the first vector to hold the non-prime numbers identified by the program. These are numbers that do not qualify as a prime number because some prime number is found as a divisor. (These numbers are not found in the original program)
-- The second vector is to hold the prime number that disqualified the corresponding number in the first vector. Each time a number is rejected as a prime number, it will be stored in the first vector. The number is rejected because it failed a division test. The divisor that caused the rejection is stored in the second vector.
The vectors are declared without a size and without initialization. The code should output like so
Non-prime:Disqualifier. So for example. 9:3 15:3 21:3 25:5
I need to access the elements of the vectors using
iterators, not subscript operators.
He said the program uses subscripts to access elements in the primes array. There are 3 of them and i need to change them to use dereferencing syntax.
Lastly i need to
count how many times the prime number 5 disqualified a number as a prime.
Here is a link to two pictures of what the program should look like at run time.
http://postimg.org/image/643qd1fa3/
http://postimg.org/image/betdcag5v/
Thanks for the help! - if the instructions are unclear i can just post a link to a picture of the assignment for clarity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
const int MAX(100); // Number of primes to be identified
long primes[MAX] = {2,3,5}; // Initialize with first three primes
long trial(5); // Hold a candidate prime
int count(3); // Count primes found - reflects initial values
bool found(false); // Indicates when a prime is found
do
{
trial += 2; // Produce next candidate value
found = false; // Reset indicator - assume it is not prime
for(int i = 0; i < count; i++) // Try division by existing primes
{
found = (trial % primes[i]) == 0; // True if no remainder
if(found) // No remainder means number is not a prime
break; // Terminate the division loop
}
// The above loop will exit either due to a break or after trying all
// existing primes as a divisor. found was initialized to false. If
// found is false after exiting the loop, a divisor was not found.
if (!found) // We have a new prime: not false = true
primes[count++] = trial; // Save candidate in next array position
}while(count < MAX);
// Main loop has completed - we have found MAX prime numbers.
// Display the prime numbers, presenting five numbers on one line.
cout << "Prime numbers found during the program execution: " << endl;
for(int i = 0; i < MAX; i++)
{
if (i % 5 == 0) // For a new line on first line of output
cout << endl; // and on every fifth line that follows
cout << setw(10) << primes[i]; // Provide space between numbers
}
cout << endl; // All primes displayed - for a new line
system("Pause");
return 0;
}
|