The program will first ask the user to enter in a value between [0, 4096]. The p
ID: 3640141 • Letter: T
Question
The program will first ask the user to enter in a value between [0, 4096]. The program will then ask the user to select one of three options: Extract, Set, or Quit. If the user selects either Extract or Set, then the program will either extract a bit from the input value, or it will set a bit for the value. The user will enter the position of the bit to either extract or set after being prompted. The program should then display the initial value in binary and the result in binary and then prompt the user with the menu once again. If the user selects the Quit option, then the program should end.
Notes: You can assume that the integer values entered in will be within the correct range of values You should error check for user input for the menu options, i.e., only an “e”, “s”, or “q” should be entered in for the menu choice. If not, then prompt the user again. You should not use any of the shift instructions in your code.
Use scanf to read input from the keyboard and printf to display your message on the monitor.
GIVEN: code to do the program
#include<stdio.h>
int main (void)
{
int x;
printf("Please enter a value between [0,4096]: ");
scanf("%d", &x);
printf("User input: %d ", x);
printf("Please select one of the following functions: ");
printf("e : Extract a particular bit ");
printf("s : Set a particular bit ");
printf("q : Quit this program ");
}
Example: Here is an example of what your program will do:
Please enter in a number between [0-4096]:
User input: 25
Please select one of the following functions:
e : Extract a particular bit
s : Set a particular bit
q : Quit this program
/* The user will then type in a character followed by the ENTER key. Your program will respond accordingly. Assume the user enters ‘e’. */
Please enter in the bit position to extract [0-31]:
// Assume the user enters ‘3’.
The initial value in binary is:
00000000000000000000000000011001
The bit value in position 3 is 1
Please select one of the following functions:
e : Extract bits of a field
s : Set bits of a field
q : Quit this program
// Assume the user enters in ‘s’:
Please enter in the bit position to set [0-31]:
//Assume the user enters ‘6’.
The resulting value in binary is:
00000000000000000000000001011001
Please select one of the following functions:
e : Extract bits of a field
s : Set bits of a field
q : Quit this program
Explanation / Answer
Hi dude, If you want to give some credit you can justify it by giving it to me....because i have made the solution this morning itlsef for someone else...and the 2 above me have just blindly copied it..
Anyways...
I have used "fflush(stdin)" in two places as you can see below. This is to clear the buffer before and after taking any character or string input from keyboard otherwise the program will not work properly.
This is a problem only in the C language.....:)...
#include<stdio.h>
void main()
{
char e,d;
int a[32],b,i,c=31;
printf("Please enter in a number between [0-4096]: User input: ");
scanf("%d",&b);
for(i=0;i<32;i++)
{
a[i]=0;
}
while(b!=0)
{
a[c]=b%2;
c--;
b=b/2;
}
c=31;
while(1)
{
printf(" Please select one of the following functions: e : Extract a particular bit s : Set a particular bit q : Quit this program ");
fflush(stdin);
scanf("%c",&d);
fflush(stdin);
if(d=='e')
{
printf("Please enter in the bit position to extract [0-31]:");
scanf("%d",&e);
printf("The initial value in binary is: ");
for(i=0;i<32;i++)
{
printf("%d",a[i]);
}
printf(" The bit value in position %d is %d ",e,a[31-e]);
}
else if(d=='s')
{
printf(" Please enter in the bit position to set [0-31]:");
scanf("%d",&e);
a[31-e]=1;
printf("The resulting value in binary is: ");
for(i=0;i<32;i++)
{
printf("%d",a[i]);
}
}
else if(d=='q')
{
break;
}
else printf("Wrong choice, enter again: ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.