subset

Discussion in 'C++' started by mary8shtr, Feb 7, 2014.

  1. mary8shtr

    mary8shtr Guest

    Hi.I write this program.but i should write it, too recursive.and Could you help me in this case.

    #include <iostream>
    using namespace std;
    int pow(int a,int b)
    {
    int p=1;
    for(int i=0;i<b;i++)
    p=p*a;
    return p;
    }
    int main()

    {
    int testcase=0;

    int n;

    long long *a;

    a=new long long[200];

    cin>>testcase;

    int i=0;

    int d,b,c;

    while(i<testcase)
    {
    cin>>n;

    for(int j=0,k=1;j<n;j++,k++)
    {
    a[j]=k;
    }


    for(int p=0;p<=pow(2,n)-1;p++)
    {
    cout<<'{';

    d=p;
    for(int m=0;m<n;m++)
    {

    c=pow(2,n-1);

    b=c&d;

    if(b!=0)

    cout<<a[m]<<' ';

    d=d<<1;
    }

    cout<<'}';

    cout<<endl;
    }

    cout<<endl;

    i++;
    }
    return 0;
    }
     
    mary8shtr, Feb 7, 2014
    #1
    1. Advertisements

  2. mary8shtr

    Stefan Ram Guest

    #include <iostream>
    #include <ostream>
    #include <vector>
    template< typename T >struct counter
    { ::std::vector< T >& sequence; int top;
    counter( ::std::vector< T >&& sequence ):
    sequence( sequence ), top( sequence.size() ){ count( 0 ); }
    void count( int const pos )
    { if( pos == top )
    { for( int pos = 0; pos < top; ++pos )
    ::std::cout <<( sequence[ pos ]?( char )( '`' +( top - pos ) ): ' ' );
    ::std::cout << '\n'; }
    else
    { sequence.at( pos )= 0; count( pos + 1 );
    sequence.at( pos )= 1; count( pos + 1 ); }}};
    int main(){ counter< int > c( ::std::vector< int >( 3 )); }
     
    Stefan Ram, Feb 7, 2014
    #2
    1. Advertisements

  3. It would make your code a lot easier to read if

    You indented consistently.
    You doubled space between lines only to separate sections of your
    functions.
    You used the space bar more frequently.

    Why does the definition of testcase contain initialization?

    Why is a defined as a pointer and not as an array?

    In the 'm' for loop, why do you re-evaluate c each iteration when its
    value never changes?

    What is the output supposed to represent?

    If you google for recursion in C, you should get several hits that
    show how to convert an iterative solution to a recursive one. If you
    don't find an example for pow, look at the one for factorial.

     
    Barry Schwarz, Feb 7, 2014
    #3
  4. mary8shtr

    woodbrian77 Guest

    I'm with you for the first two, but I've seen
    some who I think are good programmers not use
    many spaces within a line of code, and I write
    it that way sometimes also:

    http://webEbenezer.net/misc/direct.cc
    Watch out for Google.

    Duckduckgo cares about your privacy:

    https://Duckduckgo.com


    http://donttrack.us


    Brian
    Ebenezer Enterprises
    http://webEbenezer.net
     
    woodbrian77, Feb 8, 2014
    #4
  5. mary8shtr

    Stefan Ram Guest

    Yes, otherwise the real error (not checking the stream
    state after reading) might remain hidden even longer!
     
    Stefan Ram, Feb 8, 2014
    #5
  6. mary8shtr

    David Brown Guest

    You should never unnecessarily initialise a variable, because that stops
    the compiler checking your code for you.

    If you leave "int testcase;" uninitialised, and later use it without
    first setting its value, then the compiler's warnings will tell you.
    But if you've used "int testcase = 0;", then the compiler can't help you
    spot that error.
     
    David Brown, Feb 8, 2014
    #6
  7. Reliance on compiler warnings can only be done in conjunction with other
    ways of ensuring correctness of the code. Code reviews and following
    coding standards are other methods. However, in the end there is no
    single action that would completely protect of making a mistake.
    Besides, warnings are non-normative and therefore are not consistent
    from compiler to compiler or even from one version of the same compiler
    to another.

    That said, a random value is worse than a known one. It can lead to
    unpredictable behavior of the program. Repeatability, even if the
    actions of the program are incorrect, helps tremendously in fixing the bugs.

    V
     
    Victor Bazarov, Feb 8, 2014
    #7
  8. mary8shtr

    Stefan Ram Guest

    n3290 does not call it »random«:

    »if no initialization is performed, an object with automatic
    or dynamic storage duration has indeterminate value.«
     
    Stefan Ram, Feb 8, 2014
    #8
  9. mary8shtr

    Öö Tiib Guest

    Victor's point still holds since random is just (more useful) subset
    of indeterminate. So if random is worse than known then indeterminate
    is even worse.
     
    Öö Tiib, Feb 8, 2014
    #9
  10. mary8shtr

    Stefan Ram Guest

    When someone says: »x e A« (»x is an element of A.«), and
    the specs say: »x e B«, and »A c B« (»A is a subset of B«),
    then »x e A« does /not/ follow from the specs (from »x e B«).
    (It would follow if instead »B c A« would be true.)

    Hey, we're on topic again, as the subject says: »subset«!
     
    Stefan Ram, Feb 8, 2014
    #10
  11. mary8shtr

    David Brown Guest

    That's all true - compiler warnings are an aid to avoiding bugs, but
    they certainly don't cover everything. However, appropriate use of
    compiler warnings should be a normal part of your development
    methodology. If you are using a compiler that does not have a
    reasonable level of warnings, then it is a good idea to use an external
    tool (a "lint" tool), or simply pass the same code though a compiler
    that /is/ good at static error checking. Since gcc is a suitable
    choice, and is free, there is no good excuse for not using it as a
    minimum (llvm is an alternative).

    The worst thing you can do is say that since your compiler is not
    perfect at static error checking, you should ignore it!
    In a case like this, the initial value is only ever used if there is a
    bug in later code. Giving the variable an initial value hides the bug
    from helpful tools, and it certainly does not remove the bug. It is a
    far stretch to claim that it makes the bug less bad (by having a known
    value rather than an indeterminate one) - the bug is there, and the
    program will not work.
     
    David Brown, Feb 8, 2014
    #11
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.