TL/DR: Your problem is your test program updated the endline variable every cycle whereas your prime number program does not update endline variable every cycle.
---------------------------------------------------------------------------------------------------------------------------------------
When you get into numbers greater than 100 the separation between prime numbers is larger than 2 spaces so you start running into problems.
Basically your program checks the endline divided by ten every cycle. regardless of if endline has been updated or not. So if the last cycle was a prime number and printed out an endl; but the new cycle was not a prime number... then endline would still be a multiple of ten making the endline If statement valid and print an extra endl;
It might be easier to see if you use this version of the code you posted. All i did was add an "x" to your endline cout
If you count the number of x's between the prime number at a end of a line and the prime number at the beginning of a line you will see that it is the same as the number of lines or "x's" in this case.
Like the end of row three is prime number 103. The beginning of line four is prime number 107. So there are 3 spaces because, program cycles through 104 not a prime number.. endline doesn't get updated so it's still equal to 30. 30 modulo 10 is 0 so print out an endl. then it tests 105. 105 is not prime so print out an endl. then it tests 106. 106 is not prime so it prints out an endl. then it tests 107. 107 is prime so it iterates endline and doesn't print again.
Now row 4 ends with prime number 151 and the beginning of row 5 is prime number 157. so the program would test 152, not prime, so 40 modulo 10 is 0 so print endl, 153 not prime so print endl, the same with 154, 155, 156 which all print an endl. Then 157 is prime so it iterates endline and no more endl's get printed until the next row. In that case it is 5 endl's between rows. Which is the same as the difference between the last prime number of the previous row and the first prime number of the next row.
What you need to do is reset the endline variable to zero whenever the if statement that prints the endl; is run. Then set an extra check to the print endl; if statement so it doesn't run if its equal to 0 like I had in my previous post.
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
|
#include <iostream>
#include <cmath>
using namespace std;
void _Max_();
void prime(int);
int main()
{
_Max_();
return 0;
}
void _Max_()
{
int input;
cout << "Enter the number in which you want to find all prime numbers up to: ";
cin >> input;
prime(input);
}
void prime(int Size)
{
int Array[Size];
int x = 0, endline = 0;
for (int fillArray=0; fillArray<Size; fillArray++)
{
Array[fillArray] = fillArray+1;
}
for (int i=1; i<(Size-1); i++)
{
for (int j=2; j<=sqrt(i); j++)
{
if (Array[i]%j == 0)
{
x = 1;
}
}
if (Array[i] == 4)
{
x =1;
}
if (x == 0)
{
cout << Array[i] << " ";
endline++;
}
x = 0;
if (endline%10 == 0)
{
cout << "x" << endl;
}
}
}
|