binary problem - beginner problem
Author Message binary problem - beginner problem

Avoid the use of scanf(). It is much more trouble than it is worth.

#include <stdio.h>

void printBase(long v, long base)
{
static char *digits = "0123456789abcdef";
if(v) {
printBase(v / base,base);
putchar(digits[v % base]);
}

Quote:
}

int main()
{
long x = 64206;
printf("Binary:      ");
printBase(x,2);
printf("\nOctal:       ");
printBase(x,7);
printf("\nDecimal:     ");
printBase(x,10);
printBase(x,16);
return 0;
Quote:
}

--

Paul Lutus
www.arachnoid.com

Quote:

>Hello everyone,
>This is the program I am having problems with What I need to do with it
>is alter it so that it takes a second arguement in the range 2-10 and
>print the number that is its first arguement to the number base given
>by the second arguement. I cannot figure this out, any help would be
>appreciated.

>#include <stdio.h>
>void to_binary(int n);
>int main(void);
>{
> int number;
> printf("Enter an integer (q to quit):\n");
> while (scanf("%d", &number) ==1)
>{
> printf("Binary equivalent: ");
> to_binary(number);
> putchar('\n');
> printf("Enter an integer (q to quit):\n");
> }
>return 0;
>}
>void to_binary(int n)
>{
> int r;
> r = n % 2;
> if (n >=2)
>   to_binary(n / 2);
> putchar('0' + r);
> return;
>}

>Sent via Deja.com http://www.*-*-*.com/
>Share what you know. Learn what you don't.

Sun, 06 Jan 2002 03:00:00 GMT  binary problem - beginner problem
Hello everyone,
This is the program I am having problems with What I need to do with it
is alter it so that it takes a second arguement in the range 2-10 and
print the number that is its first arguement to the number base given
by the second arguement. I cannot figure this out, any help would be
appreciated.

#include <stdio.h>
void to_binary(int n);
int main(void);
{
int number;
printf("Enter an integer (q to quit):\n");
while (scanf("%d", &number) ==1)
{
printf("Binary equivalent: ");
to_binary(number);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
return 0;

Quote:
}

void to_binary(int n)
{
int r;
r = n % 2;
if (n >=2)
to_binary(n / 2);
putchar('0' + r);
return;

Quote:
}

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Mon, 07 Jan 2002 03:00:00 GMT  binary problem - beginner problem
just read another number and store it in "base", then write
a function called "void to_xinary(int n, int base)" which uses
"base" to do calculation instead of 2 in the function to_binary().

Feng Chen

: Hello everyone,
: This is the program I am having problems with What I need to do with it
: is alter it so that it takes a second arguement in the range 2-10 and
: print the number that is its first arguement to the number base given
: by the second arguement. I cannot figure this out, any help would be
: appreciated.

: #include <stdio.h>
: void to_binary(int n);
: int main(void);
: {
:  int number;
:  printf("Enter an integer (q to quit):\n");
:  while (scanf("%d", &number) ==1)
: {
:  printf("Binary equivalent: ");
:  to_binary(number);
:  putchar('\n');
:  printf("Enter an integer (q to quit):\n");
:  }
: return 0;
: }
: void to_binary(int n)
: {
:  int r;
:  r = n % 2;
:  if (n >=2)
:    to_binary(n / 2);
:  putchar('0' + r);
:  return;
: }

: Sent via Deja.com http://www.deja.com/
: Share what you know. Learn what you don't.

Mon, 07 Jan 2002 03:00:00 GMT  binary problem - beginner problem

Quote:

>Hello everyone,
>This is the program I am having problems with What I need to do with it
>is alter it so that it takes a second arguement in the range 2-10 and
>print the number that is its first arguement to the number base given
>by the second arguement. I cannot figure this out, any help would be
>appreciated.

>#include <stdio.h>
>void to_binary(int n);
>int main(void);
>{
> int number;
> printf("Enter an integer (q to quit):\n");
> while (scanf("%d", &number) ==1)
>{
> printf("Binary equivalent: ");
> to_binary(number);
> putchar('\n');
> printf("Enter an integer (q to quit):\n");

Hehe, or anything else that scanf("%d",&n) chokes on :-)

Quote:
> }
>return 0;
>}
>void to_binary(int n)
>{
> int r;
> r = n % 2;
> if (n >=2)
>   to_binary(n / 2);
> putchar('0' + r);
> return;
>}

Wow, recursion :-)

Okay, let's call this second argument b (of base) and the new function
to_base_b(). The only thing you need to do is to replace all 2's by b.
So:

void to_base_b(int n, int b)
{
int r;

r=n%b;
if (n>=b)
to_base_b(n/b,b);
putchar('0'+r);
return;

- Show quoted text -

Quote:
}

Mon, 07 Jan 2002 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages