Compare entire array lines for a set of variables - c

What i am trying to do is simple: i want to compare an entire line of an array in search of a set of variables in order like this:
var[2][]=={1,2,3,4,5} //all at the same time
instead of
var[2][0]==1 && var[2][1]==2 && var[2][2]==3 && var[2][3]==4 && var[2][4]==5 //one by one
or maybe
vartwo[4][3...7]==var[2][] ou vartwo[4][]==var[2][]
void main() {
int i,var[5];
puts("enter values:");
for(;i<5;i++) {
scanf("%d",&var[i]);
};
if(var[]=={1,1,1,0,0,0}) {
//do stuff
} else if (var[]=={1,2,3,5,2}) {
//do something else
}
.
.
.
} else {
//not found
}
}
is there any way i can do that?

Sorry to break but there is no way you can compare two arrays like the way you showed in C.
Simple old looping with element wise comparison is the way out.
for(size_t i = 0; i < len ; i++)
if( arr[i] == anotherarr[i] )
// same
memcmp is an option but unless you can ensure there will be no padding you can't use it reliably.

Related

How to simplify compound negated logic

Lets say I have array of boolean values B[], or I am figuring out true/false using function. How can I simplify this code, it there are many values (maybe tens of them)? Here is pseudocode:
if(!B[0]){
doTask1;
}
if(!B[0] && !B[1]){
doTask1;
doTask2;
}
if(!B[0] && !B[1] && !B[2]){
doTask1;
doTask2;
doTask3;
}
...
Edit 1: I forgot to mention, that I want doTask1 etc. happen only once (if any of there ifs is true), not doing it multiple times(if for example 1.st if is true, second one is true too, i still need to happen it only once)
You can use array of function pointers.
typedef void *(* funcPtr)(void);
funcPtr arrayFunPtr[N];
Then store the functions into the array.
arrayFunPtr[0]= task1;
arrayFunPtr[1]= task2;
....
arrayFunPtr[N-1]= taskN;
Then loop the bool array and call respective index function.
for(int i=0;i<N;i++)
{
if(!B[i]) arrayFunPtr[i]();
}
#Edit.
If you want to stop calling tasks function once you hit B[i]=true use the below code.
for(int i=0;i<N && !B[i] ;i++)
{
arrayFunPtr[i]();
}
(Assuming you don't want to make the same task more than once, even though your original code will)
Make a function doTask() which will take the number of the task to do (or just make an array of function pointers). And then:
for(i=0; i < numberOfTasks; i++)
{
if (!B[i])
doTask(i);
else
break;
}
Or more concisely:
for(i=0; i < numberOfTasks && !B[i]; i++)
{
doTask(i);
}
Function pointers is the way to go here.
If you do not like function pointer you might use the switch construct as shown below:
switch(i) {
case 3:
if(!B[0] && !B[1] && !B[2]) doTask3();
case 2:
if(!B[0] && !B[1]) doTask2();
case 1:
if(!B[0]) doTask1();
}
Or you can do :
You can write a method :
simplify(int[] B, int l) {
for (int i =0 ; i < l, i++) {
// write the code using &&
}
}
Then can call this method as :
if(simplify(B, 1){
doTask1;
}
if(simplify(B, 2){
doTask1;
doTask2;
}
etc.

How can I write a string that changes from iteration to iteration?

If I want to write in for loop (for example) something with the next mention:
for(int i=0;i<5;i++) {
char* str_3="atom_3";
if(strcmp(str_3,"atom_i")!=0){ // I know that it's not true to get it by
//this line.
printf("FALSE");
}
}
I want to get effect so that str_i will be compared with atom_0, atom_1 , ... , atom_4.
How can I do it?
How can I write a string that changes from iteration to iteration?
... to get effect so that str_i will be compared with "atom_0", "atom_1" , ... , "atom_4".
Construct the compare string on each iteration.
// This is not elegant, yet meets OP goals.
for(int i=0;i<5;i++) {
char atom_i[30];
sprintf(atom_i, "atom_%d", i);
if(strcmp(str_i, atom_i)!=0){
printf("FALSE");
}
}
Yet a better approach would scan str_i and avoid iteration.
int d = 0;
char ch; // Use to look for extra text
if (sscanf(str_i, "atom_%d%c", &d, &ch) == 1) {
if (d >= 0 && d < 5) {
; // do something with str_i
}
}

Shortest code to check if all int array elements are equal to a specific number?

What's the shortest way I can do an if..else statement to check if all int array elements are equal to certain numbers? For example in pseudocode:
if (allElementsOfIntArray == -1)
//do something
else if (allElementsOfIntArray == 1)
//do something else
I have another variable that is const int arraySize
My Arduino code is getting really messy at this point so I'm just trying to find the shortest way to implement this so it doesn't look like a mess to others that have to read it.
bool all_are(int* i_begin, std::size_t sz, int x)
{
const int* i_end = i_begin + sz;
for(; i_begin != i_end; ++i_begin)
if(*i_begin != x) return false;
return true;
}
if (all_are(my_array, arraySize, -1))
//do something
else if (all_are(my_array, arraySize, 1))
//do something else
Check first if all entries are the same and then switch():
for (int i=1; i<arraysize; i++)
if (theArray[0] != theArray[i]) return "no way";
/*** All array elements are the same, so we can evaluate any element ***/
switch (theArray[0])
{
case 0:
return "All are zero";
case 1:
return "All are one";
default:
return "All elements are the same";
}
The quickest way that pops into mind (assuming the array is unsorted) is to just iterate through it and checking each element. You can do this a few ways. Use a normal for loop or try a for each.
Each element in array is independent to others. So you must use for to check all of it to make sure.

AS3 keeping score

I'm making a game where my character passes an object and when doing so the score counts up by one. The object is part of an array, and when I pass the first object the score counts up continuously. Here is what I have so far:
if (beardman.x >= tubes[i].x)
{
score++;
}
How would I re-write it so that the score just counts up by one?
What you could do is have a similar array and remove from that list like so:
//assume you have a copy of the tubes array called activeTubes.
if (beardman.x >= activeTubes[i].x)
{
score++;
activeTubes.splice( i, 1 );
}
Edit: I wanted to add that splice is known to be a fairly expensive operation as it shifts how the indices are stored in memory. That being said, if you are searching through a fairly large list a optimization to make would look like this:
//assume you have a copy of the tubes array called activeTubes.
if (activeTubes[i] && beardman.x >= activeTubes[i].x)
{
score++;
activeTubes[i] = null;
}
Or even from a for loop perspective:
var i:int;
for ( ; i < activeTubes.length; i++ ) {
//check if the index is null, if so continue.
if ( !activeTubes[i] ) { continue; }
if (beardman.x >= activeTubes[i].x)
{
score++;
activeTubes[i] = null;
}
}

How do i check if atleast one element of bitmap array is not zero?

I am not looking for a function in C, a macro would be helpful. This is how I would like to use the bitmap.
Usage :
int bitmap;
if(bitmap != 0)
do something
else
do something
Suppose bit map becomes an array of bitmaps, how do I achieve the same result (without making a function call)?
You can iterate over the array:
int i;
int non_zero_found = 0;
for (i = 0; i < sizeofthearray; ++i)
{
if (array[i] != 0)
{
non_zero_found= 1;
break;
}
}
if (non_zero_found)
{
// do something
}

Resources