Corrections on School assignment...

This is a discussion on Corrections on School assignment... within the C++ Programming forums, part of the General Programming Boards category; I used some examples for calculating prime numbers up to 50,000 and had a code that worked great (no user ...

  1. #1
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33

    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
    Average number per chiliad: 102.66


    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");
    }
    //

    PLEASE HELP I have one more day to reSubmit it.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    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?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    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");
    }
    Last edited by hubris; 05-19-2009 at 01:26 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should still indent your code, like hk_mp5pdw says.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    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");
    }
    Last edited by hubris; 05-19-2009 at 01:35 PM.

  7. #7
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    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)
    Last edited by hubris; 05-19-2009 at 01:41 PM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Chiliad, eh? That's the first new, non-technical word I've learned in quite a while.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    Big, Big thank yous to all who've helped. This was good ammunition and I'll be applying it (due tomorrow) tonight after class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for school assignment
    By Allynia in forum C Programming
    Replies: 9
    Last Post: 04-29-2008, 03:07 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. stupid school assignment
    By major_small in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-20-2003, 09:06 AM
  5. School Shooting in Germany
    By Golden Bunny in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 04-27-2002, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21