0

I am making the power set of string and storing in a 2D array and now I want to store the whole power set element into a single 1D array? I am coding on CodeBlocks.

String s1="abba" and arr[][] is also of string class.

for(int i=0;i<(1<<s1.length());i++)
{
    for(int j=0;j<s1.length();j++)
    {
        if((i&(1<<j))>0)
            {   if(k<1<<s1.length())
                 arr[i][j]=s1[j];

            }
    }

}
for(int i=0;i<(1<<s1.length());i++)
{
    for(int j=0;j<s1.length();j++)
    {
        if((i&(1<<j))>0)
            {   if(k<1<<s1.length())
                 cout<<arr[i][j];

            }
    }

}

I want to store these element in a 1D array to process it.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    Suggestion: Don't store as a 2D array at all. Make a 1D array that looks like a 2D array when you want a 2D array and is a 1D array every other time. [Something like this](https://stackoverflow.com/a/2076668/4581301) with extra overloads for 1D access. – user4581301 Sep 07 '19 at 06:00
  • 2
    Please take the [tour] and read [ask]. Point is, you are talking about arrays, but your code doesn't show any. Also, check the description of the tags that you apply. If you have specific problems with code, extract a [mcve] and post that inline in your question. If you have an algorithmic problem, then describe that, but that would also make it language-agnostic. – Ulrich Eckhardt Sep 07 '19 at 06:13
  • c and c++ are two different languages. Most of the time using both tags or even write c/c++ is just wrong. – t.niese Sep 07 '19 at 06:35
  • See [Treating a 1D data structure as 2D grid](https://softwareengineering.stackexchange.com/questions/212808/treating-a-1d-data-structure-as-2d-grid/212813#212813) on Softwareengineering.SE – Doc Brown Sep 07 '19 at 06:36
  • `now I want to store the whole power set element into a single 1D array?` now we know what you want to do. But what is you problem with doing that? What is your question about that? – t.niese Sep 07 '19 at 06:37
  • 1
    Really a bit impossible to be sure of an answer until we see just how `arr` (and the rest of the objects) are declared. [MCVE](http://stackoverflow.com/help/mcve) needed. – David C. Rankin Sep 07 '19 at 08:03

0 Answers0