wheres the error?

Discussion in 'Mac Programming' started by dukebound85, Oct 11, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    Hi all, I am trying to convert decimal to binary in c

    the max decimal number is 10000 so any integer from 0 to 10000 can be entered

    10000 in binary is 14 digits long

    my function is as follows

    int fun(int num)

    {
    int remain;
    int x=0;
    int sum=0;
    int binary=0

    while(x<13)
    {
    remain=10*(num%2); //so it will be in factors of 10
    num=num/2; // keeps diving number by 2

    if(num!=0)
    {
    sum=pow(remain,x)+sum;
    }
    x=x+1

    }

    binary=sum;
    return binary;

    }

    however this only prints of sum =1 no matter what number is taken in


    any ideas? I am trying to get it so if the remainder is 1 I multiply by 10 and raise it to the power of x, then add it to the sum

    thanks for any insight. you guys are such a great group!
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    The line "return binary;" is being called on the very first time the loop goes through, meaning, the loop only goes through once and then the function ends and returns.

    Second of all, usually binary numbers are represented as strings when converting from a decimal to a binary. You're storing it in an integer, but you probably want to store it in a string (char array).

    Third, x never changes, so if you remove the return line, it will be an infinite loop.
     
  3. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #3

    whoops i mistyped my code when typing it in here. should be correct now (as in the way i have it typed currently)


    once again thanks for any help you can give
     
  4. Flynnstone macrumors 65816

    Flynnstone

    Joined:
    Feb 25, 2003
    Location:
    Cold beer land
    #4
    Can you give an example of output?

    257 decimal = 0x102 hex = 0001 0000 0010 binary
    Do you want to output a string?
     
  5. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #5

    say i enter 13,

    the output is 100

    i want to output as an integer not a string
     
  6. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    Why would you want the result as an integer? What are you going to do with a binary representation that requires it to be an integer? Maybe you have a good reason, but I agree with kainjow, it seems odd.

    Anyway, it could be done more efficiently using bit shifting and masking. Here is a method I wrote to do this with an int:
    Code:
    void printBinary(int input, int maxDigits, int perRun) {
        int i = 0;
        unsigned mask = 0;
        
        for (i = 0; i < maxDigits; i++) {
            /* insert a space separator every perRun digits */
            if (perRun > 0 && (i - maxDigits) % perRun == 0) {
                printf(" ");
            }
            mask = 0x1 << (maxDigits - i);
            if (input & mask) {
                printf("1");
            } else {
                printf("0");
            }
        }
        printf("\n");
    }
    (That has some embellishments that could be taken out to make it smaller.)

    ps. You are still missing some semicolons in your code at statement endings.
     
  8. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #8
    well its for a class , we can only use if,elses, whiles, and fors. the number has to be stored as an int

    i have been trying to figure this out for seriously 8 hrs now and yea not fun anymore lol

    any suggestions? was i even on the right track. i am new to programming so this is all new to me

    once again thanks
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Storing it in an int is definitely a tiny bit more challenging.

    Here's a little app that does it:
    Code:
    #include <stdio.h>
    
    int dec2bin(int num)
    {
    	int bin = 0, k = 1;
    	
    	while (num)
    	{
    		bin += (num % 2) * k;
    		k *= 10;
    		num /= 2;
    	}
    	
    	return bin;
    }
    
    int main()
    {
    	int num = 0;
    	
    	printf("Enter a number: ");
    	scanf("%d", &num);
    	
    	printf("%d in binary is %d\n", num, dec2bin(num));
    	
    	return 0;
    }
     
  10. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #10
    thanks, i am able to follow that except for the part involving k.is k just putting the remainder in the correct decimal place?

    just so i understand this while(num) evaluates the loop while a number is present (non zero) correct?

    once again thanks
     
  11. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #11
    Well, num % 2 is the 1 or 0 of the binary number. It starts with the least significant bit, and ends with the most significant bit. So if you were to print it out at each iteration of the loop, it would be backwards (e.g. 12 would output 0011). But we want to store it as a number, not a string. So k represents the decimal place, starting with 1, then 10, then 100, etc, multiplied by the current bit (1 or 0), and then added to the current number.

    Yes, the loop continues while num evaluates to true, which is when it's non-zero.
     

Share This Page