C programming convertions...need immediate help!!!
hey there fellow TA's this is a question for the TA programmers that we have out there...
i have an assignment where i need to convert interger to 32-bit binary, a Fibonacci generator & HTML Color Converter..
the thing is i have no idea how to write the code to convert ill have a look also at some web-sites, maybe they have can help...in the mean time, would you guys help me out plz
btw...the convertions must be done in function format, not basic logic
thnx
the first and the last can easilly be... um... used as one.. I mean, all you need is to write a base convertion algorithm, (10 -> 2 in first case, 10 -> 16 in the second case).. plus you need a color table for the second one.. basic.
as fibonaci sequence, it's umm.. a 5 line recursive method... huk huk
But since it's in C, you will prolly need extra 2-3 lines for variable declaration in the beggining of the method.. )
moo?
___________________
Oct-17-2002 20:39
bluE_Neon
Res Publica
Registered: May 2001
Location: Warsaw
oki i know wat you mean, but i need the code itself not explanation...im having a problem coding the convertions, thats wat i need
Registered: Oct 2000
Location: San Francisco, California
ok.. conversion to binary..
destination is an array which will contain the binary representation WITH leading zeroes.
void toBinary(int number, char*destination){
int cur=1;
int i;
for (i=0;i<32;i++){
if ((number&cur)!=0){
destination[31-i]=1;
}else {
destination[31-i]=0;
}
cur = cur<<1
}
destination[32]=0;
}
I think it works, didn't test though.. it's pretty fast, no divisions at all
Registered: Oct 2000
Location: San Francisco, California
fibonacci..
int fibonacci(int n){
int a=1, b=1(or is it 2??), temp;
int i;
if (n==1) return 1;
if (n==2) return 1; //or is 2nd fibonacci number 2 ? i forget
for (i=3;i<=n;i++){
temp=a+b;
a=b;
b=temp;
}
return temp;
}