Home  >  Article  >  Backend Development  >  Boolean array puzzle in C language

Boolean array puzzle in C language

WBOY
WBOYforward
2023-08-29 15:05:051156browse

Boolean array puzzle in C language

This is an array-based puzzle that requires you to change all the numbers in an array containing two elements to 0. One element of the array is 0, and another element may or may not be 0.

To solve this puzzle, the program needs to find the non-zero element and change it to 0.

The following are the constraints required to solve the Boolean array puzzle

  • The allowed operation is the complement, other operations are not allowed.
  • Loops and conditional statements are not allowed.
  • Direct assignment is also not allowed.

Program to solve Boolean array puzzle

#include <iostream>
using namespace std;
void makeZero(int a[2]) {
   a[ a[1] ] = a[ !a[1] ];
}
int main() {
   int a[] = {1, 0};
   makeZero(a);
   cout<<"arr[0] = "<<a[0]<<endl;
   cout<<"arr[1] = "<<a[1];
   return 0;
}

Output

arr[0] = 0
arr[1] = 0
You can use other ways too. Like this one which does not require the negation operation.
a[ a[1] ] = a[ a[0] ]

The above is the detailed content of Boolean array puzzle in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete