Another CS question. (C)

DAM

Diamond Member
Jan 10, 2000
6,102
1
76
while (number != 0){
   i = number%base;
number = number/base;
printf("%.2d ", i);
}


if number = 15 and base = 6 it gives me a 32, when the real answer is 23, how can i reverse the order in which it prints out?


Any help will be appreciated greatly.



dam()
 

poop

Senior member
Oct 21, 1999
827
0
0
Do not use a while, do it recursively. Try this:

poop(int number)
{
int i = number/base;
if(i)
{
poop(i)
}
printf("%.2d",number%base);
}

That is the most effecient way. You can also use a stack:

while(number)
{
push(number%base);
number = number/base;
i++
}
while(i--)
{
printf("%.2d", pull() );
}
 

DAM

Diamond Member
Jan 10, 2000
6,102
1
76
what is poop? what do i initialice it as or define it as?





thanks man



dam()
 

poop

Senior member
Oct 21, 1999
827
0
0
poop is your function. It is all pretty much right there. The function will call itself again and again until it is done dividing number by base. Then, it will print, from last to first, the results of number mod base.

The second example was just a suggestion as to how you can accomplish the same thing with a stack.

I am willing to help out, but I won't write your program for you. You wouldn't learn much if I did that. :)
 

GoldenGuppy

Diamond Member
Feb 4, 2000
3,494
0
0
Recursive would the yield the properties with much less of a mess, don't forget the identities though.