473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Function to recursively change base

I'm trying to "Write a function itob(n, s, b) that converts the
integer n into a base b character represeantation in the string s. In
particular, itob(n, s, 16) formats n as a hexadecimal integre in s.
itob must be recursive. " It's #3-5 in the C Programming language by
KandR. I also made a main to test it:

void itob(int n, char s[], int b);
main ()
{
/*void itob(int n, char s[], int b);*/
int b[]={2, 6, 8, 10, 16, 32};
int p[]={2, 6, 8, 10, 16, 32, 1239757};
char s[BUFSIZ];

int i, j;

for(i=0; i<7; i++){
for (j=0; j<6; j++){
itob(p[i], s, b[j]);
printf("s is %d\n",s);
printf("*************\n");
}
}
}

void itob(int n, char s[], int b)
{
int i, sign;
i=0;
printf("number is %d\n",n);
printf("base is %d\n",b);

if ((sign = n) <0 )
n = -n;

if (n/b>0)
itob(n/b,s,b);
s[i++]=n%b;
s[i]='\0';
printf("grrrr s[i++]is %d\n",s[i]);
}
But this is the sort of output I get (just a very small sample)

*************
number is 1239757
base is 16
number is 77484
base is 16
number is 4842
base is 16
number is 302
base is 16
number is 18
base is 16
number is 1
base is 16
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
s is 134510020
*************
number is 1239757
base is 32
number is 38742
base is 32
number is 1210
base is 32
number is 37
base is 32
number is 1
base is 32
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
s is 134510020

I just don't enjoy recursion :,[

Feb 21 '07 #1
4 3393
If the problem is to do with the "grrrr .... is 0" appearing in the
output, it is because in your recursive function, it is outside of
your if statement. When the recursion comes back up, it is going to
execute it.
On Feb 21, 10:33 am, ComaGr...@gmail.com wrote:
I'm trying to "Write a function itob(n, s, b) that converts the
integer n into a base b character represeantation in the string s. In
particular, itob(n, s, 16) formats n as a hexadecimal integre in s.
itob must be recursive. " It's #3-5 in the C Programming language by
KandR. I also made a main to test it:

void itob(int n, char s[], int b);
main ()
{
/*void itob(int n, char s[], int b);*/
int b[]={2, 6, 8, 10, 16, 32};
int p[]={2, 6, 8, 10, 16, 32, 1239757};
char s[BUFSIZ];

int i, j;

for(i=0; i<7; i++){
for (j=0; j<6; j++){
itob(p[i], s, b[j]);
printf("s is %d\n",s);
printf("*************\n");
}
}

}

void itob(int n, char s[], int b)
{
int i, sign;
i=0;
printf("number is %d\n",n);
printf("base is %d\n",b);

if ((sign = n) <0 )
n = -n;

if (n/b>0)
itob(n/b,s,b);
s[i++]=n%b;
s[i]='\0';
printf("grrrr s[i++]is %d\n",s[i]);

}

But this is the sort of output I get (just a very small sample)

*************
number is 1239757
base is 16
number is 77484
base is 16
number is 4842
base is 16
number is 302
base is 16
number is 18
base is 16
number is 1
base is 16
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
s is 134510020
*************
number is 1239757
base is 32
number is 38742
base is 32
number is 1210
base is 32
number is 37
base is 32
number is 1
base is 32
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
s is 134510020

I just don't enjoy recursion :,[

Feb 21 '07 #2
On Feb 20, 8:42 pm, "Klarth" <kah....@gmail.comwrote:
If the problem is to do with the "grrrr .... is 0" appearing in the
output, it is because in your recursive function, it is outside of
your if statement. When the recursion comes back up, it is going to
execute it.

Sorry I should have been more specific. I'm confused because I keep
getting s is 134510020
and the grr thing was just to see what was going in (or not going in)
my array which is 0 for some reason when I'd like it to be number%base.

Feb 21 '07 #3
On Feb 20, 5:33 pm, ComaGr...@gmail.com wrote:
I'm trying to "Write a function itob(n, s, b) that converts the
integer n into a base b character represeantation in the string s. In
particular, itob(n, s, 16) formats n as a hexadecimal integre in s.
itob must be recursive. " It's #3-5 in the C Programming language by
KandR. I also made a main to test it:

void itob(int n, char s[], int b);
main ()
{
/*void itob(int n, char s[], int b);*/
int b[]={2, 6, 8, 10, 16, 32};
int p[]={2, 6, 8, 10, 16, 32, 1239757};
char s[BUFSIZ];

int i, j;

for(i=0; i<7; i++){
for (j=0; j<6; j++){
itob(p[i], s, b[j]);
printf("s is %d\n",s);
printf("*************\n");
}
}

}

