Hi,
How can I write the following functions in C:
1. A function which returns the number of digits necessary to convert a number to its
base n representation (assume n is in the range 2 - 10). For example, to represent 20
in base 2, you would need 5 digits: 10100.
2. A function which calculates the base n representation of a number, with n varying
from 2-10. For example, 20 in base 2 is 10100, in base 3 is 202, and of course in
base 10 is 20.
3. A function which reverses the elements in a character array. For example, if I
declared a character array as char test_arr[5] = {‘1’,’1’,’0’,’0’,’0’}, the function reverse_char_array would change the order of the elements in test_arr to {‘0’,’0’,’0’,’1’,’1’}. This could be done by writing a swap char_array_elements function, which swaps the elements at two given indices of the character array.
4. A function to print the base n representation of a number given the character array. For example if we declared char test_arr[5] = {‘1’,’1’,’0’,’0’,’0’} and passed it into the function, 11000 would be printed to the terminal.
5. A main function to ask the user for a number and a base and prints a number to that base. If the base is not in the region of 2 - 10, keep asking the user to input a valid number.
How can I write the following functions in C:
1. A function which returns the number of digits necessary to convert a number to its
base n representation (assume n is in the range 2 - 10). For example, to represent 20
in base 2, you would need 5 digits: 10100.
2. A function which calculates the base n representation of a number, with n varying
from 2-10. For example, 20 in base 2 is 10100, in base 3 is 202, and of course in
base 10 is 20.
3. A function which reverses the elements in a character array. For example, if I
declared a character array as char test_arr[5] = {‘1’,’1’,’0’,’0’,’0’}, the function reverse_char_array would change the order of the elements in test_arr to {‘0’,’0’,’0’,’1’,’1’}. This could be done by writing a swap char_array_elements function, which swaps the elements at two given indices of the character array.
4. A function to print the base n representation of a number given the character array. For example if we declared char test_arr[5] = {‘1’,’1’,’0’,’0’,’0’} and passed it into the function, 11000 would be printed to the terminal.
5. A main function to ask the user for a number and a base and prints a number to that base. If the base is not in the region of 2 - 10, keep asking the user to input a valid number.