Pass a 2D array to function - arrays

I have a 2D array
byte sensor_AND_1[2][NUM_OF_MAX_SENSORS] = { { 0 }, { 0 } };
byte sensor_AND_2[2][NUM_OF_MAX_SENSORS] = { { 0 }, { 0 } };
and a function where I need to pass the array to
if (is_valid_sensor_id_AND(sensor_AND_1,ID)) { /*do something*/ }
else if (is_valid_sensor_id_AND(sensor_AND_2,ID)) { /*do something*/ }
What is the correct declaration to pass the whole array to the function?
bool is_valid_sensor_id_AND(byte *arr, byte sensor_id)
{
for (size_t i = 0; i < NUM_OF_MAX_SENSORS; i++) {
if (arr[0][i] == sensor_id) {
arr[1][i] = 1;
if (isset_counter(arr[1], NUM_OF_MAX_SENSORS)==isset_counter(arr[0], NUM_OF_MAX_SENSORS)) { triggered(sensor_id); memset(arr[1], 0, NUM_OF_MAX_SENSORS); }
return true;
}
}
return false;
}

I found the answer how to pass it to the function:
bool is_valid_sensor_id_AND(byte arr[][], byte sensor_id)

Related

unable to initialize array size from parametrize constructor of class Stack1...but if i directly feed integer value to array code work fine

