# Corrections on School assignment...

• 05-19-2009
hubris
Corrections on School assignment...
I used some examples for calculating prime numbers up to 50,000 and had a code that worked great (no user input which is what the instructor wanted. But she clarified and said she actually wanted the output to look somethihng like this:

Start End Number of Primes
1 1000 168
1001 2000 135
2001 3000 127
3001 4000 120
4001 5000 119
5001 6000 114
6001 7000 117
7001 8000 107
8001 9000 110
9001 10000 112

49001 50000 98

Total primes in the first 50 chiliads: 5133

Okay so I've been working on this and at first I had it outputting primes in one column and zeros in another in an infinite loop untill I nested a while loop in a for loop. Ignoring the bottom 2 lines of output (I'll figure out those later) please look at what I have and help me understand why I get NO output.
Code:

```#include <iostream> #include <iomanip> using namespace std; void prime_num(int);//Void to specify no function type. int primeCount = 0, numCount1 = 0, numCount2 = 0, rangeStart = 1, rangeEnd = 1000; int main() {     int num = 50000;  //Establishing variables         prime_num(num); }   void prime_num( int num) {                 bool isPrime = true;                 for ( int num2 = 2; num2 <= num; num2++)                 {                                 for ( int num3 = 2; num3 < num2; num3++)//Forloops to create count.                                 {                                                 if ( num2 % num3 == 0 )//if with a mod to test for !prime.                                                 {                                                   isPrime = false;                                                   break;//break from if...                                                   for(isPrime; numCount1 <  1000 && numCount2 < 50000; numCount1++);                                           {                           primeCount++;                           numCount2++;                           break;                                                         while(numCount1 >= 1000)                                 {                                 cout << rangeStart << setw(5) << rangeEnd << setw(5)<< primeCount << endl;//Print the range and the current primeCount for it                                 primeCount = 0;  //Reset primeCount and numCount1                                 numCount1 = 0;                                 rangeStart = rangeStart + 1000;                                 rangeEnd = rangeEnd + 1000;                                 break//break from while to reset the ranges and and first two counts                                 }                             }                         }                            }                                                                                        isPrime=true;                 }                 system("pause"); }```
//

• 05-19-2009
hk_mp5kpdw
Some better indentation formatting helps:
Code:

```#include <iostream> #include <iomanip> using namespace std; void prime_num(int);//Void to specify no function type. int primeCount = 0, numCount1 = 0, numCount2 = 0, rangeStart = 1, rangeEnd = 1000; int main() {     int num = 50000;  //Establishing variables     prime_num(num); } void prime_num( int num) {     bool isPrime = true;     for ( int num2 = 2; num2 <= num; num2++)     {         for ( int num3 = 2; num3 < num2; num3++)//Forloops to create count.         {             if ( num2 % num3 == 0 )//if with a mod to test for !prime.             {                 isPrime = false;                 break;//break from if...                 for(isPrime; numCount1 <  1000 && numCount2 < 50000; numCount1++);                 {                     primeCount++;                     numCount2++;                     break;                     while(numCount1 >= 1000)                     {                         cout << rangeStart << setw(5) << rangeEnd << setw(5)<< primeCount << endl;//Print the range and the current primeCount for it                         primeCount = 0;  //Reset primeCount and numCount1                         numCount1 = 0;                         rangeStart = rangeStart + 1000;                         rangeEnd = rangeEnd + 1000;                         break;//Added a semicolon here//break from while to reset the ranges and and first two counts                     }                 }             }                    }                          isPrime=true;     }     system("pause"); }```
See that first break?
• 05-19-2009
matsp
You need to indent your code correctly. Then you may find out why your code is not working correctly.

Code:

`                                          for(isPrime; numCount1 <  1000 && numCount2 < 50000; numCount1++);`
This looks EXTREMELY suspicious - what is it supposed to do? But it's also immediately after a break, so it probably doesn't actually get executed at all.

Code:

`                                break//break from while to reset the ranges and and first two counts`
Huh? No semicolon - doesn't compile with my compiler. And why put an unconditional break in a while-loop - it will never do more than one iteration, since the break will always be done. There is something called if that has the same effect, but clearer to the reader, and no need for a break.

--
Mats
• 05-19-2009
hubris
Okay I think the missing semi was just a "copy/paste" typo. But I see what you're saying about the break now. but when I utilize if (tried both) I get a blinking cursor (code compiles, just...doesn't do anything)

Corrected code now posted.

Code:

```#include <iostream> #include <iomanip> using namespace std; void prime_num(int);//Void to specify no function type. int primeCount = 0, numCount1 = 0, numCount2 = 0, rangeStart = 1, rangeEnd = 1000; int main() {     int num = 50000;  //Establishing variables         prime_num(num); }   void prime_num( int num) {                 bool isPrime = true;                 for ( int num2 = 2; num2 <= num; num2++)                 {                                 for ( int num3 = 2; num3 < num2; num3++)//Forloops to create count.                                 {                                                 if ( num2 % num3 == 0 )//if with a mod to test for !prime.                                                 {                                                     isPrime = false;                                                     break;//break from if...                                                     for(isPrime; numCount1 <  1000 && numCount2 < 50000; numCount1++);                                             {                             primeCount++;                             numCount2++;                                   if(numCount1 >= 1000)                                   {                                   cout << rangeStart << setw(5) << rangeEnd << setw(5)<< primeCount << endl;//Print the range and the current primeCount for it                                   primeCount = 0;  //Reset prime count and numCount1                                   numCount1 = 0;                                   rangeStart = rangeStart + 1000;                                   rangeEnd = rangeEnd + 1000;                                                                   }                             }                         }                            }                                                                                        isPrime=true;                 }                 system("pause"); }```
• 05-19-2009
matsp
You should still indent your code, like hk_mp5pdw says.

--
Mats
• 05-19-2009
hubris
okay indentation appended a bit (pastes a bit different than my Bloodshed for some reason) Removed both breaks and got the same result as this with only one break at the bottom and now it's just creating numbers infinitely. I'll keep tweaking it and checking for replies.

Here's the code for my infinite real number producer. (think there's any demand for an executable like this?)
Code:

```#include <iostream> #include <iomanip> using namespace std; void prime_num(int);//Void to specify no function type. int primeCount = 0, numCount1 = 0, numCount2 = 0, rangeStart = 1, rangeEnd = 1000;//Also noticed if I put this in main it doesn't see it. int main() {     int num = 50000;  //Establishing variables         prime_num(num); }   void prime_num( int num) {                 bool isPrime = true;                 for ( int num2 = 2; num2 <= num; num2++)                 {                                 for ( int num3 = 2; num3 < num2; num3++)//Forloops to create count.                                 {                                                 if ( num2 % num3 == 0 )//if with a mod to test for !prime.                                                 {                                                     isPrime = false;                                                                                                         for(isPrime; numCount1 <  1000 && numCount2 < 50000; numCount1++);                                             {                             primeCount++;                             numCount2++;                                   while(numCount1 >= 1000)                                   {                                   cout << rangeStart << setw(5) << rangeEnd << setw(5)<< primeCount << endl;//Print the range and the current primeCount for it                                   primeCount = 0;  //Reset prime count and numCount1                                   numCount1 = 0;                                   rangeStart = rangeStart + 1000;                                   rangeEnd = rangeEnd + 1000;                                   break;                                   }                             }                         }                            }                                                                                        isPrime=true;                 }                 system("pause"); }```
• 05-19-2009
hubris
Clarification: The code is to produce all prime numbers within each chiliad to 50,000 (with the above general output format.) Example :
Code:

```Start    End      Number of Primes 1          1000    168 1001    2000    135...etc.```
(for some reason these message boxes distort my indentation)
• 05-19-2009
hk_mp5kpdw
The way I might structure the program, I'd have a couple functions:

bool IsPrime(int) - Test if a given number is prime or not.
int CountPrimes(int min,int max) - Loops from min to max and counts/returns how many primes there are in that interval, calls IsPrime function.

Then, the main function would loop over the required ranges and repeatedly call the CountPrimes for the particular range and display the min/max/count as required along with keeping track of the overall total. At the end of the main function, you'd display the total count and the average.
• 05-19-2009
brewbuck
Chiliad, eh? That's the first new, non-technical word I've learned in quite a while.
• 05-19-2009
hubris
Big, Big thank yous to all who've helped. This was good ammunition and I'll be applying it (due tomorrow) tonight after class.