Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Programming language to use is C In convert.c, there is a function called itob which takes three arguments, a character array (for a buffer) and

Programming language to use is C

In convert.c, there is a function called itob which takes three arguments, a character array (for a buffer) and two numeric arguments.

The first argument is a buffer which has been passed to the function so that it can return a string value, like with fscanf and fgets.

The second argument is the numeric base for which you should be producing the representation.

The third argument, is the actual number that you're producing the representation of.

This function is void, since it is returning the string value through the first argument.

If the number is negative, the function should return the string "0\0".

Follow the process we described in the discussion on Modulo to extract digits from a number.

This function should be able to make use of the digit_itob function on each digit, again to avoid the headache of doing ASCII manipulation in this function.

itob(buffer, 36, 1664957) buffer = "ZOOT"

itob(buffer, 36, 29234652) buffer = "HELLO"

itob(buffer, 10, 2018) buffer = "2018"

itob(buffer, 16, 127) buffer = "7F"

itob(buffer, 10, -1) buffer = "0"

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

This is how I implemented digit_itob

#include

char digit_itob(int number, int base) { if(number < 0 || number >= base || base <= 0 || base > 36) { return '\0'; } else { return "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[number]; } }

int main() { printf("'%c' ", digit_itob( 15, 16)); printf("'%c' ", digit_itob( 0, 10)); printf("'%c' ", digit_itob( 1, 16)); printf("'%c' ", digit_itob( 35, 36)); printf("'%c' ", digit_itob( 35, 10)); printf("'%c' ", digit_itob( 10, 0)); printf("'%c' ", digit_itob( -1, 10)); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions