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

write a program in C plain, there is a function called itob which takes three ar

ID: 3727001 • Letter: W

Question

write a program in C plain, 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".

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"

Explanation / Answer

/****** I have provided the complete implementation along with all your test cases. It is working fine as expected ****/

#include <stdio.h>
#include <string.h>

// Utility function to reverse a string
void reverse(char *str)
{
    int length = strlen(str);
    for (int i = 0; i < length/2; i++)
    {
        char temp = str[i];
        str[i] = str[length-i-1];
        str[length-i-1] = temp;
    }
}

char retCorrespondingChar(int number)
{
    if (number >= 0 && number <= 9)
        return (char)(number + '0');   // If the remainder is in between 0 to 9
    else
        return (char)(number - 10 + 'A');   // If the remainder is more than 9
}


// Function to convert a given decimal number to base repersentaion
char* itob(char buffer[], int base, int actualNum)
{
    int index = 0;
   
    if(actualNum > 0) {
    // We will dividing input number with the base and taking the remainder
    while (actualNum > 0) // if the enterd number is graeter than 0 then only proceed
    {
        buffer[index++] = retCorrespondingChar(actualNum % base);    // calculating the remainder and getting the char for that remainder
        actualNum /= base;   // Dividing the number with the base
    }
    buffer[index] = '';   // terminating the buffer character
    reverse(buffer);   // We have to reverse the result
}
else
{
  buffer = "0";
}
    return buffer;
}

int main()
{
    int actualNum = 1664957, base = 36;
    char buffer[100];
    printf("Equivalent of %d in base %d is "
           " %s ", actualNum, base, itob(buffer, base, actualNum));
          
actualNum = 29234652, base = 36;
printf("Equivalent of %d in base %d is "
           " %s ", actualNum, base, itob(buffer, base, actualNum));
          
    actualNum = 2018, base = 10;
    printf("Equivalent of %d in base %d is "
           " %s ", actualNum, base, itob(buffer, base, actualNum));
   
   
    actualNum = 127, base = 16;
    printf("Equivalent of %d in base %d is "
           " %s ", actualNum, base, itob(buffer, base, actualNum));
   
    actualNum = -1, base = 10;
    printf("Equivalent of %d in base %d is "
           " %s ", actualNum, base, itob(buffer, base, actualNum));
   
   
    return 0;
}