[HELP] Programming problem

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


(1) Declare two vectors.

(2) Using your current do-while loop, when found evaluates as true inside your for-loop, use the vector function push_back to add both the actual number you're testing to your first vector and the prime it is divisible by in the second.

(3) After all of that, you'd iterate through your diqualifier vector, counting every time five appears. If he wants you to use iterators, use the begin() function I would assume.
http://www.cplusplus.com/reference/vector/vector/begin/

That should get you started at the very least.
Thanks for the response, unfortunately i've been busy and haven't been able to work on my program until now.

I have declared two vectors of type long. one called nonPrimes to hold nonprime numbers and one called disqualifiers to hold prime numbers that disqualify nonPrimes as primes. so your suggested step 1 is done.

My professor only mentioned the push_back vector function in a powerpoint but never went over what it does or how to use it. The book we are using only mentions it at the end of the chapter with no examples, so i tried youtube to see a visual representation of how its used and I don't think entering a static number into the push_back(#); is going to get me very far since my vectors need to be uninitialized with no size.

I reused the code for the prime numbers to display the non-primes below with the set precisions and i tested out the push_back function to see what i could get to return back.

you said to put it in the do-while loop, which i tried and couldn't figure out.

I haven't even attempted to try using iterators yet, which will probably go just as terrible.

I'm still learning so hopefully someone here is willing to explain and bare with me as i work this problem out.

Thanks





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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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

	vector <long> nonPrimes; //Vector to hold non prime numbers
	vector <long> disqualifiers; //Vector to hold prime numbers that disqualify the non prime numbers as prime numbers. 

	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

	
	/*
		Display Non-primes and their disqualifiers - just a test to try out push_back
	*/
	cout << "Non-Primes identified: " << count << endl;
	for (int i = 0; i < MAX; i++)
	{
		
		nonPrimes.push_back(i); //Push back values to the vector
		disqualifiers.push_back(primes[i]); // Push back values to disqaulifier vector
		if (i % 5 == 0)   
			cout << endl;
		cout << setw(10) << nonPrimes[i] << ":" << disqualifiers[i];
	cout << endl;


	system("Pause");

	return 0;
}
I don't think entering a static number into the push_back(#); is going to get me very far since my vectors need to be uninitialized with no size.

Um, that's how vectors work. They manage their own memory. When you add a new member using push_back(), the vector decides for itself whether it has enough memory for it, and if it doesn't, it allocates new memory.

If you're going to be using vectors, you probably ought to actually read up on them. The reference material on this site is as good a place to start as any.
I have one last thing to do. Thanks for all the help to everyone who responded. I learned a lot!

The last thing i need to do is count how many times 5 appears in the Disqualify Vector I setup a for loop but all it does is produce every value inside the disqualify vector. All i need is to count how many times 5 appears. Any insight into this is appreciated.

Thank you!

Here is my complete code
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
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

	vector <long> nonPrimes; //Vector to hold non prime numbers
	vector <long> disqualify; //Vector to hold prime numbers that disqualify the non prime numbers as prime numbers. 
	vector<long>::iterator fives;//Vector iterator which will be used to find how many times the number 5 disqualifies a nonPrime number.

	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
				{
				nonPrimes.push_back(trial); // push_back trial values to vector nonPrimes
				disqualify.push_back(primes[i]); //push back disqualifying prime numbers to disqualify vector
				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: found = true! -- add numbers to vectors
			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

	
	/*
		Display Non-primes and their disqualifiers
	*/
	cout << "Non-Primes identified: " << count << endl; // Identify how many nonprimes appear and display the value.
	for (int i = 0; i < MAX; i++) //For loop to clarify position of output
	{
		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) << nonPrimes[i] << ":" << disqualify[i] << setw(10);   // outputs nonPrime numbers and their disqualifying primes
	}
	cout << endl;

	//Use iterator (fives) to produce how many times the digit 5 appears as a disqualifying prime number
	for (fives > disqualify.begin(); fives < disqualify.end(); fives++) // bounds checking for how many times 5 appears.
	{
			cout << "Numer of times 5 was a disqualifier: " << *fives << endl; // output number of times 5 appears as a disqualifying prime
	}
	system("Pause");

	return 0;
}
Simply iterate over the vector, and check the value of each element. For each one that is equal to 5, increment a counter.

Unless there's some new-fangled C++11 way of doing it...
Topic archived. No new replies allowed.