Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Progaming Purpose: Use bit operations to extract information Demonstrate understanding of number formats and binary representations Assignment: Computers sometimes use a format called binary

C Progaming

Purpose:

  • Use bit operations to extract information
  • Demonstrate understanding of number formats and binary representations

Assignment:

Computers sometimes use a format called binary coded decimal or BCD. Good background information is available from https://en.wikipedia.org/wiki/Binary-coded_decimal , but it is far deeper than you need for this assignment. (read at your own risk, the section marked Basics covers all you need to know at this point in time).

BCD encodes a decimal number into 4 binary digits. A byte holds 2 BCD digits. A 32 bit word can contain 8 packed BCD digits.

Decimal digit

BCD

8

4

2

1

0

0

0

0

0

1

0

0

0

1

2

0

0

1

0

3

0

0

1

1

4

0

1

0

0

5

0

1

0

1

6

0

1

1

0

7

0

1

1

1

8

1

0

0

0

9

1

0

0

1

Write a program that accepts an integer input (base 10) to stdin. This input value is in packed BCD format. Note that not every possible decimal input value will map to a corresponding BCD number, some bit patterns are not valid BCD. Using the packed BCD format, display the number as a sequence of characters to stdout. The sequence of characters is a simulation of a calculators display (5 characters wide, 5 characters tall, with a space between each).

  1. Display any BCD digit as a 0 if it is an invalid encoding.
  2. All numbers are positive.
  3. Note that the number provided as input data is in packed BCD representation.
  4. Process input until a -1 is entered. Do not process the -1, just finish without printing anything out.
  5. Output must match the example exactly. Dont try and make the calculator display more readable.
  6. Use bit operations to extract the digits.
  7. The program must work for any valid input values; provided they do not overflow an integer. Suggest you test with 10 or 20 different values prior to turning it in.
  8. A two dimensional array is useful, but not essential for completing this program.
  9. Including comments, this assignment can be completed in less than 100 lines of code. If you are writing much more than this, you are on the wrong track. Ask for help.
  10. Every function must have comments that describe its purpose,implementation, and interface.
  11. Any pre and post conditions for a function must be described in the comments, and checked at runtime using assert ().
  12. As in previous assignments, suggest you use bash input redirection to make a test file to verify you program works with many inputs.
  13. Compile with -std=c99
  14. Turn into blackboard a single C file.

Examples:

Consider an input value of 4660 base 10.

This number is 0x1234 expressed in hex.

Or

0000 0000 0000 0001 0010 0011 0100

In packed BCD, this number is

0001234

Another examples:

Input value of 39321 base 10.

This number is 0x9999 expressed in hex.

Or

1001 1001 1001 1001

In packed BCD, this number is

9999

Here is an example of a decimal number that is not a valid representation of a BCD number

Input value of 298 base 10.

This number is 012A hex.

Or

0000 0001 0010 1010

This is not a valid packed BCD number, as the last digit is 1010, which is not a valid BCD digit.

For the purpose of making test cases for this assignment:

  • Determine the BCD value you want to display on the calculator display
  • Write down the bit pattern for this number
  • Convert the bit pattern to hex. (note no hex digit is > 9 for valid bcd)
  • Convert that hex number into decimal. This is the input to the program.

Grading Criteria:

1. Correct output, observed by running the code against several input files; each input file containing several input values.

2. Indentation, commenting of code

3. Compliance with the assignments requirements

4. Compiles without warnings using -Wall

Example Output:

$ gcc -Wall -std=c99 -g hw4.c

$ ./a.out

4660 (reads 00001234)

--- --- --- --- ---- ----

| | | | | | | | | / | |

| | | | | | | | | / ---- ----

| | | | | | | | | / | ^

--- --- --- --- ---- ---- ^

305419896 (reads 12345678)

---- ---- ---- ---- ---

| / | | | | / | |

| / ---- ---- ---- --- / ---

| / | ^ | | | / | |

---- ---- ^ ---- --- ---

39321 (reads 00009999)

--- --- --- --- --- --- --- ---

| | | | | | | | | | | | | | | |

| | | | | | | | --- --- --- ---

| | | | | | | | | | | |

--- --- --- ---

-1 (ends program)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

Students also viewed these Databases questions