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

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?

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;
}

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..?

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";
}
}

Related

How to insert element of a 2D Vector into another 2D vector in C++?

I have a 2D Vector matrix whose elements I want to shift into another 2D Vector result . The only thing is that the positions of the elements are changed in result by the logic shown in the code. The program that I have written gets executed but result displays all elements as 0 which is incorrect. How to fix this?
CODE
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<int>> matrix;
int n;
matrix = {{1,2},{3,4}};
n = matrix.size();
//Loop to print `matrix`
cout << "'matrix' 2D Vector" << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << "\n";
}
vector<vector<int>> result(n, vector<int> (n));
//Loop to shift elements from `matrix` to `result`
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
result[j,n-i+1] = matrix[i][j];
}
}
//Loop to print `result`
cout << "'result' 2D Vector" << endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << result[i][j] << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT
'matrix' 2D Vector
1 2
3 4
'result' 2D Vector
0 0
0 0
EXPECTED OUTPUT
'matrix' 2D Vector
1 2
3 4
'result' 2D Vector
3 1
4 2
First only include <iostream> and <vector> header files instead of <bits/stdc++.h> and your code throws out_of_range exception because of the statement result[j][n-i+1] = matrix[i][j];, change it result[j,n-i-1] = matrix[i][j];

How to print diamond shape w/ c++

I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:
*
***
*****
but I decided to challenge myself and see if I could create a simple diamond shape like this:
*
***
*****
***
*
Here is my code so far.
I should also add that the value you input, for example 5, determines how big the diamond is.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value; i++) {
int number = 0;
number+= 2;
//print spaces v v v
for (int x = 0; x < (value - value + i + 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.
I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.
I also apologize if my code is messy and hard to read.
int number = 0; and number+= 2;
value - value inside for (int x = 0; x < (value - value + i + 1); x++) {
are not required.
Inside the parenthesis, you can use
2*(value-i-1)-1
However, I would suggest you to first analyze the problem and then try to solve it instead of trying random things. For instance, let's consider the cases of even and odd inputs i.e., 2 and 3.
Even Case (2)
*
***
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 1 1
1 0 3
2 0 3
3 1 1
For row index < value
Number of Spaces = value - row index - 1
Number of Stars = 2 * row index + 1
For row index >=value
The number of spaces and stars are simply reversed. In the odd cases, the situation is similar too with a small exception.
Odd Case (3)
*
***
*****
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
The small exception is that while reversing, we have to ignore the row index = value.
Now, if we put the above analysis in code we get the solution
//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
//print spaces v v v
for (int x = 0; x < value - rowIndex -1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < 2 * rowIndex + 1; y++) {
cout << "*";
}
cout << endl;
}
And then inside main
//Row index < value
for (int i = 0; i < value; i++) {
PrintDiamond(i,value);
}
//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
PrintDiamond(i,value);
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value-1; i++) {
// int number = 0;
// number+= 2;
// //print spaces v v v
for (int x = 0; x < i+1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2*(value-1-i)-1); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
I hope that you will get this .Also in the second for loop you were iterating it one extra time by iterating the loop upto value. But since the pyramid is symmetric so the no of rows in the pyramid will be 2*value-1.So I in the second loop i should vary upto value -1.
This code should resolve the problem:
#include <sstream>
using namespace std;
void printSpaces(int howMany) {
for(int i = 0; i < howMany; i++) cout << " ";
}
void figure(int size) {
bool oddSize = size % 2 == 1;
int center = size / 2;
int spaces = size / 2;
// If figure is of an odd size adjust center
if (oddSize) {
center++;
} else { // Else if figure is of even size adjust spaces
spaces--;
}
for (int i = 1; i <= center; i++) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++) cout << "*";
cout << endl;
spaces--;
}
spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
for(int i = center; i >= 1; i--) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++)
cout << "*";
cout << endl;
spaces++;
}
}
int main() {
int value = 0;
while(value < 3) {
cout << "Please enter in a value (>= 3): ";
cin >> value;
cout << endl;
}
figure(value);
return 0;
}

change x,y of board by user input

i have a board of 10x10 and here his code:
for(int x = 0; x < 10; x++) // X
{
cout << 0;
for(int y = 0; y < 10; y++) // Y
{
cout << " " << 0;
}
cout << endl;
}
now I want to change 0 to 1 in the x,y location by user input.
how can I do that?
here is simply what I want to do ( in pictures ):
the user input is x = 2, y = 2 and the table changing from Table 1 Example to Table 2 Example ( as a new table ):
TABLE 1 Example | TABLE 2 Example
its just a curiosity question that I've been trying to make.
Get x0 and y0 before the loop using cin>> x0 and cin>> y0.
Inside the loop
if(x == x0 && y==y0)
cout << " " << 1;
else
cout << " " << 0;
The best way would be to keep all values in a two dimensional array. Then, play around with the
values of that array. After than, write the whole array once to the screen.
See the example below :
int matrix[10][10];
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++) matrix[i][j]=0;
matrix[2][3]=1;
matrix[3][4]=1;
matrix[4][5]=1;
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
cout<<matrix[i][j];
}
cout << endl;
}
you can print again the whole matrix and check the x,y pair before writing the 0 or the 1
for(int x = 0; x < 10; x++) // X
{
cout << 0;
for(int y = 0; y < 10; y++) // Y
{
//here verify the x,y against the user input
if(x == xUser && y == yUser)
{
cout << " " << 1;
} else{
cout << " " << 0;
}
}
cout << endl;
}

how to set boxes in array with at least 1 neighbor equal to 1

