2
\$\begingroup\$

I started to learn C a week ago and this is my culminating program of what I've learned but I could use a few pointers.

#include <math.h>
#include <stdio.h>
void binary(int n){
    int x = 0, m = 0, z = n;
    while(n >= 1){
            ++x;
            n /= 10;
            }
    n = z;
    int i[x];
    x = 0;
    while (n != 0){
            ++x;
            i[x] = n % 10;
            n /= 10;
            }
    while(x >= 0){
            if (i[x] == 1){
                    if (x == 1)
                            ++m;
                    else m += pow(2,x - 1);
            }
            --x;
    }
    printf("%d", m);
}
void convert(int n){
    int i[16];
    int j, k, p;
    for(k = 15; k >= 0; --k){
            i[k] = n % 2;
            n /= 2;
            }
    int *a, *b;
    a = &i[0];
    b = &i[16];
    for( ;*a == 0; ++a)
            ;
    while(a != b){
            printf("%d", *a);
            ++a;
    }
}
int main()
{
    int n;
    char m;
    printf("\nType b for binary to decimal conversion or d for decimal to binary conversion (max 65535)\n");
    m = getchar();
    printf("\nPrint number to be converted\n");
    scanf("%d", &n);
    if (m == 'b'){
            printf("%d converted to a decimal number is ", n);
            binary(n);
    }
            else{
                    printf("%d converted to a binary number is ", n);
                    convert(n);
            }
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

two sets of comments. IS the functionality sensible or not? Is the code good / open to improvement etc?

Firstly the functionality

Its very odd to put 101 into an int and say that its 5. The number 5 in an int is always 5, it has various string representations depending on the base used to display it. I would have much preferred seeing 2 functions:

  • one that accepts a string is a given base and gives that returns the valur of it as an int
  • one that takes an int and displays it as a string using a given base

In effect you would be recoding atoi and itoa

Now about the implementation of the features you implemented

First the code works, ++ for that. Nicely split into functions etc. ++

No Comments -- Gotta have at least some.

commonly accepted c practice is to define and initialize variables on first use. (Old compilers required all variables to be declared at start of scope, not any more)

for exmaple

   char m;
    printf("\nType b for binary to decimal conversion or d for decimal to binary conversion (max 65535)\n");
    m = getchar();

is better as

printf("\nType b for binary to decimal conversion or d for decimal to binary conversion (max 65535)\n");
char m = getchar();

You alignment is somewhat funky. Commonly accepted c styles are

while(n >= 1)
{
    ++x;
    n /= 10;
}

or

while(n >= 1){
   ++x;
   n /= 10;
}

your should '\n' on the last line of your output, its considered polite.

I would have made the conversion functions return something rather than having them do the printing. Imagine you needed to do something else with the converted value

There is no error checking. Entering 42 and asking for a 'b' conversion returns 0. THis is in fact an error, you should say so.

n,z,x are not obvious names. I see that z is a copy of n so you can restore it later, call it saven or something like that

Now for the actual logic. Given that you are doing such a strange thing I find it hard to really comment on it. But its not very clear. Taking convert, I would have converted the input to a string , base 2, then scanned the string back as decimal.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.