Subject Computer Science C-Family Programming

Question

For this assignment, the main function will serve simply as i/o, so I have placed main into the same file. You may turn in your assignment with main in the same file.
When the C scanf function is called and the %d format specifier is used, implicitly there is a conversion from a base 10 number to binary. Likewise, when printf is used with %d, there is a conversion from binary to base 10. In the first part of your assignment, you will write functions that make this more obvious.

1. Write a function called scan_decimal. Here is its prototype:
void scan_decimal(int *xptr);
The function must call scanf using the %s format specifier, and convert the string to an int. You may only use the %s format specifier. Assume that the user will type a non-negative decimal number as input. Here is an example of its use. Let’s say the user
types 15
int x;
scan_decimal(&x);
printf(“%d\n”, x);
The above code should print 15

2. Write a function called print_decimal. Just as with scan_decimal, %s is the only format specifier that you are allowed to use in print_decimal. Here is its prototype:
void print_decimal(int x);
You may assume that x is a non-negative integer. Combined with scan_decimal, here is an example of its intended use. This time, let’s say the user types 373
int x;
scan_decimal(&x);
print_decimal(x);
The above code should print 373

3. Write a function called scan_negative. Here is its prototype:
void scan_negative(int *xptr);
Assume that the user will type a negative decimal number as input.
The function must call scanf using the %c and %s format specifiers, and convert the
string to an int. The %c speficier will be used to read (and discard) the ‘-‘at the beginning of the negative number. The %s format will be used to read the digit characters in the rest of the string. Here is an example of its use. Let’s say the user types -15
int x;
scan_negative(&x);
printf(“%d\n”, x);
The above code should print -15

4. Write a function called scan_hex. Here is its prototype:
void scan_hex(int *xptr);
Here is an example of its use, in conjunction with print_decimal.
Assume that the user will type a non-negative hex number as input.
Also, assume that the “digits” a-f are in lower case. Let’s say the user
types 1b
int x;
scan_hex(&x);
print_decimal(x);
The above code should print 27

5. Write a function called print_hex. Here is its prototype:
void print_hex(int x);
Here is an example of its use, along with scan_hex and
print_decimal:
int main() {
int x;
printf(“Type a positive hex integer\n”);
scan_hex(&x);
printf(“You typed: \n\t”);
printf_decimal(x);
printf(“ in decimal, or ”);
print_hex(x);
printf( ‘in hex.\n”);
}

If the user inputsc 2ac, then the output should be You typed 684 in decimal, or 2ac in hex.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

// The format of the input is a base 10.
void scan_decimal(int *xptr) {
    char buffer[256];
    char newline;
    *xptr = 0;
    scanf("%s%c", buffer, &newline); // gets rid of \n after the input

    // you write the rest
int length;
for (length = 0; length < sizeof(buffer); length++) {
       if (buffer[length] == '\0') {
            break;
       }
}
int i;
int base = 1;
for (i = length-1; i >= 0; i--) {
       *xptr += (buffer[i] - '0') * base;
       base *= 10...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor
Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats