W
whisper
Hi, I am learning how to program in C. Please help me by criticizing
anything
in the code below I wrote that converts int to binary and back. Is
there
something wrong that you see in the coding style, programming errors.
Any pointers would be greatly appreciated. Thanks!
When writing functions with more than one word for name, what is the
standard
practice - to use __ to join them or make them into one word ? For
example,
is binary_to_int more acceptable than BinaryToInt as name. I realize it
makes
no difference in terms of programming, but is there anything like a
recommended
style ?
Do comments have to be more informative ?
#include <stdio.h>
#include <math.h>
int Log2(int n) {
/* return the number of bits needed to store n in binary.
Is there a native C function to do this ? */
return (int) log((double) n) * (int) log(10) +1;
}
int *binary(int m) {
/* convert integer m into binary, return the array of bits
- least significant bit first */
int *binrep;
int numbits, i, indx;
numbits=Log2(m);
binrep=(int *) malloc (numbits*sizeof(int));
if(binrep==NULL) error("Could not allocate memory");
indx=0;
for(i=0;i<numbits;i++)
binrep=0;
while(m>0) {
binrep[indx++]=m%2;
m=m/2;
}
return binrep;
}
int btoint(int *binrep, int maxbits) {
/* convert binary rep to integer, binary rep
has least significant bit first */
int i,num=0;
int pow=1;
for(i=0;i<maxbits;i++) {
num=num+binrep*pow;
pow=2*pow;
}
return num;
}
int main() {
int i,inp, numbits;
int *binvect;
printf("Enter input: ");
scanf("%d", &inp);
numbits=Log2(inp);
binvect=binary(inp);
for(i=0;i<numbits;i++)
printf("%d ", binvect);
printf("\n");
printf("Input Number was %d\n", btoint(binvect, numbits));
}
anything
in the code below I wrote that converts int to binary and back. Is
there
something wrong that you see in the coding style, programming errors.
Any pointers would be greatly appreciated. Thanks!
When writing functions with more than one word for name, what is the
standard
practice - to use __ to join them or make them into one word ? For
example,
is binary_to_int more acceptable than BinaryToInt as name. I realize it
makes
no difference in terms of programming, but is there anything like a
recommended
style ?
Do comments have to be more informative ?
#include <stdio.h>
#include <math.h>
int Log2(int n) {
/* return the number of bits needed to store n in binary.
Is there a native C function to do this ? */
return (int) log((double) n) * (int) log(10) +1;
}
int *binary(int m) {
/* convert integer m into binary, return the array of bits
- least significant bit first */
int *binrep;
int numbits, i, indx;
numbits=Log2(m);
binrep=(int *) malloc (numbits*sizeof(int));
if(binrep==NULL) error("Could not allocate memory");
indx=0;
for(i=0;i<numbits;i++)
binrep=0;
while(m>0) {
binrep[indx++]=m%2;
m=m/2;
}
return binrep;
}
int btoint(int *binrep, int maxbits) {
/* convert binary rep to integer, binary rep
has least significant bit first */
int i,num=0;
int pow=1;
for(i=0;i<maxbits;i++) {
num=num+binrep*pow;
pow=2*pow;
}
return num;
}
int main() {
int i,inp, numbits;
int *binvect;
printf("Enter input: ");
scanf("%d", &inp);
numbits=Log2(inp);
binvect=binary(inp);
for(i=0;i<numbits;i++)
printf("%d ", binvect);
printf("\n");
printf("Input Number was %d\n", btoint(binvect, numbits));
}