nested for loop problem

Discussion in 'C++' started by notahipee, May 5, 2008.

  1. notahipee

    notahipee Guest

    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;
    }
     
    notahipee, May 5, 2008
    #1
    1. Advertising

  2. notahipee

    red floyd Guest

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

    Please read the FAQ 5.8
    (http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8). Be a
    bit more specific as to "this isn't working". What are your inputs?
    What is your output? What is your *expected* output?

    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;
    > }
     
    red floyd, May 5, 2008
    #2
    1. Advertising

  3. notahipee

    Christopher Guest

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


    Additionally, you should be using the standard C++ headers:

    #include <iostream>
    #include <cmath>

    using namespace std;

    or qualify:
    std::cin
    std::cout
    etc.
     
    Christopher, May 5, 2008
    #3
  4. notahipee

    notahipee Guest

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

    >
    > Please read the FAQ 5.8
    > (http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8).  Be a
    > bit more specific as to "this isn't working".  What are your inputs?
    > What is your output?  What is your *expected* output?
    >
    > 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, May 5, 2008
    #4
  5. notahipee

    notahipee Guest

    On May 5, 11:53 am, Christopher <> wrote:
    > On May 5, 1:47 pm, "Victor Bazarov" <> wrote:
    >
    > > I believe you wanted 'c<b' or 'c!=b'.

    >
    > Additionally, you should be using the standard C++ headers:
    >
    > #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, May 5, 2008
    #5
  6. notahipee

    notahipee Guest


    > 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.
    output reads 131314151617171718191919202121.....
     
    notahipee, May 5, 2008
    #6
  7. notahipee

    notahipee Guest

    On May 5, 12:05 pm, notahipee <> 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.
    > output reads 131314151617171718191919202121.....


    Oh never mind I just read the FAQ. Thank you for your help.
     
    notahipee, May 5, 2008
    #7
  8. notahipee

    Puppet_Sock Guest

    On May 5, 2:42 pm, notahipee <> 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
     
    Puppet_Sock, May 5, 2008
    #8
  9. notahipee

    MiB Guest

    On 5 Mai, 20:42, notahipee <> 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;
    }
     
    MiB, May 6, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Low

    Nested for loop problem

    Don Low, Nov 8, 2003, in forum: Python
    Replies:
    4
    Views:
    525
    Duncan Smith
    Nov 9, 2003
  2. Replies:
    3
    Views:
    494
    Victor Bazarov
    Dec 7, 2007
  3. Adrienne Boswell

    Re: problem with nested loop (I think)

    Adrienne Boswell, Jan 25, 2009, in forum: HTML
    Replies:
    1
    Views:
    376
  4. middletree

    problem with nested loop (I think)

    middletree, Jan 23, 2009, in forum: ASP General
    Replies:
    8
    Views:
    128
    Adrienne Boswell
    Jan 25, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    457
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page