void itob(int n, char s[], int b)
{
int i, sign;
i=0;
printf("number is %d\n",n);
printf("base is %d\n",b);

if ((sign = n) <0 )
n = -n;

if (n/b>0)
itob(n/b,s,b);
s[i++]=n%b;
s[i]='\0';
printf("grrrr s[i++]is %d\n",s[i]);

}

But this is the sort of output I get (just a very small sample)

*************
number is 1239757
base is 16
number is 77484
base is 16
number is 4842
base is 16
number is 302
base is 16
number is 18
base is 16
number is 1
base is 16
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
s is 134510020
*************
number is 1239757
base is 32
number is 38742
base is 32
number is 1210
base is 32
number is 37
base is 32
number is 1
base is 32
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
grrrr s[i++]is 0
s is 134510020

I just don't enjoy recursion :,[
I wrote a function that does something similar. But I decided no
sense hauling that bulky string all over the place. And I restricted
myself to using only putchar() as a library function call within the
bit of stuff that does the actual work. Of course, it would not
satisfy an assignment that asked you to stuff the answer into a
string. P.S. If you use 64 bit integers, then your string must be a
minimum of 65 characters in length. But I am not sure I see the
efficacy of making it BUFSIZ characters. So I have an assignment for
you. Comment this code and tell me what each line of it is doing.
After you have done that, it will be easy for you to write code that
does exactly what you want.

#include <stdio.h>
#include <assert.h>
#include <time.h>

static const char symbol_table[] =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z',
};

void emit_digits(unsigned long long whomper, unsigned base)
{
unsigned long long digit = whomper % base;
assert(base <= 36); /* Else, you're going to need a new symbol
set */
assert(base >= 2); /* Else you'll have a heck of a long wait */

whomper /= base;
if (whomper)
emit_digits(whomper, base);
putchar(symbol_table[digit]);
}

void change_base(unsigned long long whomper, unsigned base)
{
emit_digits(whomper, base);
putchar('\n');
}

#ifdef UNIT_TEST
int main(void)
{
unsigned long long two_to_the_31 = 2147483648;
unsigned long long some_other_thing;
unsigned base;
size_t i;
unsigned j;
change_base(two_to_the_31, 2);
change_base(two_to_the_31, 16);
change_base(two_to_the_31, 32);
putchar('\n');
srand((unsigned) time(NULL));
for (i = 1; i < 10; i++) {
some_other_thing = rand();
some_other_thing *= rand();
for (j = 2; j <= 36; j++) {
change_base(some_other_thing, j);
}
putchar('\n');
}
putchar('\n');
return 0;
}
#endif
Feb 21 '07 #4
On Feb 20, 5:33 pm, ComaGr...@gmail.com wrote:
I'm trying to "Write a function itob(n, s, b) that converts the
integer n into a base b character represeantation in the string s. In
particular, itob(n, s, 16) formats n as a hexadecimal integre in s.
itob must be recursive. " It's #3-5 in the C Programming language by
KandR. I also made a main to test it:

void itob(int n, char s[], int b);
main ()
{
/*void itob(int n, char s[], int b);*/
int b[]={2, 6, 8, 10, 16, 32};
int p[]={2, 6, 8, 10, 16, 32, 1239757};
char s[BUFSIZ];
Comment the following out
>
int i, j;

for(i=0; i<7; i++){
for (j=0; j<6; j++){
itob(p[i], s, b[j]);
printf("s is %d\n",s);
printf("*************\n");
}
}
Replace with:
itob(3, s, 16);
printf("s is %d\n", s);
}

void itob(int n, char s[], int b)
{
int i, sign;
i=0;
printf("number is %d\n",n);
printf("base is %d\n",b);

if ((sign = n) <0 )
n = -n;

if (n/b>0)
itob(n/b,s,b);
s[i++]=n%b;
s[i]='\0';
Replace
printf("grrrr s[i++]is %d\n",s[i]);
with

printf("grrrr i is %d and s[i]is %d\n", i, s[i]);
>
}
Then figure out what should be happening, and what actually is. (You
may need some more printfs, or you may need a debugger, or whatever.)
Fix problems.

Then replace
itob(3, s, 16);
with
itob(12, s, 16);

Figure out what should be happening, and what actually is. Fix
problems.

Then with
itob(20, s, 16);
Figure out what should be happening, and what actually is. Fix
problems.

At this point, comment back in your original code, and it should
work. If not, find the smallest number that doesn't work and put in a
single line as I've shown, then track it down.

Michael

Feb 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
1
by: cognominal | last post by:
I am using fifrefox 1.5.0.2 and I have modified the javascript shell to deal with e4x. Below is a session. I would have expected <toto/>.nodeKind to be of type "function". Is this right? Is...
8
by: Scott Sauyet | last post by:
I found myself needing to find my way recursively through a document in XSLT, finding the dependencies one element had on others, and including only those in an initial set, their dependencies, the...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.