Thread: Stuck

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    11

    Stuck

    Ive just started learning c++ and I've been working on writing a program for over a week now, at this point I've pretty much lost hope and have no clue what to do. Im trying to write a program that will list out numbers 1-50 along with their factors and whether they are prime or not, so basically Im trying to create a sieve of erathsothenes thing that will predict whether the numbers are prime or not, and do it with the basic stuff like loops and if statements, ect..does anyone have any suggestions?


    this is how far ive gotten:....

    Code:
    #include <iostream>
    #include <math.h>
    
    double numb;
    double factor;
    double leftover;
    double numoffactors;
    
    
    int main()
    {
    
    	using namespace std;
    
    	cout << "Number          Factor          Prime or Not" << endl;   
    	cout<< " ------------------------------------------"<< endl;
    	cout<<  " 1              doesn't count      Not Prime     "<< endl;
    	cout.width(10);
    	for(int numb=2;numb <=20;numb++)		
    	{
    	cout << numb << endl;
    	cout.width(19);
    	cout<< factor<<endl;
    	cout.width(39);
    	cout<<leftover<<endl;
    }
    
    
    
    for(int factor = 2;factor<=20;factor++)
    	
     	
     while (numb>factor)	
     {
     	leftover= fmod(numb,factor);
     }
     
     
    
    	
    if(leftover == 0)
    		{
    			numoffactors = (factor + 1);
    		 
    		}
    		else
    			{
    			numoffactors=1;
    			}
    		
    		
    		
    if(numoffactors < 2) 
    		{
    			cout.width(35);
    			cout << "Prime" << endl;
    		}
    			
    	
    			
    	
    if(numoffactors > 2) 
    		{
    			cout.width(35);	
    			cout << "Not Prime" << endl;
    		}
    			
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
    #include <cmath>  //use the up to date math header
    using namespace std;  //make stuff in namespace std 
    					  //available throughout program
    int main()
    {
      //avoid global variables whenever possible
      //every number you use will be an int so use ints
      //not doubles
      int numb;
      int factor;
      int numoffactors;
      cout << "Number		  Factor		  Prime or Not" << endl;   
      cout<< " ------------------------------------------"<< endl;
      cout<<  " 1			  doesn't count	  Not Prime	 "<< endl;
      cout.width(10);
      //factor should be given a meaningful value before 
      //you attempt to use it 
      //don't redeclare numb and factor in loops
      for(numb = 2; numb <= 20; ++numb)  //prefer prefix increments over 
    									 //postfix
    									 //be generous but not over
    									 //indulgent with spaces to make 
    									 //the code easier to read
      {
    	cout << numb << endl;
    	cout.width(19);
      
    	numoffactors = 2;			   //all numbs have at least two 
    									//"factors", 1 and numb
    									//set numoffactors to 2 for each
    									//time through outer loop
    	for(factor = 2; factor<= numb; ++factor) 
    									//don't use 20, use numb and 
    									//drop a loop
    	{ 
    	  if(numb % factor == 0)		//I prefer % to fmod() 
    	  {
    		cout << factor << ", ";
    		numoffactors += 1;		 
    	  }
    	} 
     
    	cout.width(35);			//only need this once
    	if(numoffactors == 2)	
    	  cout << "Prime" << endl;
    	else					//Prime, Not Prime is mutually
    								   //exclusive so use if/else
    								   //rather than to ifs
    	  cout << "Not Prime" << endl;
      }
      return 0;
    }
    I'll avoid comment on indentation practices because I never know when it's you versus this board screwing you up, but try to be as neat as possible to be sure you have the correct matching {}s and {}s wherever you need them.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Thanks a bunch for your help ...

    I had just one more question on this..when I check numbers 2 through 20 it works and outputs everything just fine but when I get up to numbers like 2 through 500 the output starts looking really messy even though the rows and everything were lined up initially. I dont even necessarily need all the numbers to be displayed but is there a way for me to keep the rows and columns that are displayed lined up?

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Something like this should work with you:
    Code:
    if(numb % factor == 0) {
    	if(factor < 10)
    		cout << factor << ",   ";
    	else if(factor < 100)
    		cout << factor << ",  ";
    	else if(factor < 1000)
    		cout << factor << ", ";
    	numoffactors += 1;		 
    }
    Red = changed.
    Basically this code is checking to see how many space the number actually needs, so that all of the numbers could be outputted in a nice straight line, I hope I got what you needed.

    --EDIT--
    Ack! I was looking at the wrong code, but something like above should give you what you want. Find the number that takes up the greatest amount of characters of space, and then pad the whole column with that set of space padding. Am I making any sense?
    Last edited by Kleid-0; 01-11-2005 at 08:23 AM.

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I dont even necessarily need all the numbers to be displayed but is there a way for me to keep the rows and columns that are displayed lined up?
    You can use the <iomanip> library to format the output of your code if you're having a problem with layout. You can incorporate the following into your code...

    Code:
    #include <iomanip>
    
    #define COLWIDTH 10 // Set to whatever colum width you prefer in your table
    
    using std::setw; // If you haven't already used 'using namespace std;'
    
    cout << setw(COLWIDTH) << factor;

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Quote Originally Posted by elad
    Code:
    #include <iostream>
    #include <cmath>  //use the up to date math header
    using namespace std;  //make stuff in namespace std 
                          //available throughout program
    int main()
    {
      //avoid global variables whenever possible
      //every number you use will be an int so use ints
      //not doubles
      int numb;
      int factor;
      int numoffactors;
      cout << "Number          Factor          Prime or Not" << endl;   
      cout<< " ------------------------------------------"<< endl;
      cout<<  " 1              doesn't count      Not Prime     "<< endl;
      cout.width(10);
      //factor should be given a meaningful value before 
      //you attempt to use it 
      //don't redeclare numb and factor in loops
      for(numb = 2; numb <= 20; ++numb)  //prefer prefix increments over 
                                         //postfix
                                         //be generous but not over
                                         //indulgent with spaces to make 
                                         //the code easier to read
      {
        cout << numb << endl;
        cout.width(19);
      
        numoffactors = 2;               //all numbs have at least two 
                                        //"factors", 1 and numb
                                        //set numoffactors to 2 for each
                                        //time through outer loop
        for(factor = 2; factor<= numb; ++factor) 
                                        //don't use 20, use numb and 
                                        //drop a loop
        { 
          if(numb % factor == 0)        //I prefer % to fmod() 
          {
            cout << factor << ", ";
            numoffactors += 1;         
          }
        } 
     
        cout.width(35);            //only need this once
        if(numoffactors == 2)    
          cout << "Prime" << endl;
        else                    //Prime, Not Prime is mutually
                                       //exclusive so use if/else
                                       //rather than to ifs
          cout << "Not Prime" << endl;
      }
      return 0;
    }
    I'll avoid comment on indentation practices because I never know when it's you versus this board screwing you up, but try to be as neat as possible to be sure you have the correct matching {}s and {}s wherever you need them.


    ...k i just went back thro my programs and looked at this again, i had thought i finished it with this but the program only prints out "not primes". ive been through this several times but i cant figure out whats stopping it from identifying and printing out "primes" too.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try changing factor <= numb to factor < numb because when factor = numb numb % factor == 0 which will increment numoffactors which will cause all values of numb to be considered not prime
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Quote Originally Posted by elad
    try changing factor <= numb to factor < numb because when factor = numb numb % factor == 0 which will increment numoffactors which will cause all values of numb to be considered not prime

    ah that did the trick! thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM