# homework help. cant get for loop to count right

Printable View

• 03-09-2005
bluegoo06
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;  }  }}```
• 03-09-2005
InvariantLoop
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
• 03-09-2005
Krak
I know a certain little girl who didn't read the rules thread.;)
• 03-09-2005
bluegoo06
i did read the rules what did i mess up?
• 03-09-2005
bluegoo06
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
• 03-09-2005
7stud
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?
• 03-09-2005
7stud
...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.
• 03-09-2005
bluegoo06
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.
• 03-09-2005
Lithorien
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. }```
• 03-09-2005
7stud
Quote:

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.
• 03-09-2005
Lithorien
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?
• 03-10-2005
chrismax2
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,)