C Programming functions

Joined
Dec 3, 2021
Messages
2
Reaction score
0
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.
 
Joined
Dec 3, 2021
Messages
2
Reaction score
0
Could do the following function to get a number and base from a user, any other help for the rest:

#include<stdio.h>

int getnumber_and_base();

int main() {

int input = 0;
//call a function to input number from key board
input = getnumber_and_base();

//when input is not in the range of 1 to 9,print error message
while (!((input <= 10) && (input >= 2))) {
printf("[ERROR] The base you entered is out of range\n");

input = getnumber_and_base();
}
//this function is repeated until a valid input is given by user.
printf("The base you entered is correct %d\n", input);
return 0;
}

//this function returns the number given by user
int getnumber_and_base() {
int num,base;
//asks user for a input in given range
printf(" Enter a number ");scanf("%d", &num);
printf(" Enter a base between 2 to 10 ");scanf("%d", &base);
return base;
}
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Hello! Your user input function is only returning one value (the base), so you'll want to pass in pointers to it so you can get more outputs. I'd recommend the following. I also did the first function, just for kicks, to show you how base conversion tend to work (looping and dividing, more often than not). I'd recommend checking out RapidTables to test and get a feel for it, as well. That should get you through the second function. The third is a pretty straight forward string reversal; you just work inwards from both ends, swapping each element and moving one character inward on each. The fourth function isn't so bad, you just need to figure out how to convert each character to it's digits; try subtracting '0' from each and you'll get an offset (NB, this only works because you don't need to go above base10). Give them a shot and let me know if something's not clicking and we can try to work through algorithms in more detail.

C:
#include <stdio.h>

void getnumber_and_base(int *num, int *base);
int get_num_digits(int num, int base);

int main()
{

        int num = 0;
        int base = 0;
        //call a function to input number from key board
        getnumber_and_base(&num, &base);

        //this function is repeated until a valid input is given by user.
        printf("The base you entered is correct %d base %d\n", num, base);
        printf("num digits: %d\n", get_num_digits(num, base));
        return 0;
}

//this function returns the number given by user
void getnumber_and_base(int *num, int *base)
{
        *base = 0;
        //asks user for a input in given range
        printf(" Enter a number ");
        scanf("%d", num);
        while (*base == 0){
                printf(" Enter a base between 2 to 10 ");
                scanf("%d", base);
                if (*base < 2 || *base > 10){
                        *base = 0;
                }
        }
}

int get_num_digits(int num, int base){
        int digits = 1;
        while (num > base){
                num /= base;
                digits++;
        }
        return digits;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top