How to construct a repeating array - arrays

How can I build an array given the requirements below?
for an array NxM of A(i,j):
for A(1,1), A(1,2), A(1,3) = 1 and A(1,4), A(1,5), A(1,6) = 0, repeat these 6 characters for A(1,M-5), A(1,M-4), A(1,M-3) = 1 and A(1,M-2), A(1,M-1), A(1,M) = 0.
for A(2,1), A(2,2) = 1 and A(2,3), A(2,4), A(2,5), A(2,6) = 0, repeat these 6 characters for A(2,M-5), A(2,M-4) = 1 and A(2,M-3) A(2,M-2), A(2,M-1), A(2,M) = 0.
for A(3,1) = 1 and A(3,2), A(3,3), A(3,4), A(3,5), A(3,6) = 0, repeat these 6 characters for A(3,M-5) = 1 and A(2,M-4), A(3,M-3), A(3,M-2), A(3,M-1), A(3,M) = 0
Repeat the above 3 steps for N rows
i.e for a 12x12 array
A = [1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0]

Related

find all 256 cases of 2 numbers in array of 8 length [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I find all combination of 2 numbers {0,1} in array of 8 length in c,
example
arr[]={0,0,0,0,0,0,0,0}
arr[]={0,0,0,0,0,0,0,1}
arr[]={0,0,0,1,1,0,0,1}
an so on
You can generate all combinations fairly easily using a recursive procedure:
arr = [0,0,0,0,0,0,0,0]
Generate(position)
if position > 8 then
print arr
else
arr[position] = 0
Generate(position+1)
arr[position] = 1
Generate(position+1)
Generate(1)
This will go down 8 levels in the call stack and then print the array [0, 0, 0, 0, 0, 0, 0, 0]. Then it will return to the 7th level, and go down again, printing [0, 0, 0, 0, 0, 0, 0, 1]. It will repeat this process, toggling each of the higher-order bits in turn until all 256 possibilities are generated. Instead of printing the arrays, you could save the arrays as you go.
Another possibility is to just create the 256 8-bit arrays and use an iterative procedure to toggle the elements in such a way as to guarantee you cover all your bases. An example with 4-bit strings:
0 0 0 0 0 0 0 0
0 0 0 0 => toggle bits in 4th position => 0 0 0 1
0 0 0 0 in blocks of size 1 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 1 => toggle bits in 3rd position => 0 0 0 1
0 0 0 0 in blocks of size 2 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 1 0 0 1 1
0 0 0 0 0 0 0 0
0 0 0 1 => toggle bits in 2nd position => 0 0 0 1
0 0 1 0 in blocks of size 4 0 0 1 0
0 0 1 1 0 0 1 1
0 0 0 0 0 1 0 0
0 0 0 1 0 1 0 1
0 0 1 0 0 1 1 0
0 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 0
0 0 1 1 0 0 1 1
0 0 0 0 0 1 0 0
0 0 0 1 0 1 0 1
0 0 1 0 0 1 1 0
0 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
0 0 0 1 => toggle bits in 1st position => 0 0 0 1
0 0 1 0 in blocks of size 8 0 0 1 0
0 0 1 1 0 0 1 1
0 1 0 0 0 1 0 0
0 1 0 1 0 1 0 1
0 1 1 0 0 1 1 0
0 1 1 1 0 1 1 1
0 0 0 0 1 0 0 0
0 0 0 1 1 0 0 1
0 0 1 0 1 0 1 0
0 0 1 1 1 0 1 1
0 1 0 0 1 1 0 0
0 1 0 1 1 1 0 1
0 1 1 0 1 1 1 0
0 1 1 1 1 1 1 1

What is wrong with my sudoku generate code in c?

That's my homework, making a sudoku game. I have done my algorithm but it's entering infinite loop. I didn't understand why.
I am trying create a random number and control it for find true number. Checking all columns and rows for find same number as like as our random number if it is, it's changing test number and if test has changed trying find another number for true number. Simple sudoku logic.
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int num, col, row, row2, col2, test = 0;
int sudo[9][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
do {
test = 0;
num = rand() % 9 + 1;
//control
for (col2 = 0; col2 <= 8; col2++) {
if (num == sudo[col2][row]) {
test++;
}
}
for (row2 = 0; row2 <= 8; row2++) {
if (num == sudo[col][row2]) {
test++;
}
}
} while (test > 0);
sudo[col][row] = num;
}
}
//print
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
printf(" %d ", sudo[col][row]);
if (col == 2 || col == 5) {
printf(" | ");
}
}
if (row == 2 || row == 5) {
printf("\n---------------------------------");
}
printf("\n");
}
}
Your algorithm is broken, and I can demonstrate why. If it were possible to fill in a sudoku puzzle this way, it would also be trivial to solve a sudoku puzzle this way, which it is not.
Essentially your code boils down to the following. I've added early exits on the inner for-loops to stop searching once we find the number already in the current row or column (and actually made sense of what 99.9% of the world thinks of concerning "rows" and "columns" in a NxN matrix):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NSIZE 9
void print_matrix(int const ar[][NSIZE])
{
for (size_t i=0; i<NSIZE; ++i)
{
for (size_t j=0; j<NSIZE; ++j)
{
fputc('0' + ar[i][j], stdout);
fputc(' ', stdout);
}
fputc('\n', stdout);
}
}
int main()
{
srand((unsigned)time(NULL));
int sudo[NSIZE][NSIZE] = {{0}};
int row, col;
for(row=0;row<NSIZE;++row)
{
for(col=0;col<NSIZE;++col)
{
int row2 = 0, col2 = 0, num;
printf("Trying ");
do
{
num = rand()%9+1;
printf("%d ", num);
for(row2=0; row2<NSIZE && num!=sudo[row2][col]; ++row2);
for(col2=0; col2<NSIZE && num!=sudo[row][col2]; ++col2);
}
while (row2 < NSIZE || col2 < NSIZE);
fputc('\n', stdout);
sudo[row][col] = num;
printf("sudo[%d][%d] = %d\n", row, col, num);
print_matrix(sudo);
}
}
}
As the loops progress, we report what number we're trying, and what the matrix looks like upon placement of a keeper. For example, a test run of the above initially can look like this:
Trying 8
sudo[0][0] = 8
8 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 1
sudo[0][1] = 1
8 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 9
sudo[0][2] = 9
8 1 9 0 0 0 0 0 0
0 0 0 0 0 0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 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
and this continues for perhaps a while. But eventually, unless you get extraordinarily lucky, the following is bound to happen (and this one went pretty deep before the wheels fell off):
Trying 1 6 3 4
sudo[6][6] = 4
8 1 9 6 3 4 7 5 2
1 3 5 4 8 6 2 7 9
3 6 4 8 7 9 5 2 1
7 9 1 2 4 5 3 8 6
4 7 3 9 2 8 6 1 5
5 4 2 3 6 1 8 9 7
6 8 7 1 9 3 4 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Note we're about to try and populate sudo[6][7]. To do that we must find a number that is not in the sudo[r][7] column already, nor the sudo[6][c] row. But looking at the numbers already in those positions.
sudo[r][7] : {5,7,2,8,1,9}
sudo[6][c] : {6,8,7,1,9,3,4}
Therefore we're looking for a number from 1..9 that is NOT in: {1,2,3,4,5,6,7,8,9}, which we're NEVER going to find.
The algorithm is broken. There is a reason backtracking is used for tasks like this.

