This is a c program practice must solve this problem recursively Write a program
ID: 3847511 • Letter: T
Question
This is a c program practice
must solve this problem recursively
Write a program called bin_str.c that completes a binary number. A binary number is made up of 0's and 1's but the input strings you will receive can also contain x's. An x represents a digit that can be either a 0 or a 1. Your program should display all the possible binary digits that can be formed. For example the string x1x0 could represent either 0100, 0110, 1100, or 1110.
1. Your program should accept the binary string as a command line argument 2. You MUST solve this program RECURISIVELY
Examples
1../bin_str.out 0110 0110
2../bin_str.out 01x0 0100
0110 3../bin_str.out xx
00 01 10 11
4../bin_str.out 101x100x11x 10101000110
10101000111
10101001110
Explanation / Answer
The program to print all the possible binary numbers by filling up the 'x' is as follows. The input is given and the letter 'z' is denoted to end the input.
#include <stdio.h>
int main()
{
printf("Enter input and z to end");
char ip;
int x[5];
char input[20];
int i=0;
int j=0;
scanf("%c",&ip);
//char input[]=scanf("%s");
while(ip!='z')
{
input[i]=ip;
if(input[i]=='x')
{
x[j]=i;
j++;
}
i++;
scanf("%c",&ip);
}
int i,n,m;
char s[10];
char ch[2];
printf(" Enter the no of bits : ");
scanf("%d",&n);
ch[0]=(char)(n+48); //Converting value of n into character
ch[1]='';
strcpy(s," %0");
strcat(s,ch);
strcat(s,"d"); //s contains following string "%0nd" where n is the no of bits
m=pow(n,2)-1;
for(i=0;i<=m;i++)
{
printf(s,dec_to_bin(i));
}
return 1;
}
int dec_to_bin(int i)
{
int b=0,pos=0,remainder;
while(i>0)
{
remainder=i%2;
b+=remainder*pow(10,pos++);
i/=2;
} return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.