I'm attempting to go through an array and figure out which ones have at least 1 immediate neighbor..
the answer I'm getting is
display
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
display
11111
10001
11111
11111
11111
box :1 :1
box :1 :3
which is perfect. I am able to see from the output which ones have exactly 1 neighbor array[1][1] and array[1][3].
Now how do I set those 2 only (array[1][1] and array[1][3]) = 1?
Since you use the nested 'display' loop multiple times, it'd be ideal to factorise that into its own function:
void Draw(int array[5][5]) {
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 5; ++y)
std::cout << array[x][y] << ' ';
std::cout << '\n';
}
}
Likewise, your 'finding neighbours' checks can be shortened drastically with an additional nested loop.
This will also make solving your problem much easier:
#include <iostream>
using namespace std;
void Draw(int array[5][5]) {
cout << endl;
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
cout << array[x][y] << ' ';
cout << '\n';
}
cout << endl;
}
int main() {
int num = 0;
cout << "How many neighbours?: "; //Asks user for the number of neighbours they'd like to check for.
cin >> num; //Saves the number of neighbours to look for in 'num'
int array[5][5];
int dummyarray[5][5]; //A spare array we'll use to keep track of which boxes have the specified number of neighbours.
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
array[x][y] = 1; //Here we fill our original array with 1's
}
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
dummyarray[x][y] = 1; //Now we fill our dummy array with 1's so it's the same as our original array
}
Draw(array); //Here we call the Draw function, which prints our array for us.
array[1][1] = 0;
array[1][2] = 0; //Set some values of our original array to 0
array[1][3] = 0;
for (unsigned x = 0; x < 5; ++x) { //Here's where the checking starts
for (unsigned y = 0; y < 5; ++y) {
if (array[x][y] == 0) { //If we find a 0, start checking for neighbours of that 0 that are also 0's
int count = -1; //Because this method counts the original box (array[x][y]) when searching for 0's, we anticipate this by removing one from the count, so that we get the correct number of neighbours
for (int vertical = -1; vertical < 2; ++vertical) { //This loop checks boxes above and below our original box
for (int horizontal = -1; horizontal < 2; ++horizontal) { //This loop checks boxes side to side of our original box
if (vertical + y >= 0 && vertical + y < 5 && horizontal + x >= 0 && horizontal + x < 5) { //If we're inside the array bounds (ie, if we're not trying to access array[-1][-2] or something similar which would undefined behaviour), continue
if (array[x + horizontal][y + vertical] == 0) //If the box we're checking is a 0, add 1 to the number of neighbours our original box has
++count;
}
}
}
cout << "(" << x << ", " << y << "): " << count << endl;
if (count == num) dummyarray[x][y] = 0; //If the box we just checked has the number of neighbours we're looking for, mark it in the dummy array.
}
}
}
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
array[x][y] = dummyarray[x][y]; //Here we copy all of dummyarray's data to our original array.
}
Draw(array); //Finally, we draw our array again with Draw()
return 0;
}
I'm really confused why you're trying to match the full neighborhood if all you need is "at least one neighbor is 1". Is there a reason you can't just do:
// going through the loop to figure out which ones have at least 1 neighbor
for (int x = 0; x <5 ; x++)
{
for (int y = 0; y <5 ; y++)
{
if ( (array[x][y] == 0 )
&&
(array [x-1][y] == 1 || array [x+1][y] == 1 ||
array [x][y-1] == 1 || array [x][y+1] ==1 ||
array [x+1][y+1]==1 || array [x-1][y-1]==1 ||
array [x-1][y+1]==1 || array [x+1][y-1] == 1)
)
{
cout << endl;
cout << "box :" << x << " :" << y << endl;
}
}
Oh and you should probably handle the cases at the borders.
int clamp04(int coord){return coord<0?0:(coord>4)?4:coord;}
and then use it like
// going through the loop to figure out which ones have at least 1 neighbor
for (int x = 0; x <5 ; x++)
{
for (int y = 0; y <5 ; y++)
{
if ( (array[x][y] == 0 )
&&
(array [clamp04(x-1)][y] == 1 || array [clamp04(x+1)][y] == 1 ||
array [x][clamp04(y-1)] == 1 || array [x][clamp04(y+1)] ==1 ||
array [clamp04(x+1)][clamp04(y+1)]==1 || array [clamp04(x-1)][clamp04(y-1)]==1 ||
array [clamp04(x-1)][clamp04(y+1)]==1 || array [clamp04(x+1)][clamp04(y-1)] == 1)
)
{
cout << endl;
cout << "box :" << x << " :" << y << endl;
}
}
Also, as a side-note, if you're then setting those values to 1 the operation you're executing is a morphological filter operation known as "dilation"

Outputted array values different from the values stored

I made an array and set the values from 1 to 9 in the initializeBoard function, but for some reason when I print the values they come out 0 to 8. Why is this happening? Shouldn't it print out 1 to 9 since those are the numbers I put in the array in initializeBoard?
int main()
{
initializeBoard();
ticTacToeBoard();
}
void initializeBoard()
{
for (int i = 1; i < 9; i++)
ticTacBoard[i] = i;
cout << endl;
}
void ticTacToeBoard ()
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
cout << ticTacBoard[3 * y + x] << " ";
cout << endl;
}
}
You have an off-by-one error. Arrays use zero-based indexing in C++. Your code does not assign a value to the zeroth element of the array.
Try this instead:
for (int i = 0; i < 9; i++)
{
ticTacBoard[i] = i + 1;
}
The loop:
for (int i = 1; i < 9; i++)
{
ticTacBoard[i] = i;
}
will only do 1-8 since it will stop when i++ increases it to 9, so you're not initializing all 9 elements.
You should realistically do the same loop like this:
for (int i = 0; i < 9; i++)
{
ticTacBoard[i] = (i + 1);
}
Problem lies in:
ticTacBoard[i] = i;
Should be:
ticTacBoard[i-1] = i;
Two suggestions:
Shouldn't you initialize ticTacBoard before accessing it with []? Make sure you give it enough memory for all of your slots! (I'm guessing you're doing tic tac toe, so you'll want 3x3 = 9 slots)
Indexing in C++ starts at 0. You want to do for (i=0; i<9; i++)
Hope this helps!
Your i starts at 0, so your first value will be 0. Since the inequality i < 9 breaks when i = 9, i is actually never 9 (the loop exits before your code is actually run).
Try using <= instead of just < to account for i = 9:
for (int i = 1; i <= 9; i++)
{
ticTacBoard[i - 1] = i;
}
Other than that, arrays are indexed starting from 0 in C++ (and virtually every other language), so the first element is array[0], second is array[1], etc.
You'll have to subtract 1 from your array index.