package the_JC;
public class Cls_01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack1 obj1 = new Stack1(20);
for(int i = 1; i<=obj1.len; i++) {
obj1.push(i*3+i);
}
for(int i=0; i < obj1.len; i++) {
System.out.println(obj1.pop());
}
}
}
class Stack1{
int len=0;
Stack1(int num){
len = num;
System.out.println(len);
}
int[] stck = new int[len]; // this array is not accepting len as value from constructor above ,taking len =0
int pos = -1;
void push(int value) {
System.out.println(stck.length);
if(pos==len-1) {
System.out.println("Overflowed");
} else {
stck[++pos] = value;
System.out.println("Pushed value :\t"+value);
}
}
int pop(){
if(pos<0) {
System.out.println("Underflow");
return 0;
} else {
return stck[pos--];
}
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at the_JC/the_JC.Stack1.push(Cls_01.java:33)
at the_JC/the_JC.Cls_01.main(Cls_01.java:10)**
class Stack1{
int len=0;
int[] stck;
int pos;
Stack1(int num){
len = num;
stck = new int[len];
pos = -1;
System.out.println(len);
}
void push(int value) {
System.out.println(stck.length);
if(pos==len-1) {
System.out.println("Overflowed");
} else {
stck[++pos] = value;
System.out.println("Pushed value :\t"+value);
}
}
int pop(){
if(pos<0) {
System.out.println("Underflow");
return 0;
} else {
return stck[pos--];
}
}
}

Finding depth of specified element in an array of arrays (or list of lists etc.)

I've to create a function (using pseudocode) which returns depth of specified element in an array (with optional arrays inside), e.g.:
def array[] = {"a", {"b", {"c"}, "d"}, {{{}}}, "e"};
For "e" it should returns 0, for "c" it should returns 2 etc.
If there's no specified element in array, function should returns -1.
I've tried a few times, but I've no idea for elegant (and working..) solution, only this:
func foo(array[], var element) {
int depth = 0;
boolean found = false;
boolean recursion = false;
boolean foundInRecursion = false;
for (int i = 0; i < array.length; i++) {
if (array[i] instanceof array[]) {
depth++;
recursion = true;
foo(array[i], element);
recursion = false;
} else if (array[i] == element) {
if (recursion) {
foundInRecursion = true;
} else {
found = true;
}
}
}
if (foundInRecursion) {
return depth;
} else if (found){
return 0;
} else {
return -1;
}
}
I would really appreciate any help!
Thanks
In your pseudocode:
func depth(array[], var element) {
/* this function returns how deep the element is in the given array */
for (int i = 0; i < array.length; i++) {
current = array[i]
if current == element {
return 0; // element is right in the array we are given
}
if (current instanceof array[]) {
// Whoa, subarray! How deep our element is in it?
subdepth = depth(current, element)
if subdepth >= 0 {
// Element was found in *sub*-array, so add 1 to the depth
return subdepth + 1;
}
}
}
// If we are here, we found nothing
return -1;
}
I believe that such elegant solution should be an iterative code that goes over each layer.
public int IterativeDepth(List<Object> lst, T element)
{
// Set the first iteration
int depth = 0;
List<Object> next = new List<Object>();
while ( ! IsNullOrEmpty(lst) )
// For each layer
{
foreach (var current in lst)
// For each element of a layer
{
if (current instanceof T && current == element)
// Found it, return the depth
{
return depth;
}
if (current instanceof List) {
// It's a list, append it to the next iteration
next.Add(current);
}
}
// Set the next iteration
lst = next;
next.clear();
depth += 1;
}
// Found nothing
return -1;
}

compare more than two strings

I have 5 strings. I need to compere all five at once.
char set_password1[5] = "1111";
char set_password2[5] = "2222";
char set_password3[5] = "3333";
char set_password4[5] = "4444";
char set_password5[5] = "5555";
if(!strcmp(Entered_Password,set_password1))
{
}
If any of these passwords match with Enter _Password i need to do something. so do i have to write five if statements like this
if(!strcmp(Entered_Password,set_password1))
{
}
if(!strcmp(Entered_Password,set_password2))
{
}
if(!strcmp(Entered_Password,set_password3))
{
}
if(!strcmp(Entered_Password,set_password4))
{
}
if(!strcmp(Entered_Password,set_password5))
{
}
or is there any other way.
I already tried this way, but it didn't work.
if(!strcmp(Entered_Password, (set_password1||set_password2||set_password3||set_password4||set_password5))
{
}
Alternatively, you may use a 2D array.
char password[5][5] = {"1111", "22222", "3333", "44444", "55555"};
int match = 0;
for(int i = 0; i < 5; i++)
{
if(strcmp(password[i], user_password) == 0)
{
match = 1;
break;
}
}
if(match == 1)
{
//do your action
}
Replace line
if(!strcmp(Entered_Password, (set_password1||set_password2||set_password3||set_password4||set_password5))
with
if (!(strcmp(Entered_Password, set_password1) && strcmp(Entered_Password,set_password2) && (strcmp(Entered_Password,set_password3) && strcmp(Entered_Password,set_password4) && strcmp(Entered_Password,set_password5)))
You have to compare each variable seperately with the initial string.

the path to a point in a matrix. Want to solve it by DP

The given matrix is the problem matrix. With cell 0,0 marked as 2 to show the entry point. We have to find the path to reach element 9. I have used recursive backtracking and marked the valid path as 2 that will lead to the element 9. Can someone help me in implementing it using DP.
include <stdio.h>
int path(int [][7],int ,int);
static int exitstack=0;
int main(void) {
int result,i,j;
int arr[7][7]={
{2,1,1,1,1,1,1},
{0,1,1,0,0,0,1},
{0,1,0,1,1,0,1},
{1,0,9,0,1,1,1}, //the problem matrix
{1,0,1,0,1,1,0},
{1,1,1,1,1,0,1},
{1,0,0,0,0,0,1}
};
path(arr,0,0);
for(i=0;i<7;++i)
{
for(j=0;j<7;++j)
printf("%d",arr[i][j]);
printf("\n");
}
return 0;
}
int path(int p[7][7],int x,int y)
{
int i;
if(p[x][y]==9)
{
exitstack=1;
return 1;
}
for(i=1;i<=4;++i)
{
if(i==1) //moving right
{
if((y+1)<7)
{
if(p[x][(y+1)]==9)
path(p,x,(y+1));
if(p[x][(y+1)]==1)
{
p[x][(y+1)]=2;
if(path(p,x,(y+1)))
return 1;
}
}
}
if(i==2) //moving up
{
if((x-1)>0)
{
if(p[(x-1)][y]==9)
path(p,x-1,y);
if(p[(x-1)][y]==1)
{
p[x-1][y]=2;
if(path(p,x-1,y))
return 1;
}
}
}
if(i==3) //moving left
{
if(y-1>0)
{
if(p[x][(y-1)]==9)
path(p,x,y-1);
if(p[x][(y-1)]==1)
{
p[x][(y-1)]=2;
if(path(p,x,y-1))
return 1;
}
}
}
if(i==4) //moving down
{
if(x+1<7)
{
if(p[x+1][y]==9)
path(p,x+1,y);
if(p[x+1][y]==1)
{
p[x+1][y]=2;
if(path(p,x+1,y))
return 1;
}
}
}
}
if(p[x][y]==9)
{
return 1;
}
else
{
if(exitstack==1)
return 1;
else
{
p[x][y]=0;
return 0;
}
}
}
If you by DP mean dynamic programming, you could try to use the D* algorithm. It's a bit too long to describe here, but you can read more about it here (http://www.cs.cmu.edu/~ggordon/likhachev-etal.anytime-dstar.pdf) or here (http://en.wikipedia.org/wiki/D*), or google it.

Accessing certain elements in an Struct in C

So I have the following struct I created:
struct _I_TypeInstructions {
const char *instructionName;
char *opcode;
} I_TypeInstructions[] = { { "lw", "100011" }, { "sw", "101011" }, { "beq",
"000100" } };
typedef struct _I_TypeInstructions I_TypeInstructionsStruct;
If I have a new instructionName and I want to check if it is in the I_TypeInstructionsStruct how do I iterate through just the *instructionName part of the struct above. For example the function I want to write would look something like
bool checkIfInstructionIsI_Type(char *instructionName) {
// somehow iterate through instructionNames in the struct above
// checking if parameter char *instructionName in this method is equal to
// "lw" "sw" "beq" but skipping over the binary numbers.
}
Searching a list of structs is rather straight forward:
bool checkIfInstructionIsI_Type(char *instructionName)
{
for (int i = 0; i<NumInstructions; i++)
{
if (strcmp(I_TypeInstructions[i].instructionName, instructionName) == 0)
{
return true;
}
}
return false;
}
int i;
for(i = 0; i < 3; ++i)
{
if (strcmp(instructions[i].instructionName, instructionName) == 0)
{
printf("Match found");
}
}
It's generally more useful to return the actual element that matches your string. It's the same amount of work anyway.
Add an empty element to the end of your array and then you have a end marker.
typedef struct _I_TypeInstructions {
const char *instructionName;
char *opcode;
} I_TypeInstructionsStruct;
I_TypeInstructionsStruct I_TypeInstructions[] = {
{ "lw", "100011" },
{ "sw", "101011" },
{ "beq", "000100" },
{ 0, 0}
};
I_TypeInstructionsStruct *find_instruction(char *name)
{
I_TypeInstructionsStruct *i ;
for (i = I_TypeInstructions ; i->instructionName ; i++)
if (!strcmp(i->instructionName,name)) return i ;
return 0 ;
}

Resources