Making two matrix from one

I have a matrix that I would like to split into two separate matrices based on a set of conditions.
The input matrix can be generated with the following code:
lbits = 8;
ntags = 10;
k = randi(lbits,1,ntags);
Tag = zeros(lbits,ntags);
Tag(lbits*(find(k)-1) + k)=1;
TagAnswer = Tag';
Which returns:
TagAnswer =
0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
My conditions are:
If place of bit '1' is on position lbits/2 or higher, add the row to matrix A
If place of bit '1' is less then position lbits/2, add the row to matrix B
With the above TagAnswer I want the 2nd, 5th and 7th rows to be moved into B and the remaining rows moved into matrix A
Assuming my edit is correct, you can use the row and column outputs of find to index TagAnswer and pull the rows based on your conditions:
% Generate sample data
lbits = 8;
ntags = 10;
k = randi(lbits,1,ntags);
Tag = zeros(lbits,ntags);
Tag(lbits*(find(k)-1) + k)= 1;
TagAnswer = Tag';
% Find bit locations and distribute rows accordingly
[r, c] = find(TagAnswer);
A = TagAnswer(r(c>=(lbits/2)), :);
B = TagAnswer(r(c<(lbits/2)), :);
For my test case I have:
TagAnswer =
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0
A =
0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
B =
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0
Edit: Because MATLAB stores data column-major, find also works column-major and will likely lose the row ordering. If it important to preserve the row ordering of TagAnswer in A and B you can use sort after the find call:
[r, sortidx] = sort(r);
c = c(sortidx);

How to detect connected components in a 2D array?

