Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   nested for loop problem (http://www.velocityreviews.com/forums/t610951-nested-for-loop-problem.html)

 notahipee 05-05-2008 06:42 PM

nested for loop problem

Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.

#include <iostream.h>
#include <math.h>

int main ()
{
int a, b, c, d;

cout << "Enter two integers ";
cin >> a >> b;

for (c=a; c==b; c++)
{
for(d=2; d<=sqrt(c); d++)
{
if(c%d==0)
continue;

else cout << c;
}
}
cout << " are the prime numbers in the range " << a << " to " << b <<
endl;

return 0;
}

 red floyd 05-05-2008 06:50 PM

Re: nested for loop problem

notahipee wrote:
> Would someone be able to tell me why this isn't working. The nested

(http://www.parashift.com/c++-faq-lit....html#faq-5.8). Be a
bit more specific as to "this isn't working". What are your inputs?

Note that from a cursory examination of the code, I could figure out
what "isn't working" means, but still...

> for loops seem correctly coded to me. I would appreciate any input.
>
> #include <iostream.h>
> #include <math.h>
>
> int main ()
> {
> int a, b, c, d;
>
> cout << "Enter two integers ";
> cin >> a >> b;
>
> for (c=a; c==b; c++)

are you sure this is what you want? The loop will only execute as long
as the second statement is true. So if a and b are different, well...
nothing happens (which I assume is your complaint).
> {
> for(d=2; d<=sqrt(c); d++)
> {
> if(c%d==0)
> continue;
>
> else cout << c;
> }
> }
> cout << " are the prime numbers in the range " << a << " to " << b <<
> endl;
>
> return 0;
> }

 Christopher 05-05-2008 06:53 PM

Re: nested for loop problem

On May 5, 1:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> I believe you wanted 'c<b' or 'c!=b'.

#include <iostream>
#include <cmath>

using namespace std;

or qualify:
std::cin
std::cout
etc.

 notahipee 05-05-2008 06:56 PM

Re: nested for loop problem

On May 5, 11:50*am, red floyd <no.s...@here.dude> wrote:
> notahipee wrote:
> > Would someone be able to tell me why this isn't working. The nested

>
> (http://www.parashift.com/c++-faq-lit....html#faq-5.8). *Be a
> bit more specific as to "this isn't working". *What are your inputs?
>
> Note that from a cursory examination of the code, I could figure out
> what "isn't working" means, but still...
>
> > for loops seem correctly coded to me. I would appreciate any input.

>
> > #include <iostream.h>
> > #include <math.h>

>
> > int main ()
> > {
> > * *int a, b, c, d;

>
> > * *cout << "Enter two integers ";
> > * * * * * *cin >> a >> b;

>
> > * *for (c=a; c==b; c++)

>
> are you sure this is what you want? *The loop will only execute as long
> as the second statement is true. *So if a and b are different, well...
> nothing happens (which I assume is your complaint).
>
>
>
> > * *{
> > * * * * * *for(d=2; d<=sqrt(c); d++)
> > * * * * * *{
> > * * * * * * * * * *if(c%d==0)
> > * * * * * * * * * * * * * *continue;

>
> > * * * * * * * * * *else cout << c;
> > * * * * * *}
> > * *}
> > * *cout << " are the prime numbers in the range " << a << " to " << b <<
> > endl;

>
> > * *return 0;
> > }- Hide quoted text -

>
> - Show quoted text -

I am trying to get the first loop to cycle from integer a to integer
b.
The second loop is supposed to test each integer for prime. The output
I intend is to print out only the primes. My opening cout and closing
cout work but the loops produced nothing.

 notahipee 05-05-2008 07:01 PM

Re: nested for loop problem

On May 5, 11:53*am, Christopher <cp...@austin.rr.com> wrote:
> On May 5, 1:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>
> > I believe you wanted 'c<b' or 'c!=b'.

>
>
> #include <iostream>
> #include <cmath>
>
> using namespace std;
>
> or qualify:
> std::cin
> std::cout
> etc.

I tried c<=b and it works. However, it is couting duplicate,
triplicate, etc. in stead of just once each.

 notahipee 05-05-2008 07:05 PM

Re: nested for loop problem

> I tried c<=b and it works. However, it is couting duplicate,
> triplicate, etc. in stead of just once each.

For example if I enter 12 and 79 for a and b.

 notahipee 05-05-2008 07:17 PM

Re: nested for loop problem

On May 5, 12:05*pm, notahipee <werldpe...@hotmail.com> wrote:
> > I tried c<=b and it works. However, it is couting duplicate,
> > triplicate, etc. in stead of just once each.

>
> For example if I enter 12 and 79 for a and b.

Oh never mind I just read the FAQ. Thank you for your help.

 Puppet_Sock 05-05-2008 08:25 PM

Re: nested for loop problem

On May 5, 2:42*pm, notahipee <werldpe...@hotmail.com> wrote:
> Would someone be able to tell me why this isn't working. The nested
> for loops seem correctly coded to me. I would appreciate any input.

[snip]

There are way better algorithms. Without even trying:

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

That has pseudo code, and a gif animation of the process.
Socks

 MiB 05-06-2008 04:12 PM

Re: nested for loop problem

On 5 Mai, 20:42, notahipee <werldpe...@hotmail.com> wrote:
> Would someone be able to tell me why this isn't working. The nested
> for loops seem correctly coded to me. I would appreciate any input.

The algorithm you use for generating the primes is very inefficient.
However, bug fixing your code with minimal changes only, try this one.
It still misidentifies 0 and 1 as prime numbers (enter 0 as start
value...).

#include <iostream>

int main ()
{
unsigned int a, b;

std::cout << "Enter two integers ";
std::cin >> a >> b;

for ( unsigned int c = a; c <= b; c++ ) {
bool isprime = true;
for( unsigned int d = 2; d * d <= c; d++ ) {
if ( c % d == 0 ) {
isprime = false;
break;
}
}
if ( isprime ) std::cout << c << ' ';
}
std::cout << "are the prime numbers in the range " << a << " to " <<
b << std::endl;

return 0;
}

 All times are GMT. The time now is 06:38 PM.