Thread: homework help. cant get for loop to count right

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    homework help. cant get for loop to count right

    hi everyone
    i have a minor problem. my for loop wont count right. i am so confused as to why it will not work. so if anyone can be of some assistance i would greatly appreciate it.
    i am over looking something and over analyzing this so i need an outside opinion.
    purpose of program is to test numbers to see if they are prime. just in case you are wondering.
    Inside my function "find_prime" i have for loop and it is always equal to 2. The idea is to count up and divide the number by various numbers (j) to see if it is prime.
    My if statement is never true for some reason so can someone please give me some ideas how to make this work right. thanks
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    bool find_prime(int);
    
    int main ()
    {
    	int i, counter =0;
    	bool test;
    
    	for (i =2; i <= 500; i++)
    	{
    		test = find_prime(i);
    		if ( test == true )
    		{
    			counter++;
    			cout << setw(6) << i << "\t";
    		}
    	}
    	cout << "\nThe total number of the prime numbers between 2 and "  << i << " is " << counter << endl;
    	return 0;
    }
    
    bool find_prime (int m)
    {
     int temp, j;
     for ( j = 2; j < m - 1 ; j++ )
     {
    	temp = m % j;
    
    if (temp == 0)
    	{
    	cout << "j is " << j;
    	return false; 
    	}
    else 
     {
    	 return true;
    	 break;
     }
     }}

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    I think you should consider your algorithm, why is j always equal to 2? A number NUM is a prime if its only divisors are 1 and NUM. The first ten positive primes are as follows, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
    When no one helps you out. Call google();

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I know a certain little girl who didn't read the rules thread.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    i did read the rules what did i mess up?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    and j is not always equal to 2...thats the intial value...my counter is not working which is supposed to make j count up..and divide the number by the new number until it not longer works and thus its prime
    thats what im having trouble with

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    In this code:
    Code:
    for (i =2; i <= 500; i++)
    	{
    		test = find_prime(i);
    What does i equal the first time through the loop? You are sending that value to the find_prime() function. Let's look at find_prime():
    Code:
    bool find_prime (int m)
    {
     int temp, j;
     for ( j = 2; j < m - 1 ; j++ )
    What does m equal? What does j equal initially? Is j < m -1 true or false? What happens then?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...also:

    1) Will the following break statement ever execute?

    Code:
    return true;
    break;
    2) Do you need to test if even numers are prime? (Hint: loops can be incremented by 2 if you want: i = i+2)

    3) Also, if the divisor j is such that, j * j is greater than the number you are checking for prime, then you can stop checking, i.e. you can break. Your teacher may not have told you that, but if you haven't found a factor for the number you are checking by then, you won't find one in the rest of the loop.
    Last edited by 7stud; 03-09-2005 at 05:34 PM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    the problem is that always excutes. return true. the output is all odd numbers. i cannot get the counter "j" to count up and divide "m" the number to be tested to work correctly. all the evne numbers have been weeded out, but i am having a lot of trouble figuring out how to test the odd numbers to be prime. i dont care how slow the program is i just need it to work.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    The way you're going about this looks.. less than optimal.. to start with. I wrote this code, but I'm going to erase some of what's inside the loops and put little comment marks - that's for you to figure out what I did and reproduce. I'm not going to give you the answer, just a bit of a nudge.

    Code:
    #include <iostream>
    
    bool is_prime(/* I passed something here. */);
    
    int main()
    {
    	int endnum = /* Initalizing variables is good. */;
    	
    	std::cout << "Input ending number to test: ";
    	std::cin >> endnum;
    	
    	std::cout << "\nThe list of prime numbers is:\n";
    	
    	for (/* I have some loop conditions here. What were they? */)
    	{
    		is_prime(x);
    	}
    	
    	std::cin.ignore(80, '\n'); // Gets rid of the errant newline in the input stream.
    	std::cin.get(); // Keeps the window open.
    	return(0);
    }
    
    bool is_prime(/* */)
    {
    	for (int y = 2; y < x; y++) // Pay attention to how you can declare a variable locally to a loop. This will help you.
    	{
    		if (/* There was a condition checked here. Figure it out. */)
    		{
    			// Do something.
    		}
    	}
    	
    	// Handle the other condition.
    }

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    the problem is...
    The problem is, you didn't take the time to answer the questions I posed in my first post. The questions should be simple to answer, and if you write down the answers to each one, it should lead you to the problem. If you answer the questions, and you still can't see the problem, then post your answers, and I'll be happy to discuss them with you.

    Here they are again:

    What does i equal the first time through the loop? You are sending that value to the find_prime() function. Let's look at find_prime():
    Code:

    bool find_prime (int m)
    {
    int temp, j;
    for ( j = 2; j < m - 1 ; j++ )


    What does m equal? What does j equal when the for loop starts? Is j < m -1 true or false?

    Lithorien,

    Writing code for beginners does not help them in any way, and it violates the homework policy on this site. As should be apparent, the poster knows how to structure a main() function and call another function, so your post doesn't help in that regard.
    Last edited by 7stud; 03-09-2005 at 08:56 PM.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by 7stud
    Lithorien,

    Writing code for beginners does not help them in any way, and it violates the homework policy on this site. As should be apparent, the poster knows how to structure a main() function and call another function, so your post doesn't help in that regard.
    Depends on the beginner. I learn more from seeing code in context with certain things commented out that I have to figure out than I do from comments like yours.

    It depends on the person -- and I didn't violate the homework policy because it's not completed code. Why not read it and see?

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    I agree, to actrually see the answer helps me more for the future then having to try and work it out on my own and then keep doing something wrong in the future.
    PS
    my feelings have been hurt giving me a red mark
    all because I stand up for all us new guys. (ok I been doing C++ off and on for years, but never far enough to learn Object programming,,,, but that's another post,)
    Last edited by chrismax2; 03-10-2005 at 06:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  2. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  3. Help on Homework
    By Sentiax in forum C Programming
    Replies: 13
    Last Post: 02-04-2009, 12:33 AM
  4. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM