Problems with writing powerset code
Asked Answered
A

1

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";
    }
}
Abut answered 22/2, 2013 at 16:22 Comment(11)
I am unsure of what this has to do with C#, so i removed the C# tag.Diametral
Why the C language tag when you are using cout which is not defined in C or C#?Inch
When you used the debugger, which line was the error at?Inch
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!Abut
C++ have a very nice set of algorithms in the standard library, e.g. std::transform which can be used to "transform" all members of a collection.Gresham
Also, never mix printf and std::cout. Use one or the other.Gresham
Please add input, expected output and actual output to your question.Stanza
@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 disagrees.Blubberhead
@Blubberhead The accepted answer in your linked question actually seem to suggest that if you skip using std::endl then std::cout is faster.Gresham
@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.Blubberhead
is there anybody that can help me? i edited again but it doesn't workAbut
S
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.

Supermarket answered 22/2, 2013 at 16:51 Comment(2)
I written in C because you included not of header file with .h extention .Supermarket
thanks alot so my code works too and it dosent have any errorsAbut

© 2022 - 2024 — McMap. All rights reserved.