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

Boolean array puzzle in C language

WBOY
Release: 2023-08-29 15:05:05
forward
1187 people have browsed it

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;
}
Copy after login

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] ]
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template