Program must be in C++ Comment the program well Write a program that prints all
ID: 641773 • Letter: P
Question
Program must be in C++
Comment the program well
Write a program that prints all sequences of 32 digits on stdout, such that each digit is a O or 1, each sequence is exactly 32 digits in length, and no sequence has two 1's adjacent in the output. For example, the following sequences should be included in the output (not necessarily in this order): The following sequences should not be printed, because each has ''11'' somewhere in the output: The following sequence also should not be printed, because it is not 32 digits in length: The following sequence also should not be printed, because not every digit is a 0 or 1: AAAA52A9 Program must be in C++ Comment the program wellExplanation / Answer
#include <iostream>
using namespace std;
void print (int n);
void aux_print(int n, char ch, int i, char a[]);// aux function which take length, 'currunt charecter', currunt length, char array as arguments
int main()
{
print(32);//print all the strings containg only '0' and '1' , length = 32 and no two 1's come to gether
return 0;
}
void print (int n)
{
char a[33]="";// char array to hold the strings generated
aux_print(n,'0',0,a);//call aux function with currunt charecter as '0' and currunt length = 0
aux_print(n,'1',0,a);// call aux function with currunt charecter as '1' and currunt length = 1
}
void aux_print(int n, char ch, int i, char a[])
{
if(i==n)//length of string = n(32)
{
a[i]='';
cout<<a<<" ";// print the string
return;
}
else
{
a[i]=ch;
if(ch=='0')//if the present character is '0' then the next charecter can be anything hence call the fuction with both '0' and '1' as next charecter
{
aux_print(n,'0',i+1,a);
aux_print(n,'1',i+1,a);
}
else
aux_print(n,'0',i+1,a);// if the currunt charecter is 1 then next charecter must be '0' hence call the fuction with only 0 as the next charecter
}
}
O/P:
Output
.
.
.
.
10101010101010101010101010101010
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.