@undecked0 I tried running your latest code.
I called it like this from main(),
PrimeList(30);
and this was the output:
0 10 20 30 40 50 60 70 8
0 20 40 60 80 100 120 140 16
0 30 60 90 120 150 180 210 24
0 40 80 120 160 200 240 280 32
0 50 100 150 200 250 300 350 40
0 60 120 180 240 300 360 420 48
0 70 140 210 280 350 420 490 56
0 80 160 240 320 400 480 560 64 |
Now if I run it like this
PrimeList(31);
, then this is the output:
1 11 21 31 41 51 61 71 8
1 21 41 61 81 101 121 141 16
1 31 61 91 121 151 181 211 24
1 41 81 121 161 201 241 281 32
1 51 101 151 201 251 301 351 40
1 61 121 181 241 301 361 421 48
1 71 141 211 281 351 421 491 56
1 81 161 241 321 401 481 561 64 |
There's a pattern there, but it may not yet be clear.
So let's add a single extra line at the start,
1 2 3 4 5 6
|
void PrimeList(int listMax)
{
cout << std::boolalpha;
// etc.
// rest of code unchanged
|
Now this is the output:
true 1true 2true 3true 4true 5true 6true 7true 8
true 2true 4true 6true 8true 10true 12true 14true 16
true 3true 6true 9true 12true 15true 18true 21true 24
true 4true 8true 12true 16true 20true 24true 28true 32
true 5true 10true 15true 20true 25true 30true 35true 40
true 6true 12true 18true 24true 30true 36true 42true 48
true 7true 14true 21true 28true 35true 42true 49true 56
true 8true 16true 24true 32true 40true 48true 56true 64 |
My apologies for going through this so slowly - one step at a time and we'll start to be able to explain what is happening.
You see that word "true" or "false" which appears? That is the output from the function
isPrime(listMax)
. The result is a boolean value (either true or false). By default it is shown as the digit 1 or 0. The
boolalpha
manipulator is used, it sets a flag so that the text rather than the digit is shown.
But why is it always the same value (all true or all false)? Well, the value
listMax
doesn't change, so the result will always be the same.
If we go back to the post I made yesterday, perhaps you missed the salient points. In your code, I suggested two things:
1. call the function isPrime with the value
row * column
.
2. use an if statement to test the result.
That would lead to code something like this:
1 2
|
if (isPrime(row * column))
cout << setw(4) << row * column;
|
Now if that is dropped into your existing code, the output looks a bit like this:
Now there is something positive there. The numbers 2 3 5 and 7 are indeed prime numbers. 1 is not considered prime, a small tweak to function isPrime() can fix that.
As for your output, it has a few problems. The really important one is that the value listMax is not used at all, anywhere within the function.
I'd start by modifying the function to just step through every integer from 1 to listMax. Only after that would I begin to consider how to format the output into columns.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void PrimeList(int listMax)
{
int number = 1;
while (number <= listMax)
{
if (isPrime(number))
{
cout << setw(4) << number;
}
number++;
}
}
|
Output:
1 2 3 5 7 11 13 17 19 23 29 |