quick c++ help plz

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Afrobean
    Admiral in the Red Army
    • Dec 2003
    • 13262

    #1

    quick c++ help plz

    Code:
    bool prime(int n) 
    {
    	for (int i = 2; i == (n-1); i++) 
    	{
    		if(n==2)
    		{
    			return true;
    		}
    		if ((n % i) == 0) 
    		{
    			return false;
    		}
    	}
    	return true;
    } 
    
    int main()
    {
    	int count = 0;
    	bool isitprime;
    	
    	cout << "The prime numbers from 1 to 10000 are:\n";
    	
    	for(int loop = 1; loop <= 10000; loop++)
    	{
    		isitprime = prime(loop);
    		if (loop == 2)
    		{
    			isitprime = true;
    		}
    		else if (isitprime == true) 
    		{
    			++count;
    			cout << setw(6) << loop;
    			
    				if ((count % 10) == 0) 
    				{
    					cout << "\n";
    				}
    
    		}
    
    	}
    	cout << '\n' << "There were " << count
    			 << " prime numbers.\n";
    			 
    	return 0;			 
    }
    It's supposed to output all of the prime numbers between 1 and 10000 and I am no longer in any mood to try to figure out what I did wrong here. Can anyone see any obvious and blaring mistakes (obviously I'm using bad technique to do this, but I mean any easy fixed to get it to work)? As is that outputs every number between 1 and 10000. I also had a build earlier that looked to be 100% correct yet when it came to outputting the numbers, it didn't output ****.

    EDIT: **** it. Gotta go home now. Still open for help on this though, because I'll have to email it sometime to the instructor before too long. However, I have work tonight and I must now jet home and try to get a shower and shave in before.
    Last edited by Afrobean; 01-23-2007, 07:08 PM.
  • itmorr
    Custom User Title
    • Jun 2006
    • 1498

    #2
    Re: quick c++ help plz

    I can't try compiling it at the moment, but might it have anything to do with using 'loop' as a variable name? Also, might it have anything to do with short circuiting on the if-then? Like, if it sees that loop==2, it isn't going to perform the ++count or anything in that block, will it? What's the output look like?




    Originally posted by jwcgator
    (12:31:27 AM) jwcgator2: I got it! I'll write an auto-procrastination program!
    (12:31:33 AM) jwcgator2: meh, i'll make it later

    Comment

    • RandomPscho
      FFR Player
      • Jun 2006
      • 504

      #3
      Re: quick c++ help plz

      Simple error.

      Code:
      bool prime(int n) 
      {
      	for (int i = 2; [u][b][i]i<n[/i][/b][/u]; i++) //LOOK AT THIS LINE!!!
      	{
      		if(n==2)
      		{
      			return true;
      		}
      		if ((n % i) == 0) 
      		{
      			return false;
      		}
      	}
      	return true;
      }
      You are checking if i==(n-1), which is only true when i=2 and n=3, so every other time the loop is skipped, and 3 is prime so it gets displayed anyway.

      i<n is the same as i<=(n-1) which is what I think you meant to type.

      I would also take away the checks if n=2 because it is redundant and makes the program less efficient.

      EDIT: Here is my version with the same logic

      Code:
      #include <iostream>
      using namespace std;
      
      bool prime(int num) //It is good practice to avoid single letters for names
      {
      	for (int i = 2; i <num; i++) 
      	{        		
      		if ((num % i) == 0) 
      		{
      			return false;
      		}
      	}
      	return true;
      } 
      
      int main()
      {
      	int count = 0;
      	//Read below, the isitprime variable is not needed if you add the prime() check inside the if statement
      	
      	cout << "The prime numbers from 1 to 10000 are:\n";
      	
      	for(int loop = 1; loop <= 10000; loop++)
      	{
      		if (prime(loop)) //Notice how you can put a boolean function in an if statement
      		{
      			++count;
      			cout.width(6);
                              cout << loop;			
      			if ((count % 10) == 0) 
      			{
      				cout << "\n";
      			}
      		}
      	}
      	cout << '\n' << "There were " << count << " prime numbers.\n";	
      	system("PAUSE");			 
      	return 0;			 
      }
      I don't know what library you are using, iostream.h and iostream both do not have the cout << setw() command. Both have cout.width() though.
      Last edited by RandomPscho; 01-24-2007, 04:20 PM.

      Comment

      • Afrobean
        Admiral in the Red Army
        • Dec 2003
        • 13262

        #4
        Re: quick c++ help plz

        I don't know what library you are using, iostream.h and iostream both do not have the cout << setw() command. Both have cout.width() though.
        Honestly, I'm not sure. It was part of a set of template code. Had I done it from scratch, I probably would have had an easier time with it.

        As for the changes you recommended: I believe that's what I put when I said this above:
        I also had a build earlier that looked to be 100% correct yet when it came to outputting the numbers, it didn't output ****.
        Basically, it would display "the prime numbers between 1 and 10000 are:" and nothing else. I think it got trapped in an infinite loop before displaying anything, because with the compiler I was using, it gives a "press any key to continue" when the program is finished running.

        Anyway... the cpp I saved ended up being corrupted, so I might just code it from scratch and steal the few lines of output from the template.

        Comment

        • RandomPscho
          FFR Player
          • Jun 2006
          • 504

          #5
          Re: quick c++ help plz

          All you have to change is that i<n and it will work fine. That is the only problem in the first code you posted. My rendition of the code also works 100% (from the previous post.

          Comment

          • Afrobean
            Admiral in the Red Army
            • Dec 2003
            • 13262

            #6
            Re: quick c++ help plz

            Still gonna code it from scratch anyway since my original cpp got corrupted somehow. I'll be sure to keep what you said in mind though. Thanks for the help.

            By the way, shouldn't there be a function in math or something for checking if a number is prime? I probably wouldn't get much credit in class if I used something like that, but it seems like that would be something that should be included in math.

            Comment

            • FoJaR
              The Worst
              • Nov 2005
              • 2816

              #7
              Re: quick c++ help plz

              Code:
              bool isItPrime(int a);
              
              int main()
              {
              int x = 0;
              for(i = 10000; i > 0; i--)
              {
              if(isItPrime(i))
              {
              array[x] = i;
              x++;
              }
              }
              
              cout << "afro is a noobface";
              }
              
              bool isItPrime(int a)
              {
              int x = 0;
              
              for(i = 2; i < a; i++)
              {
              if(a%i == 0 && a != i)
              x++;
              }
              
              if(x > 0)
              return false;
              
              else
              return true;
              }
              Last edited by FoJaR; 01-25-2007, 02:21 AM.

              Comment

              • RandomPscho
                FFR Player
                • Jun 2006
                • 504

                #8
                Re: quick c++ help plz

                Math.h from what I know contains trig functions, hyperbolic functions, logarithms, expenential functions, power/root funstions, and rounding functions.

                Comment

                • FoJaR
                  The Worst
                  • Nov 2005
                  • 2816

                  #9
                  Re: quick c++ help plz

                  fojar's code is the best oh man why am i not using fojar's code

                  Comment

                  • Afrobean
                    Admiral in the Red Army
                    • Dec 2003
                    • 13262

                    #10
                    Re: quick c++ help plz

                    rofl I never did this

                    I think I'm going to end up not doing this week's work as well. I think it's something about arrays or something. Maybe I'll look at it. I mean, I was able to understand the concept that last week's was supposed to represent (having more than one function )... I just got hung up on stupid prime number logic bull****. I actually haven't looked at this week's work yet though hahaha

                    Comment

                    • RandomPscho
                      FFR Player
                      • Jun 2006
                      • 504

                      #11
                      Re: quick c++ help plz

                      Prime number is easy. If you count down from 1 less than the number down to 2 and nothing is divisible; ITS PRIME! Your method has more than one function, you should be able to understand it


                      An array/matrix or however many dimensions you choose to use is like a grid.

                      int array[5] would be:
                      [0][1][2][3][4]
                      The number inside I am using the element's index. It starts at 0 and counts up one.

                      int matrix[3][3] would be
                      [1][2][3]
                      [4][5][6]
                      [7][8][9]
                      Where to access say 9, it would be matrix[2][2] because the rows and column numbering starts at 0, so 2 over and 2 down (which is really 3 over and 3 down).

                      Any questions just ask, please

                      Comment

                      • FoJaR
                        The Worst
                        • Nov 2005
                        • 2816

                        #12
                        Re: quick c++ help plz

                        Originally posted by Afrobean
                        rofl I never did this

                        I think I'm going to end up not doing this week's work as well. I think it's something about arrays or something. Maybe I'll look at it. I mean, I was able to understand the concept that last week's was supposed to represent (having more than one function )... I just got hung up on stupid prime number logic bull****. I actually haven't looked at this week's work yet though hahaha
                        use my code

                        if you need help afro, we here man we here

                        Comment

                        Working...