3

I'm trying to generate the powerset of a set and I wrote this code. The problem is, when user enter two similar member of the set it dosen't work properly. What can I do? Here is my code:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

char obtain(char *p,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        cout<<"enter member"<<(i+1)<<"\n";
        cin>>*(p+i);
    }
    return *p;
}

void set_display(char *p,int n)
{
    cout<<"{";
    for(int i=0;i<n;i++)
    {
        cout<<*(p+i)<<",";
    }
    cout<<"}";
}

void powset(char *p,int n)
{
    unsigned int m = (double)pow((double)2, n);
    int i, j;
    for(i = 0; i < m; i++)
    {
        cout<<"{";
        for(j = 0; j < n; j++)
        {
            if(i& (1<<j))
                cout<<*(p+j);
        }
        cout<<"}\n";
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    I am unsure of what this has to do with C#, so i removed the C# tag. – Freeman Feb 22 '13 at 16:23
  • Why the C language tag when you are using `cout` which is not defined in C or C#? – Thomas Matthews Feb 22 '13 at 16:24
  • 2
    When you used the debugger, which line was the error at? – Thomas Matthews Feb 22 '13 at 16:24
  • it dosent have any errors but when i compile the code to examine it i enter one member twice and it didin't work correctly! – elahe razavi Feb 22 '13 at 16:29
  • C++ have a very nice set of [algorithms](http://en.cppreference.com/w/cpp/algorithm) in the standard library, e.g. [`std::transform`](http://en.cppreference.com/w/cpp/algorithm/transform) which can be used to "transform" all members of a collection. – Some programmer dude Feb 22 '13 at 16:30
  • 2
    Also, _never_ mix `printf` and `std::cout`. Use one or the other. – Some programmer dude Feb 22 '13 at 16:31
  • 2
    Please add input, expected output and actual output to your question. – zch Feb 22 '13 at 16:31
  • @JoachimPileborg I understand why mixing `printf` and `cout` is probably indicative of sloppy coding, but why *never*? `sync_with_stdio` exists for a reason right? And for instance, a quick search says [this](http://stackoverflow.com/questions/1924530/mixing-cout-and-printf-for-faster-output) disagrees. – BoBTFish Feb 22 '13 at 16:35
  • @BoBTFish The accepted answer in your linked question actually seem to suggest that if you skip using `std::endl` then `std::cout` is faster. – Some programmer dude Feb 22 '13 at 16:43
  • @JoachimPileborg Yes, but it does start by saying it is acceptable. I can't think of a reason you'ld want both to be honest (maybe you want to do something that is nicer with format strings, but don't want to do it into a buffer with `snprintf`...), but yours seemed to be more of a moral objection, the stress on *never* implying not just "I can't think of a good reason", but "There never can be a good reason". Maybe I just read too much into it. – BoBTFish Feb 22 '13 at 16:48
  • is there anybody that can help me? i edited again but it doesn't work – elahe razavi Feb 22 '13 at 16:52

1 Answers1

0

Ok I could not debug your code where you are missing. but since you posted your question I written an code in pure C. May be you find it helpful.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(int argc, char* argv[]){
    int N;
    char y[20]={0};
    int i,j;
    y[0] = 'a';
    y[1] = 'b';
    y[2] = 'c'; 
    N = 3;
    int powerSet;
    powerSet=pow(2,N);
    for(i = 0; i<(powerSet);i++){
        for(j=0;j<N;j++){
            if((i & (1<<j))){
                printf("%c ",y[j]);
            }
        }
        printf("\n");
    }           
    printf("\n");
    return EXIT_SUCCESS;
}

And its working:

:~$ gcc x.c -lm -Wall
:~$ ./a.out 

a 
b 
a b 
c 
a c 
b c 
a b c 

[ANSWER]

You error case: When two symbols are same.

y[0] = 'a';
y[1] = 'a';
y[2] = 'c'; 

:~$ ./a.out 

a 
a 
a a 
c 
a c 
a c 
a a c 

But this is wrong according to set theory. Because in Set we can't have same element twice (more then onec). BUT ALSO INPUT IS WRONG: (a, a, c) is not a set. So code is usable.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208