Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment you will create a C program that displays an 8-bit signed nu

ID: 3786088 • Letter: F

Question

For this assignment you will create a C program that displays an 8-bit signed number as a string of eight 1's or 0's. Your program should query the user for an 8 bit number between -128 and +127. After the user has entered the number, your program will show the number in binary. The program should loop until the user enters a number outside of the requested range. An example of what the interaction should look like is below. The colors are used to make it more clear what is entered and what your program should print (i.e. the lines in red). You do not have to print your text in color.

You will only need to submit your C source code file for this assignment.

Please enter a number between -128 and 127: 16

00001000

Please enter a number between -128 and 127: 15

00001111

Please enter a number between -128 and 127: 128

128 is out of range. Bye!

Explanation / Answer

C code:

#include <stdio.h>
#include <math.h>
int main()
{
   int n;
   while(1)
   {
       printf("%s ","Please enter a number between -128 and 127:" );
       scanf("%i",&n);
       if(n<-128 || n> 127)
       {
           printf("%i is out of range. Bye! ",n );          
           return 0;
       }
       else
       {
           if(n>=0)
           {
               int j = 0;
               int arr[8];
               while(j<8)
               {
                   arr[j] = n%2;
                   n = n/2;
                   j = j+1;
               }
               int i = 7;
               while(i >= 0)
               {
               printf("%i",arr[i]);
               i--;                          
               }
               printf(" ");
       }
       else
       {
           n = -n;
               int j = 0;
               int arr[8];
               while(j<8)
               {
                   arr[j] = n%2;
                   n = n/2;
                   j = j+1;
               }

           // invert
               int x = 0;
               while(x<=7)
               {
                   if(arr[x] == 1)
                   {
                       arr[x] = 0;
                   }
                   else
                   {
                       arr[x] = 1;
                   }
                   x = x+1;
               }
//           add 1
               int comp = 0;
               x = 0;
               while(x<=7)
               {
                   comp = comp + (arr[x]*pow(2,x));
                   x++;
               }
               comp = comp + 1;  
               j = 0;                          
               while(j<8)
               {
                   arr[j] = comp%2;
                   comp = comp/2;
                   j = j+1;
               }

               int i = 7;
               while(i >= 0)
               {
               printf("%i",arr[i]);
               i--;                          
               }
               printf(" ");

       }                          
       }
   }
   return 0;
}

Sample Output:

Please enter a number between -128 and 127:
-100
10011100
Please enter a number between -128 and 127:
-128   
10000000
Please enter a number between -128 and 127:
-127
10000001
Please enter a number between -128 and 127:
15
00001111
Please enter a number between -128 and 127:
2
00000010
Please enter a number between -128 and 127:
128
128 is out of range. Bye!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote