Question

In C programming: Generalize the to_binary() function of Listing 9.8 (from your textbook) to a to_base_n(number,base)...

In C programming:

Generalize the to_binary() function of Listing 9.8 (from your textbook) to a to_base_n(number,base) function that takes a second argument in the range 2–10. It then should convert (and print) the number that is its first argument to the number base given by the second argument. For example, tobasen(129,8) would display 201, the base-8 equivalent of 129. Test the function in a complete program.

Homework Answers

Answer #1

#include <stdio.h>

#include <string.h>

char reVal(int num)

{

    if (num >= 0 && num <= 9)

        return (char)(num + '0');

    else

        return (char)(num - 10 + 'A');

}

void strev(char *str)

{

    int len = strlen(str);

    int i;

    for (i = 0; i < len / 2; i++)

    {

        char temp = str[i];

        str[i] = str[len - i - 1];

        str[len - i - 1] = temp;

    }

}

char *to_base_n(char res[], int base, int inputNum)

{

    int index = 0;

    while (inputNum > 0)

    {

        res[index++] = reVal(inputNum % base);

        inputNum /= base;

    }

    res[index] = '\0';

    strev(res);

    return res;

}

int main()

{

    int inputNum, base;

    printf("Enter input number: ");

    scanf("%d",&inputNum);

    printf("Enter base: ");

    scanf("%d",&base);

    char res[100];

    printf("Equivalent of %d in base %d is %s\n",inputNum, base, to_base_n(res, base, inputNum));

    return 0;

}

//SAMPLE OUTPUT:

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...
Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes...
Please solve in c++ Split the Number Programming challenge description: You are given a number N...
Please solve in c++ Split the Number Programming challenge description: You are given a number N and a pattern. The pattern consists of lowercase latin letters and one operation "+" or "-". The challenge is to split the number and evaluate it according to this pattern e.g. 1232 ab+cd -> a:1, b:2, c:3, d:2 -> 12+32 -> 44 Input: Your program should read lines from standard input. Each line contains the number and the pattern separated by a single whitespace....
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: a) Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
This is function.py def processString(string): # Implement this function This is # This should print 4.5...
This is function.py def processString(string): # Implement this function This is # This should print 4.5 3.0 processString("An example. Two") # This should print 4.4 5.3 4.5 processString("This is the first sentence. The second sentence starts after the period. Then a final sentence") Download function.py and complete the processString(string) function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per...
c) A function named tallied_data() that takes in a nested list (Use this on the data...
c) A function named tallied_data() that takes in a nested list (Use this on the data from part 1, but it should be able to be used to solve similar problems), indices for two columns and returns a tallied list. The inputs are: i) a nested list, ii) an index for the ‘reference column’/ ‘category’ (col_ref) iii) another index for the column to be tallied (col_tally) iv) this function returns a list of tuples where each element is a tuple...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on using arrays. Instructions Create a program called arrays.c that does the following: 1. Prompt the user for a string (<= 100 characters). (using the attached file: AS06Data.txt) AS06Data.txt file contains the text " Instructions: When your executing program displays "Enter string 1:" starting with T, select the line of text below, copy with ctrl-c, then type ctrl-v THIS IS a test of string 0123456789...
Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in...
Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in an array, then place them proper locations in the array. Follow these steps: 1. Create an array with 11 integers, which will be randomly selected from the range of 10 to 100. Only random numbers between 10 and 100 are allowed in the array. Print the items of the array on screen as one line. 2. Develop a function that takes the array as...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT