Like Tree3Likes
  • 1 Post By Adak
  • 1 Post By jimblumberg
  • 1 Post By Malcolm McLean

algorithm to code.

This is a discussion on algorithm to code. within the C Programming forums, part of the General Programming Boards category; hey...I got this algorithm of conversion and now I'm stuck at how to code it. Can anyone please help. "Algorithm ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    south africa
    Posts
    7

    algorithm to code.

    hey...I got this algorithm of conversion and now I'm stuck at how to code it. Can anyone please help.


    "Algorithm to Convert From any Base to Base 10 Decimal.

    Let 'n' be the number of digits in the number. For example, 104 has 3 digits, so 'n'=3.
    Let 'b' be the base of the number. For example, 104 is decimal so 'b' = 10.
    Let 's' be a running total, initially 0.
    For each digit in the number, working left to right do:
    Subtract 1 from 'n'.
    Multiply the digit times b^n and add it to 's'.
    When done with all the digits in the number, the decimal value should be 's' .

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's exactly what the number line means.

    But that won't help you start your program, I guess. Run through the C tutorial (click the tab for it at the top of this page), and you'll know enough to get at least a minimal start.

    The start of a program is almost always 100% up to YOU, on the programming forums - you can (and must) be able to do that, at least.

    We help YOU, we don't replace your work with ours, upon request.
    NBK Rules likes this.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    south africa
    Posts
    7
    I did start the coding but I'm stuck with that part.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    I did start the coding but I'm stuck with that part.
    Really? Maybe it would help if you showed what you have.

    Jim
    NBK Rules likes this.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    You'll get very confused if you talk about number bases in C. Internally all numbers are stored as binary patterns of bits. Then in C source numbers are decimal by default, of hexadecimal or octal if given the right prefixes.
    But C programs can handle numbers in any base, represented as ascii strings or, if you want a huge base, arrays of integers. Not atomic operations will work on such numbers, you have to write your own functions to add, subtract, print them out, etc.
    Normally no-one does that, and everything is converted to the internal binary representation as soon as possible.

    However mathematically there's nothing special about base ten. It's certainly possible to write a function to convert from an arbitrary base to another arbitrary base.
    mast3124mind likes this.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    south africa
    Posts
    7
    there is the code...I only get wrong values when running it.


    Code:
    int main()
    {
         
         char s[20];
         int base;
        int r,index,n,p,b=1,sum=0; /* n is the number of digits in the value to be converted */
        
        printf("enter the number and base: ");
        scanf("%s %d",s,&base);
        
        for(n=strlen(s)-1;n>=0;n--)
        {
            for(index=1; index<=strlen(s); index++)
            sum += s[index] * pow(base, n);
        }
        printf("the number in base ten is %d",sum);
        printf("\n");
        system("pause");
    }

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    Here is a function I wrote to essentially do the opposite of what you are wanting.

    Code:
    void itob( int n , char * string , int b ){
        int i;
        for( i = 0;n > 0; ++i ){
            /* nums 0-9 have their respective ascii values inserted into string.
             * higher nums have uppercase letters, starting with 'A' for num 10 */
    
    
            string[ i ] = n % b + ( ( n % b <= 9 ) ? '0' : '7' );
    
    
            /* adding '0' to the number turns it from a number 0-9 into the ascii version 
               adding '7' turns the number into the alphabetical equivilant
            */
            n /= b;
        }
        string[ i ] = '\0';
        reverse( string );   // reverse() reverse's the order of the string.... durr
        return;
    }
    Maybe it can help you by 'reverse-engineering' it?
    Last edited by jwroblewski44; 03-27-2013 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Algorithm code problem
    By Djur0 in forum C Programming
    Replies: 11
    Last Post: 01-22-2012, 01:11 PM
  2. Replies: 55
    Last Post: 12-14-2011, 02:58 AM
  3. Understanding Genetic Algorithm Code!!
    By L3munoz in forum C Programming
    Replies: 2
    Last Post: 10-25-2010, 06:53 AM
  4. need source code for Cohen sutherland algorithm
    By specks5317 in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2002, 08:24 PM

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