I've been thinking if there is any smarter solution if I want to check all eight neighbors of an arbitrary element in a binary number only 2D array in C
What I do is:
Psudo Code:
//return how many neighbor of an element at x,y equals 1.
int neighbor(int** array, int x, int y)
if x>WIDTH
error
if y>HIEGHT
error
if x==0
ignore west, nw, sw, and calculate the rest.....
etc..
this is pretty dull, is there any smarter solution?
I used a similar approach to get Adjacent Mines for a particular cell in the Minesweeper Game. What I did, is I used an Array like this (MAX_NUMBER_OF_CELLS = 8) :
int offset[MAX_NUMBER_OF_CELLS][2] = {
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1}
};
Considering we are talking about the CELL at location 0, 0 in a matrix. We will simply add these offset values, to the CELL to check, if the adjacent CELL is a valid CELL (i.e. it falls within the matrix). If it is VALID, then we will see if it contains 1, if yes than increment sum by 1 else not.
//rest of the values represent x and y that we are calculating
(-1, -1) (-1, 0) (-1, 1)
-------------------------
(0, -1) |(0, 0(This is i and j))| (0, 1)
-------------------------
(1, -1) (1, 0) (1, 1)
sum = 0;
for (k = 0; k < MAX_NUMBER_OF_CELLS; k++)
{
indexX = i + offset[k][0];
indexY = j + offset[k][1];
if (isValidCell(indexX, indexY, model)) // Here check if new CELL is VALID
// whether indexX >= 0 && indexX < rows
// and indexY >= 0 && indexY < columns
{
flag = 1;
if (arr[indexX][indexY] == 1))
sum += 1;
}
}
EDIT 1 :
Here is one working example (C is not my language, though still tried my hands on it to give you one idea :-)) :
#include <stdio.h>
#include <stdlib.h>
int findAdjacent(int [4][4], int, int, int, int);
int main(void)
{
int arr[4][4] = {
{0, 1, 0, 0},
{1, 0, 1, 1},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
int i = 2, j = 2;
int sum = findAdjacent(arr, i, j, 4, 4);
printf("Adjacent cells from (%d, %d) with value 1 : %d\n", i, j, sum);
return EXIT_SUCCESS;
}
int findAdjacent(int arr[4][4], int i, int j, int rows, int columns)
{
int sum = 0, k = 0;
int x = -1, y = -1; // Location of the new CELL, which
// we will find after adding offsets
// to the present value of i and j
int offset[8][2] = {
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1}
};
for (k = 0; k < 8; k++)
{
x = i + offset[k][0];
y = j + offset[k][1];
if (isValidCell(x, y, rows, columns))
{
if (arr[x][y] == 1)
sum += 1;
}
}
return sum;
}
int isValidCell(int x, int y, int rows, int columns)
{
if ((x >= 0 && x < rows) && (y >= 0 && y < columns))
return 1;
return 0;
}
One possible optimization, if you want to know the number of neighbors of cells a lot more than you want to change the cells, is to preprocess the number of neighbors for each cell and save the results in an another array.
int** actualArray;
// fill in with 0s and 1s
int** numberOfNeighbors;
// write a function to calculate the number of neighbors for cell x,y in actualArray and
// store the result in numberOfNeighbors[x][y]
preprocess(actualArray, numberOfNeighbors); // call this whenever actualArray changes
// now you can get the number of neighbors of a cell in constant time
// from numberOfNeighbors[x][y]
Are you trying to find the current position of the element in the array? If so, you can define a macro like:
#define OFFSET(x,y) ((GridWidth*y)+x)
Or, if you're trying to find which surrounding 'boxes' could contain an element (i.e which neighbors are 'in bounds')...
for k = 0 while k < GridWidth
for m = 0 while m < GridWidth
if k < GridWidth
toRight = true
if m < GridWidth
toDown = true
if k > 1
toLeft = true
if m > 1
toUp = true
From there, combine the directions to get diagonals - if toRight && toUp, then toUpRight=true etc
EDIT - I forgot to mention, this is if the grid is stored in a 1d array. For 2d, m would be for GridHeight
Related
Given 2 arrays of integers a[] and b[] with the same size of n (1 <= n <= 100) numbered from 1 to n.
(0 <= a[i], b[i] <= 6)
You can swap any a[i] with b[i].
What is the minimum number of swaps needed so that the difference of the sums of array a[] and b[] is minimum ?
Then print out:
The number of swaps
The swapped indexes
The difference of sums of both arrays
Example
n = 6
a[] = { 1, 1, 4, 4, 0, 6 }
b[] = { 6, 3, 1, 1, 6, 1 }
Result
- 2 (The number of swaps)
- 5, 6 (The swapped indexes)
- 0 (The difference of sums of the arrays)
Explanation
If you swap a[5] with b[5] and a[6] with b[6] which requires 2 swaps, arrays a[] and b[] will become:
a[] = {1, 1, 4, 4, 6, 1}
b[] = {6, 3, 1, 1, 0, 6}
Sum of a[] is 1 + 1 + 4 + 4 + 6 + 1 = 17
Sum of b[] is 6 + 3 + 1 + 1 + 0 + 6 = 17
So the difference of the two sums is 0.
Here's an iterative method that saves the differences so far and updates the smallest list of indexes needed to swap to achieve them.
JavaScript code:
function update(obj, d, arr){
if (!obj[d] || obj[d].length > arr.length)
obj[d] = arr;
}
function f(A, B){
let diffs = {0: []};
for (let i=0; i<A.length; i++){
const newDiffs = {};
for (d in diffs){
// Swap
let d1 = Number(d) + B[i] - A[i];
if (diffs.hasOwnProperty(d1) && diffs[d1].length < diffs[d].length + 1)
update(newDiffs, d1, diffs[d1]);
else
update(newDiffs, d1, diffs[d].concat(i+1));
d1 = Number(d) + A[i] - B[i];
if (diffs.hasOwnProperty(d1) && diffs[d1].length < diffs[d].length)
update(newDiffs, d1, diffs[d1]);
else
update(newDiffs, d1, diffs[d]);
}
diffs = newDiffs;
}
console.log(JSON.stringify(diffs) + '\n\n');
let best = Infinity;
let idxs;
for (let d in diffs){
const _d = Math.abs(Number(d));
if (_d < best){
best = _d;
idxs = diffs[d];
}
}
return [best, idxs];
};
var A = [1, 1, 4, 4, 0, 6];
var B = [6, 3, 1, 1, 6, 1];
console.log(JSON.stringify(f(A, B)));
Here's a C++ implementation of mine based on Javascript answer of גלעד ברקן.
Short Explanation:
We maintain a mapping of all differences and their minimum swaps seen so far and try to extend all of the differences seen so far based on new values to get new mapping of such kind. We have 2 choices at each step when considering ith items in A and B, either consider the items as it is or swap the ith items.
Code:
#include <iostream>
#include <climits>
#include <unordered_map>
#include <vector>
using namespace std; // Pardon me for this sin
void update_keeping_existing_minimum(unordered_map<int, vector<int> >& mp, int key, vector<int>& value){
if(mp.find(key) == mp.end() || mp[key].size() > value.size())mp[key] = value;
}
// Prints minimum swaps, indexes of swaps and minimum difference of sums
// Runtime is O(2^size_of_input) = 2^1 + 2^2 .. + 2^n = 2*2^n
// This is a bruteforce implementation.
// We try all possible cases, by expanding our array 1 index at time.
// For each previous difference,
// we use new index value and expand our possible difference outcomes.
// In worst case we may get 2 unique differences never seen before for every index.
void get_minimum_swaps(vector<int>& a, vector<int>& b){
int n = a.size();
unordered_map<int, vector<int> > prv_differences_mp;
prv_differences_mp[0] = {}; // initial state
for(int i = 0 ; i < n ; i++){
unordered_map<int, vector<int> > new_differences_mp;
for (auto& it: prv_differences_mp) {
// possibility 1, we swap and expand previous difference
int d = it.first;
int d1 = d + b[i] - a[i];
if(prv_differences_mp.find(d1) != prv_differences_mp.end() && prv_differences_mp[d1].size() < (prv_differences_mp[d].size() + 1)){
update_keeping_existing_minimum(new_differences_mp, d1, prv_differences_mp[d1]);
} else {
// only place we are modifying the prv map, lets make a copy so that changes don't affect other calculations
vector<int> temp = prv_differences_mp[d];
temp.push_back(i+1);
update_keeping_existing_minimum(new_differences_mp, d1, temp);
}
// possibility 2, we don't swap and expand previous difference
int d2 = d + a[i] - b[i];
if(prv_differences_mp.find(d2) != prv_differences_mp.end() && prv_differences_mp[d2].size() < prv_differences_mp[d].size()){
update_keeping_existing_minimum(new_differences_mp, d2, prv_differences_mp[d2]);
} else {
update_keeping_existing_minimum(new_differences_mp, d2, prv_differences_mp[d]);
}
}
cout<<i<<":index\n";
for(auto& it: prv_differences_mp){
cout<<it.first<<": [ ";
for(auto& item: it.second)cout<<item<<" ";
cout<<"] ; ";
}
cout<<"\n";
prv_differences_mp = new_differences_mp;
}
int best = INT_MAX;
vector<int> min_swap_ans;
for(auto& it: prv_differences_mp){
int _d = it.first >= 0 ? it.first: -it.first;
if(_d < best){
best = _d;
min_swap_ans = it.second;
}
}
cout<<"Number of swaps: "<<min_swap_ans.size()<<"\n";
cout<<"Swapped indexes:\n";
for(auto idx: min_swap_ans)cout<<idx<<" ";
cout<<"\nDifference: "<<best<<"\n";
}
int main(){
vector<int> A{ 1, 1, 4, 4, 0, 6 };
vector<int> B{ 6, 3, 1, 1, 6, 1 };
get_minimum_swaps(A, B);
return 0;
}
Let's suppose I have this array
int diml = 3;
int dims = 3;
int time [diml][dims] ={
(10, 3, 5),
( 4, 7, 2),
( 2, 8, 1)
};
How can I get every combination like:
(10, 3, 5)
(10, 3, 2)
(10, 3, 1)
(10, 7, 5)
(10, 7, 2)
(10, 7, 1)
...
(2, 8, 5)
(2, 8, 2)
(2, 8, 1)
*Is this possible without saving all the combinations in a new array, but just a 1D local array that can store the current combination on every cycle?
*I'd prefer cycles over recursion. And at the end of each cycle I need the pattern (like 10, 3, 2) so I can elaborate it.
*The dimensions of the 2D array are MxN (3x3 is just an example).
*A solution with binary trees is accepted (but I want to save the indexes too).
I should do this in C. I have found similar solutions in StackOverflow but they work by column and they save the data in a 2D array, but that's not what I need.
Thanks in advance! (:
For this example codes, The first one was built so it would be easier to understand example 2. Example 1 was built for only 3x3 matrixes. Example 2 was built so that it can accommodate a matrix with 8 columns at maximum. I didn't use malloc or return an array. It will print back all the possible combinations for you. It doesn't deal with returning the data but it wouldn't be hard to incorporate that into the code.
For the method of calculation all the possible combination, I would use a 3x3 matrix as an example.
In a 3x3 matrix, there are 3 rows and 3 columns. I treated each column as a set of number that I can pick and the rows as the possible of numbers that I can pick from. So in that example, I can pick 012 for my first, second, and third set of number.
So to get all the possible combinations, I have 3 arrays, [0] [1] [2]. They all start at 0. I first save the possible combination of 0 0 0. Then I increase array 2 by 1. Then I would get 0 0 1. I then save that combination. I will keep on doing that and one the [2] array == 2. I turn that to 0 and add a 1 to the array to the left of it. So it become 0 1 0. When I reach a loop where my values of my arrays are 0 2 2, the loop after that, I will get 1 0 0. I will keep on doing that until all the value turn to zero then I am done.
For how the data is store, I store them continually in an array. To read back the value properly. Say for example in a 2x5 matrix. Each combination will have 5 numbers. Thus, the first combination is the first five indexes, the next combination, is the next five numbers after that one.
To calculate how much array length you would need before calculating the combinations, you can just base the calculation on rows and columns. Think of this like the lottery. If there are 3 columns, it like you can pick 3 numbers. Each column have 3 rows, so that mean for each time you pick a number there are 3 possible numbers to pick from. For you to hit a jackpot the chances are 1:3 x 1:3 x 1:3 or 1:27 because there are 27 possibilities if picking 3 numbers like this (123 123 123) and matching them in the correct order. Thus, for a 3x3 matrix, it is 3x3x3, 4x4 = 4x4x4x4, 1x3 = 1, 3x1 = 3, 2x5 = 2x2x2x2x2 = 32.
Thus, the amount of possible combinations is "the amount of rows" to the power of "the amount of columns".
The size is the amount of possible combinations multiply by the amount of numbers per combination. Of which would be "possibilities multiply column count = array size needed.
Example 1:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int m1 = 3;
const int m2 = 3;
int matrix[m1][m2] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};
printMatrixCombo(m1, m2, matrix);
return 0;
}
// Only use this for a 3x3
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int output[81] = {0};
for (int group1 = 0; group1 < 3; group1++){
for (int group2 = 0; group2 < 3; group2++ ){
for (int group3 = 0; group3 < 3; group3++ ){
output[oi++] = matrix[group1][0];
output[oi++] = matrix[group2][1];
output[oi++] = matrix[group3][2];
}
}
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}
Example 2:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int row = 4;
const int col = 4;
/*// 3x3
int matrix[row][col] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};//*/
// 4 x 4
int matrix[row][col] = {
{10, 3, 5, 7},
{4, 7, 2, 3},
{2, 8, 1, 9},
{9, 4, 8, 11}
};//*/
/*// 5 x 5
int matrix[row][col] = {
{10, 3, 5, 7, 25},
{4, 7, 2, 87, 42},
{2, 8, 1, 85, 39},
{9, 4, 8, 94, 57},
{10, 3, 5, 7, 93},
};//*/
/*// 2 x 2
int matrix[row][col] = {
{10, 3},
{4, 7},
};//*/
/*// 1 x 1
int matrix[row][col] = {
{10},
};//*/
/* // 3 x 1
int matrix[row][col] = {
{10},
{4},
{1}
}; //*/
/*// 1 x 3
int matrix[row][col] = {
{10, 4, 1},
};// */
printMatrixCombo(row, col, matrix);
return 0;
}
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int allZ = 0;
// This is the maximum for a 5x5
// Change to fit usage case
int output[15625] = {0};
int colCount[8] = {0};
int lastCol = col - 1;
int lastRow = row - 1;
while (1){
for ( int i = 0; i < col; i++ )
output[oi++] = matrix[colCount[i]][i];
if ( colCount[lastCol] == lastRow ){
colCount[lastCol] = 0;
for (int i = lastCol - 1; i > -1; i--){
if ( colCount[i] == lastRow ){
colCount[i] = 0;
} else {
colCount[i]++;
break;
}
}
} else {
colCount[lastCol]++;
}
allZ = 1;
for ( int i = 0; i < col; i++ ){
if ( colCount[i] != 0 ){
allZ = 0;
break;
}
}
if (allZ == 1) break;
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
printf("Array's length(indexes) is %d\n", oi );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}
So given an array of:
input[3] = {0, 0, 0}
this outputs :
output[3] = {3, 0 ,0}
code:
void create_hist(double input[], int num_of_inputs, int output[])
{
int num_to_check = input[0];
int counter = 0;
for (int i = 0; i < num_of_inputs; i++)
{
int j = output[i];
if ((int)input[i] == num_to_check)
{
counter++; /* it was found */
}
output[j] = counter;
}
return;
}
but if I have a floating point array
input[5] = {0.0000, 1.0000, 2.0000, 3.0000, 4.000}
and I want to truncate the values to int, and count how many times each integer in the range 0 - 10 appears in the input array then output it to:
output[5] = {1, 1, 1, 1, 1}
output[0] = {1} //indicates how many times 0 appeared in the array
or
input[10] = {1.000, 4.000, 5.0000, 2.000, 4.000, 7.000, 9.000, 6.000, 0.000, 0.000}
and output
output[10] = {2, 1, 1, 0, 2, 1, 1, 1, 0, 1}
output[0] = {2} // this indicates how many times 0 appeared in the first array
Can anyone tell me how to do this?
You shouldn't use output[i] as an array index. It's a counter, not the value whose count you want. You should use (int)input[i] as the index.
You first need to initialize all elements of output to 0, then you increment the elements corresponding to the integer part of each input.
memset(output, 0, sizeof(output[0]) * MAX_INPUT_VALUE);
for (int i = 0; i < num_of_inputs; i++) {
output[(int)input[i]]++;
}
I was experimenting around with 2d array manipulation in c. in-place rotation of a 2d shape in particular, so i did my research and came up with :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define RAD(x) ((x) * 3.14 / 180)
char shape[3][3] = {{1, 1, 1},
{1, 0, 1},
{1, 1, 1}};
char nf[5][5]= {{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}};
char expa[5][5]= {{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{1, 0, 0, 0, 1},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0}};
void print(int row, int col, char shapein[][col]) {
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
printf("%c", ((shapein[i][j]) == 1) ? '#' : ' ');
}
printf("\n");
}
}
void main() {
int nw = 5;
int nh = 5;
int xf = nw / 2;
int yf = nh / 2;
float angle = RAD(90);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
int nx = ((i - xf) * cos(angle) - (j - yf) * sin(angle)) + xf;
int ny = ((i - xf) * sin(angle) + (j - yf) * cos(angle)) + yf;
nf[nx][ny] = shape[i][j];
}
}
print(5, 5, nf);
}
i was expecting to get output like this :
however what i get is :
i did what i understood from my research:
- rotations do happen at origin (assuming top left corner)
- moved coords so that it is in origin scale.
- used output array's dimension as space when rotating.
I am stumped, i need a little help.
while at it, as you can see from my code, i hard coded the new rotated dimensions,
however, it would be nice if somebody could guide me how to calculate new rotated dimensions dynamically,without using the "maximum of corners" method.
The expected result you show looks like shape rotated by 45º, but you set the angle to 90º.
You are working with integer coordinates but using floating-point functions. The sin and cos routines necessarily return approximations, and, when floating-point values are converted to integers, they are truncated. You may prefer rounding, perhaps using the round function.
xf and yf appear to be set to the center of the image using nw / 2 and nh / 2, but they are integers, so the result, 2 in each case, is a considerable distance from the actual center, 2.5.
There are likely additional errors, but you should fix those and continue working. Also, take steps to debug your code. Inside the loop, print each value of xf, yf, nx, and ny so you can see the inputs and outputs of the calculations. Check them manually to see if they are right or not. If they are not right, examine the individual calculations to see what they are doing.
This is the last part in a huge project. The question of my project is to code the program in C which can convert from decimal to binary. I did everything, however, I stuck with one part. After separating into two arrays - one array contains the positions of 1 and another one includes the positions of 0. For example with number 19. My arrays have:
array1 = {4,1,0} //which are pow(2,4),pow(2,1), and pow(1,0). Obviously, these numbers should be replaced with number 1.
Similarly, array2 = {3,2} // which are pow(2,3) and pow(2,2) indicate the numbers should be substituted with number 0.
My question is: Is there any way to combine and sort these two arrays into one new array. Eventually, we need to compare the value of the new array to seek for the repeated value to replace 0 and 1.
Example: lets look at number 19;
array1 = {4,1,0};
array2 = {3,2};
newarray = {4,3,2,1,0};
expectedoutput = {1 0 0 1 1};
Below is my code to convert from decimal to binary, but it does not accomplish due to the unsolved question above.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(){
int number,number1,y,i,total=0,z,a,ya,a1,m,n,count1=0,count2=0;
int array1[10];
int array2[10];
float x,xa;
printf("Enter the number you want to convert from decimal integer to binary: \n");
scanf(" %d",&number1);
number = number1;
x = log2f(number1);
y = floor(x);
while(y!=0){
for (m=0;m<=100;m++){
x = log2f(number1);
y = floor(x);
number1 = number1 - pow(2,y);
//array1 = (int * )malloc(y * sizeof(int));
array1[m] = y;
count1 += 1;
if (number1==0){
break;
}
}
}
x = log2f(number);
y = floor(x);
for (i=0;i<=y;i++){
z = pow(2,i);
total += z;
}
a = total - number;
a1=a;
xa = log2f(a);
ya = floor(xa);
while(ya!=0){
for (n=0;n<=100;n++){
xa = log2f(a);
ya = floor(xa);
a = a - pow(2,ya);
array2[n] = ya;
count2 += 1;
if (a==0){
ya = 0;
break;
}
}
}
Assuming both array1 and array2 are sorted in descending order, one approach to create the binary array expectedoutput is by continuously comparing the first elements of array1 and array2. Whichever is greater of the two first elements will be the value that gets sent to expectedoutput as either a 1 or a 0. The greater first element is then removed from its containing array. Here is an illustration:
/*
expectedoutput = {}
array1 = {4, 1, 0}
array2 = {3, 2}
procedure: --------------------------------------------------------------------
(any element from array1 is added to expectedoutput as 1, and from array2 as 0)
{4, 1, 0} ------- ------- {3, 2}
^ | ^
4 > 3 --> array1 = {1, 0} , expectedoutput = {1}
|
{1, 0} ------- ------- {3, 2}
^ | ^
1 < 3 --> array2 = {2}, expectedoutput = {1, 0}
|
{1, 0} ------- ------- {2}
^ | ^
1 < 2 --> array2 = {}, expectedoutput = {1, 0, 0}
|
{1, 0} ------- ------- {}
|
----------> array2 is empty, so add 1 to expectedoutput
until array1 becomes empty
Now: array1 = {}, array2 = {},
expectedoutput = {1, 0, 0, 1, 1}
*/
Since we are dealing with 1s and 0s, it would be a good idea to set empty arrays to some invalid value like -1. You can use memset at initial declaration as int array1[10]; memset(array1, -1, sizeof(array1)); so that later on array1 = {4, 1, 0, -1, -1, -1, -1, -1, -1, -1}. Similarly with any other array.
Now we can implement the above procedure by writing two functions: one that removes the first element from an array, and another that creates expectedoutput using two arrays.
To remove first element of an array:
// remove_first(arr, len) removes the first element of arr[len]
// For example: arr[5] = {3, 2, 6, -1, -1}
// remove_first(arr, 5)
// arr[5] == {2, 6, -1, -1, -1}
void remove_first(int *arr, int len) {
for (int i = 0; i < len-1; ++i) {
arr[i] = arr[i+1];
}
arr[len-1] = -1;
}
To create binary array:
// create_binary_array(arr, arr1, len1, arr2, len2) updates arr by setting
// its elements to either 1 or 0, using arr1 and arr2
// For example: arr[10] = {}, arr1[10] = {3, 1}, arr2[10] = {2}
// create_binary_array(arr, arr1, 10, arr2, 10)
// arr == {1, 0, 1}, arr1 == {}, arr2 == {}
void create_binary_array(int *arr, int *arr1, int len1, int *arr2, int len2) {
int i = 0;
while (arr1[0] != -1 || arr2[0] != -1) {
if (arr1[0] == -1) {
while (arr2[0] != -1) {
arr[i] = 0;
++i;
remove_first(arr2, len2);
}
} else if (arr2[0] == -1) {
while (arr1[0] != -1) {
arr[i] = 1;
++i;
remove_first(arr1, len1);
}
} else if (arr1[0] > arr2[0]) {
arr[i] = 1;
++i;
remove_first(arr1, len1);
} else if (arr1[0] < arr2[0]) {
arr[i] = 0;
++i;
remove_first(arr2, len2);
}
}
}
so now you can have:
int expectedoutput[10];
memset(expectedoutput, -1, sizeof(expectedoutput));
int array1[10] = {4, 1, 0, -1, -1, -1, -1, -1, -1, -1};
int array2[10] = {3, 2, -1, -1, -1, -1, -1, -1, -1, -1};
create_binary_array(expectedoutput, array1, 10, array2, 10);
// Now: expectedoutput == {1, 0, 0, 1, 1, -1, -1, -1, -1, -1}