user5768645
user5768645

Reputation:

How do I create a permutation of a 2D string array?

I have the following code. I set each box equal to 1. Now I want to set 3 boxes at a time to 0. How do I do that without manually setting each of them to 1?

Is there a permutation formula that will set 3 at the time to 1?

#include <iostream>
using namespace std;

int main()
{
  int array[2][2];
  for (int x = 0; x<2; x++) 
  {
        for (int y = 0; y<2; y++)
            array[x][y] = 1;
  }

 // display all cells to see that all of them are set to zero 
 cout << "diplaying" << endl;   
 for (int x = 0; x<2; x++)   
 {
    for (int y = 0; y<2; y++)
       cout << array[x][y] << " " ; 
    cout << endl;
 }

Printing this would look something like.

1   1

1   1

Now how do I get to print

0   1          

0   0

and

1   0

0   0

and

0   0

1   0

and

0   0

0   1

without having to set them individually that way?

Upvotes: 0

Views: 213

Answers (3)

cozyconemotel
cozyconemotel

Reputation: 1161

After reading carefully, I interpreted your question this way: You want to go from this

1   1

1   1

to this

0   1          

0   0

using the for clause.

if you simply want to leave a cell unchanged.. you could just save it and restore it after filling the array with 0.

memo = array[0][1];

for (int x = 0; x<2; x++) {
    for (int y = 0; y<2; y++) {
        array[x][y] = 0;
    }
}
array[0][1] = memo;

If that's what you want to do.

Where's the "string array" by the way..?

Upvotes: 0

NoseKnowsAll
NoseKnowsAll

Reputation: 4624

Personally, I would store the array as a 1D std::vector<int> of size n*n. Then you could call std::next_permutation() on it very simply. (It's worth noting that you don't have to use a std::vector; as long as it is contiguous in memory, you should be able to use std::next_permutation() properly)

The only thing you have to do that makes your permutation logic "2D" is the act of printing it out. However, your loop as-is should handle that properly, so no problems there either.

EDIT: Upon re-reading your code, you could not use this as-is. Instead, you should initialize your 1D std::vector to be 0 everywhere, except 1 at position 0. THEN, permutations of that would yield the output you want.

Furthermore, your printing loop would not print out the array properly. You probably want:

for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
        std::cout << vector[i*2+j] << " " ;
    }
    std::cout << std::endl;
}

Upvotes: 5

ForeverStudent
ForeverStudent

Reputation: 2537

I would write a function that prints all zeros except where it needs to print the actual value, then call this function with different indexes for desired effect.

#include <iostream>
using namespace std;
#define SZ 2
void printValue(int a [SZ][SZ], int x, int y)
{
    for(int i=0; i<SZ; ++i )
    {
        for(int j=0; j<SZ; ++j)
        {
            if(i==x && j==y) cout<<a[i][j];
            else cout<<"0";
            cout<<" ";
        }
        cout<<endl;
    }
}

Now you can make use of this function in a for loop

int main()
{
  int arr[SZ][SZ];
  for (int x = 0; x<SZ; x++)
  {
        for (int y = 0; y<SZ; y++)
            arr[x][y] = 1;
  }

    // display all cells to see that all of them are set to zero
 cout << "diplaying" << endl;

 for (int x = 0; x<SZ; x++)
 {
    for (int y = 0; y<SZ; y++)
       cout << arr[x][y] << " " ;
    cout << endl;
 }

 //now display all combos:
 for(int i=0; i<SZ; ++i)
 {
     for(int j=0; j<SZ; ++j)
     {
         printValue(arr, i,j);    
     }
  cout<<"\n\n";
 }


}

Upvotes: -1

Related Questions