As mentioned the title above. I want to find out whether there are how many components in a 2D Array. Whereas, components are made by 1 numbers and there are only 0 and 1 number in the array.
I implemented this problem by using DFS (Deep First Search) algorithm with recursive calls and an array to mark cell visited.
However, I want to implement this problem with another way without using recursion, stack, queue, struct... Only using for/while function are allowed.
Example:
Array data:
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
0 0 1 1 1 0 1 1 0 1 0 0 0 0 0 1
0 0 1 0 1 0 1 1 0 1 0 1 1 1 0 1
0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1
0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1
0 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1
0 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1
0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1
0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0
0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0
0 1 0 1 1 1 1 1 0 0 1 1 1 1 0 0
0 1 0 1 0 1 0 1 0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
Array after determined components with specific labels.
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
0 0 2 2 2 0 3 3 0 1 0 0 0 0 0 1
0 0 2 0 2 0 3 3 0 1 0 4 4 4 0 1
0 0 2 0 2 0 0 0 0 1 0 4 0 4 0 1
0 0 2 0 2 0 0 0 0 1 0 4 0 4 0 1
0 0 2 2 2 0 0 0 0 1 0 4 0 4 0 1
0 0 0 0 0 5 5 5 0 1 0 4 4 4 0 1
0 0 0 0 0 5 0 5 0 1 0 0 0 0 0 1
0 0 0 0 0 5 5 5 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 6 6 6 6 6 0 0 0 0 7 7 7 7 0 0
0 6 0 0 0 6 0 0 0 0 7 0 0 7 0 0
0 6 0 6 6 6 6 6 0 0 7 7 7 7 0 0
0 6 0 6 0 6 0 6 0 0 7 7 7 7 0 0
0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0
0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0
Thank you in advance.
I guess you could iterate through the matrix, and check the neighbours for each cell, and copy the value of the neighbour if that is > 0 or set a new value if all the neighbours are 0. In pseudocode:
comp = 1
for i = 0 to n:
for j = 0 to n:
for nei : neighbours(i, j):
if nei > 0:
m[i,j] = nei
break
m[i,j] = comp
comp++
And neighbours are the 4 (or 2) adjacent neighbouring cells to (i, j)

matlab using cell2mat and reshape data to other form

I have cell array
Columns 1 through 6
[8x8 uint8] [8x8 uint8] [8x8 uint8] [8x8 uint8] [8x8 uint8] [8x8 uint8]
Columns 7 through 8
[8x8 uint8] [8x8 uint8]
if I use cell2mat function, I get this
Columns 1 through 18
0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0
0 1 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0
0 1 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1
0 1 1 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1
0 1 1 1 1 0 0 1 1 0 0 0 1 1 0 1 0 0
0 1 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0
0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1
0 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 1
Columns 19 through 36
1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0
1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0
0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0
1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 1 1 1
1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0
0 1 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 0
0 0 0 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1
Now I want matrix with 8 columns.
What I want is this
0 1 0 1 0 0 1 0
0 1 1 1 0 0 1 1
0 1 1 1 0 1 0 1
0 1 1 0 0 1 0 0
0 1 1 1 1 0 0 1
0 1 1 0 1 1 1 1
0 1 1 0 1 1 0 1
0 1 1 0 0 1 0 0
0 0 1 0 0 0 0 1
1 1 0 0 0 1 0 1
1 0 1 1 0 1 0 1
1 0 1 0 0 0 0 0
1 0 0 0 1 1 0 1
1 0 1 1 0 0 0 1
1 1 1 1 0 1 0 1
1 1 0 0 1 1 0 1
.
.
.
.
.
If I got your question correctly, you simply need to transpose the cell array before transforming it. See the following example (I edited the actual output to compress the display a bit):
> a
a =
{
[1,1] =
1 0 0
0 1 0
0 0 1
[1,2] =
2 0 0
0 2 0
0 0 2
[1,3] =
3 0 0
0 3 0
0 0 3
}
> cell2mat(a)
ans =
1 0 0 2 0 0 3 0 0
0 1 0 0 2 0 0 3 0
0 0 1 0 0 2 0 0 3
> cell2mat(a')
ans =
1 0 0
0 1 0
0 0 1
2 0 0
0 2 0
0 0 2
3 0 0
0 3 0
0 0 3
Note that using reshape brings another ordering:
> reshape(cell2mat(a), 9,3)
ans =
1 2 3
0 0 0
0 0 0
0 0 0
1 2 3
0 0 0
0 0 0
0 0 0
1 2 3
Just transposing your cell array and then passing it to cell2mat will probably be enough.
Another (less preferred, loops are generally not welcome in MATLAB) solution is to loop over your cell array and use matrix concatenation. If your cell array has name ca, this will do the thing:
imat = []; for i = 1:numel(ca); imat = [imat; ca{i}]; end
The answer will be in imat.
You can use the reshape function.
tmp = cell2mat(...);
res = reshape(tmp, numel(tmp)/8, 8);

Resources