-
April 27th, 2009, 10:31 AM
#1
Help Please before i go crazy!!!!
Hi there can you help me understand what is happening in this program or in other words try and explain the logic of it to me, i can understand the program but for one line.
This is the line: for(j=2; j <= i/2; j++)
Like is this for statement a loop or what is it. I just don't get the j<=i/2 bit can some please expalin this bit to me, before i go crazy.
The program basically is to find and output prime numbers between 1 - 100.
#include <iostream>
using namespace std;
int main() {
int i, j;
bool isprime;
for(i=1; i < 100; i++)
{
isprime = true;
// see if the number is evenly divisible
for(j=2; j <= i/2; j++)
// if it is, then it is not prime
if((i%j) == 0)
isprime = false;
if(isprime)
cout << i << " is prime.\n";
}
return 0;
}
Many Thanks
almul
-
April 27th, 2009, 10:42 AM
#2
Re: Help Please before i go crazy!!!!
Use code tags like this:
Code:
#include <iostream>
using namespace std;
int main()
{
int i, j;
bool isprime;
for(i=1; i < 100; i++)
{
isprime = true;
// see if the number is evenly divisible
for(j=2; j <= i/2; j++)
// if it is, then it is not prime
if((i%j) == 0)
isprime = false;
if(isprime)
cout << i << " is prime.\n";
}
return 0;
}
Would it make more sense to you like this
Code:
#include <iostream>
using namespace std;
int main()
{
int i, j;
bool isprime;
for(i=1; i < 100; i++)
{
isprime = true;
// see if the number is evenly divisible
int limit = i / 2;
for(j=2; j <= limit; j++)
// if it is, then it is not prime
if((i%j) == 0)
isprime = false;
if(isprime)
cout << i << " is prime.\n";
}
return 0;
}
It's the same thing
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
April 27th, 2009, 10:50 AM
#3
Re: Help Please before i go crazy!!!!
Thanks but what does the: "i/2" bit actually mean. Thats what im having trouble understanding.
Also is the second for statement an actually loop???
Can someone please explain.
Thanks
-
April 27th, 2009, 10:54 AM
#4
Re: Help Please before i go crazy!!!!
Originally Posted by almul
Thanks but what does the: "i/2" bit actually mean. Thats what im having trouble understanding. Can someone please explain.
Just like kindergarten arithmetic...
For integers:
1 / 2 = 0
2 / 2 = 1
3 / 2 = 1
4 / 2 = 2
5 / 2 = 2
6 / 2 = 3
7 / 2 = 3
8 / 2 = 4
etc.
-
April 27th, 2009, 11:00 AM
#5
Re: Help Please before i go crazy!!!!
Ok got that bit sorted now for the second for statement. Is this 2nd for statement a loop if it is then where are its curley brackets?
Thanks
-
April 27th, 2009, 12:00 PM
#6
Re: Help Please before i go crazy!!!!
As supplied, this code is equivilent to:
Code:
#include <iostream>
using namespace std;
int main()
{
int i, j;
bool isprime;
for(i=1; i < 100; i++)
{
isprime = true;
// see if the number is evenly divisible
for(j=2; j <= i/2; j++) if((i%j) == 0) isprime = false;
if(isprime) cout << i << " is prime.\n";
}
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|