Prime Number Problem

Hi,

I am teaching myself C++, so this is not homework.
I am reading to C++ books, and research my code on the web.
I do not know any C++ programmers, so I would appreciate your help.
I would like someone to review this code and tell me my error.

The output of this code is...
2 3 5 7

However, it should have listed the first 100 prime numbers.
I think the problem is with the outer for loop (w/ var k),
but I am not sure.


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
// Calculate a table of prime numbers using primes to test trial number.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
	const int MAX = 100;
	int primes[MAX] = {2, 3, 5};
	int prime_count = 3;
	int trial = 7;
	int isprime = 0;
	int remainder = 0;

	for (int k = 0; k < MAX; k++)
	{

		for (int i = 0; i < prime_count; i++)
		{
			remainder = trial % *(primes + i);
			//cout << "primes array = " << *(primes + i) << endl;
			//cout << "remainder = " << remainder << endl;
			if (remainder == 0)
				isprime++;
		}
	
		if (isprime == 0)
		{
			primes[prime_count] = trial;
			prime_count++;
		}

		trial += 2;

	} 

	for (int i = 0; i < prime_count; i++)
	{
		cout << setw(6) << *(primes + i);
		if (i == 0)
			continue;
		if (i%10 == 0)
			cout << endl;
	}

	cout << endl;

	return 0;
}

Last edited on
Could you please use indentation? This gives me a headache trying to follow it...
Done.
Why are you doing *(primes + i) instead of primes[k] on line 22?
Just for experience.
I mean, why are you using i instead of k?
Last edited on
The problem is with isprime
On line 22, I am using *(primes + i) to loop through the primes found so far in order to test whether trial is prime or not.
The outer loop (w/ var k) loads the next trial number to be tested.
Your usage of i instead of k is another problem, because most of the loops will be accessing zeroes (you didn't initialize the rest of the array, so by default it is all zeroed out). Look carefully at the way your loops are set up and what they intend to do.
kev82: could you elaborate a little? How is isprime the problem, and how does it prevent the next number from being added to the array?
I'm not sure how you can check one number against many prime numbers to determine if it is prime...? Could you explain how you expect this to work, as where I thought I understood before, I am now lost.
LB: The only mistake I see is that on line 17: k < MAX, should be, prime_count < MAX.
OK, but could you explain what I asked you to explain? I'm still lost.
Last edited on
LB: There are several ways to find a prime number. If a number can be divided evenly by any of the prime numbers smaller than it then the trial number is not prime. The even numbers do not need to be tested, because they can all be divided by 2.
I'm a little afraid this may be homework, so I'm not going to tell you directly. Also, if it is your own code you will be more pleased if you figure it out.

I suggest you concentrate on this thought - if the numbers are not being added to the array then that means lines 30-34 aren't executing.
kev82: Thanks for the tip. I added "isprime = 0;" to line 19. This fixed the problem. I have a hard time finding that kind of error. I appreciate the help, and to re-assure you I am not in a class, I am however, working through problems in the book. I do not know any other way to learn except to practice. I do get stuck sometimes, and books can't answer questions, or review code and tell you where the mistake is. Thanks again for your help.
LB: kev82 solved the problem for us. I am just posting to thank you for your time and effort on this problem.
Topic archived. No new replies allowed.