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...
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...
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...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
An advantage of programming is the ability to perform millions of calculations and dump it to...
An advantage of programming is the ability to perform millions of calculations and dump it to a file for plotting. This can be extremely useful for engineers if they are simulating or characterizing a system. Below you will calculate data, write it to a file, and plot the contents in excel. a) Create a csv file for writing. This can be done by creating a file with the open method as we have done in class but with a *.csv...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT