I need help with decimal to binary Algorithm

This is a discussion on I need help with decimal to binary Algorithm within the C Programming forums, part of the General Programming Boards category; Hi there have been working on a project where I am asked to create a program that can accept input ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    I need help with decimal to binary Algorithm

    Hi there have been working on a project where I am asked to create a program that can accept input and give output.

    I need to be able to reconise input as binary or decimal or error..
    then out put the answer.

    If input is Decimal then output is Binary equivalent.
    If input is Binary then output is Decimal equivalent.

    I have managed everything Up till the Decimal to Binary.. I Know how to do this on paper however I am at a point where I just cannot get the correct answer for this conversion.
    Any Suggestions or nudge in the right direction would be greatly appreciated..

    Here is my code so far... sorry its a little messy ill get round to cleaning it up after i have figured out this issue.. lol

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    
    char s[80];
    char i[80];
    char type=0;
    int len;
    int two;
    int counter=1 ;
    int sum = 0;
    int index=0;
    
    	
    	printf ("Type in a number: ");
    	gets (s);
    	if (s[0] == '0'){
    		type = 'b';
    	} else if (s[0]>='2' && s[0]<='9'){
    		type = 'd';
    	} else if (s[0] == '1') {
    		type = '\0';
    	} else {
    		type = 'i';
    	}
    	
       if (type != 'i') {                  
    		while (s[counter]!='\0'){
    			if (s[counter]>='2' && s[counter]<='9'){
    				type = 'd';
    			}else {
    				if (s[counter]!='0' && s[counter]!='1') {
    					type = 'i';
    					break;
    				} else {
    					if (type != 'b') {
    						type = '\0';
    					}
    				}
    			}
    			
    		
    			if (type=='d' && s[0]=='0'){
    				type = 'i';
    				break;
    			}
    			counter++;
    		}
    
    	}if (type=='\0'){
    		printf ("is this number decimal (d) or binary (b)");
    		gets (i);
    		type = i[0];
    	}
    	// print number into variable, if n > 255 or n < 0 type == 'i'
    	
    	if(type=='b') {
    		len = strlen (s)-1;
    		two = 1;
    		
    		while (len >= 0) {
    			if (s[len] == '1') {
    				sum += two;
    			}
    			two = two*2;
    			len--;
    		}
    		printf("Converting binary to decimal. Answer is: %d\n", sum);
    	} else if(type=='d') {
    		/*this is where the conversion for decimal to binary will happen..
                    when figured out... lol  */
    		}
    	
    		printf("number: %d", sum);
    		
    	} else if(type=='i') {
    		printf("That is an invalid number!");
    	}
    
    	return 0;
    }

    Again any help would be appreciated.

    regards

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,453
    1. do not use gets - read FAQ
    2. make small functions dedicated to one purpose like
    int decimal2binary(int src, char* output, size_t bufLen);

    Structure your code properly and add comments about what each part of the code should do like
    Code:
    int main(void)
    {
      /* going to ask type of conversion */
       int choice = menu();
     
       switch(choice)
       {
        case 0: /* decimal2binary */
          {
             char buffer[20];
             /* ask user input */
             int src = getDecNumber();
             /* perform conversion */
             int res = decimal2binary(src,buffer,sizeof buffer);
             if(res == 0) /* success */
             {
                /* output result */
    
    etc
    this way you can post only the part of the code that is problematic and ask concrete question.
    "Doesn't work" is too general
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    51

    I was asking for help with the conversion of

    decimal to binary.. I have left a comment in the code as to the area i am wanting to do it.

    Regarding gets() sorry i didnt explain but that part was required as outlined in the brief i am using.

    thanks for the response! will take all your points on board!

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,453
    I have left a comment in the code as to the area i am wanting to do it.
    The problem with your outline - it is very hard to find what is the input, what is the input format, and what should be the output

    When you break code in functions - you can describe exactly what is the input parameter (with type and example of format) and what is the expected output
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Your code is rather messy to read and the use of that gets function further complicates things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Damn binary to decimal algorithm
    By xmltorrent in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 04:30 PM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21