Question: Hello Need help with a C Program. I have the starter code need the completion of code. Please review code it's missing information to run

Hello Need help with a C Program. I have the starter code need the completion of code. Please review code it's missing information to run code. I provided the test and output which he wants us to test the program. I provided only the starter code it's not complete .

Assignment

Your task is to develop a C program, called ascii2bin, that

  • reads a string of 1's and 0's as ASCII digits, and
  • outputs the equivalent decimal number

Your program must

  1. exercises the read() system call to read a single byte, at a time, from stdin
  2. validate that the read byte is appropriate for conversion, e.g., it must be either an ASCII '0' or '1'
  3. converts each byte into an integer value via a mathematical expression
  4. uses the resulting integer as part of the calculation to determine the final number
  5. identifies the end of a input string by either end of file or by a new line
    • End of file is detected when read() returns the value '0'
    • A new line is identified in the ASCII table as either: newline, nl, LF, or '
  6. prints this final number on stdout
  7. returns a value of 0 upon success and 1 otherwise

The Algorithm

 offset = ?; number = 0; retval = read(0, &ascii_value, 1); while (retval == 1) digit = ascii_value - offset; number = number << 1 + digit; retval = read(0, &ascii_value, 1); printf("%d ", number); return 0; 

Validation Checks:

You should add additional validation checks to your code to catch potential errors. At a minimum, validate the following:

  • that each ASCII input character is one of the following characters: '0', '1', or ' '
  • that the calculated number does not exceed 2^32

Here are the test Numbers he gave us:

0101 5
54356 0
138 0
2863311530 0
4294967295 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!