Thread: converting to base 10

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    converting to base 10

    Hi,

    I'm currently writing a program that takes a number (n) and a base (b) between [1, 10] from the user's input and convert it into base 10. However, I have to make sure that all the digits of n has to be between [0, b-1] with the exception of n being 1. For example:

    n = 012 and b =3, output is 5
    n = 00110 and b=2, output is 6
    n = 00110 and b=1, output is 2

    for base (b) = 1, the current program cannot check to see if the digits of 'n' is greater than 1 or not because of this code that i use to check it: output[index] = n%b. For example:

    n=23 and b = 2 : output[index] = 23%2 = 3 ; and 3 is greater than base 2
    BUT if n = 23 and b =1; ouput[index] = 23%1 = 0; and I CANNOT TAKE THE 3 to compare to base 1 so that 3 is greater than 1.

    Please help me! The following is my code:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
      int n, b;
      int output[64];
      int index = 0;
    
      printf("Enter base(b) between [1,10], and a number(n) between [0,b-1] in this format 'n b': ");
      scanf("%d %d", &n, &b);
    
      printf("n is: %d\n", n);   //Test
      printf("b is: %d\n", b);   //TEST
    
      if ((b < 1) || (b > 10))
      {
        printf("Your base is not between 1 and 10");
        return 0;
      }
    
      if (n < 0)
      {
        printf("n must be positive");
        return 0;
      }
    
    
      while (n != 0)
      {
        output[index] = n % b;
    
        if ((output[index] >= b) && (output[index] != 1))  //TROUBLE AREA RIGHT HERE
        {
          printf("one of the digits is greater than the base");
          return 0;
        }
    
        n = n/b;
        ++index;
        printf("number to convert: %d\n", n);
      }
      return 0;
    }
    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the if() inside the while loop makes no sense, since it can never be true. You've just done n&#37;b, so output[index] >= b will always be false.

    Also, what is the other printf() supposed to do?
    Printing output[index] would tell you more IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Sorry, the while loop is supposed to look like this:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
      int n, b;
      int output[64];
      int index = 0;
    
      printf("Enter base(b) between [1,10], and a number(n) between [0,b-1] in this format 'n b': ");
      scanf("&#37;d %d", &n, &b);
    
      printf("n is: %d\n", n);   //Test
      printf("b is: %d\n", b);   //TEST
    
      if ((b < 1) || (b > 10))
      {
        printf("Your base is not between 1 and 10");
        return 0;
      }
    
      if (n < 0)
      {
        printf("n must be positive");
        return 0;
      }
    
    
      while (n != 0)
      {
        output[index] = n % 10;
    
        if ((output[index] >= b) && (output[index] != 1))  //TROUBLE AREA RIGHT HERE
        {
          printf("one of the digits is greater than the base");
          return 0;
        }
    
        n = n/b;
        ++index;
        printf("number to convert: %d\n", n);
      }
      return 0;
    }
    For some reason if i put n = 110 and b=2, it goes into the if statement, which it is not supposed to.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This problem is WAY easier if you read the "n" value as a string instead of an integer. You are going to be processing it digit-wise, anyway, so why convert from digits to a value only to convert back to digits again?

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Hi, I'm reading "n" as a string now, but for some reason I'm keep getting a "segmentation fault (core dumped)". Do you know why is it doing this?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
      int b;
      char n;
      int output[64];
      int index = 0;
    
      printf("Enter base(b) between [1,10], and a number(n), such that digits of n is between[0,b-1] in this format 'n b': ");
      scanf("&#37;s %d", &n, &b);
    
      printf("n is: %s\n", n);   //Test
      printf("b is: %d\n", b);   //TEST
    /*
      if ((b < 1) || (b > 10))
      {
        printf("Your base is not between 1 and 10");
        return 0;
      }
    
      if (n < 0)
      {
        printf("n must be positive");
        return 0;
      }
    
    
      while (n != 0)
      {
        output[index] = n % 10;
        n = n/b;
        ++index;
        printf("number to convert: %d\n", n);
      }*/
      return 0;
    }
    Thank you!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You're not reading it as a string, you're reading it as a single char (but with string format).

    --
    Mats

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by xp5 View Post
    Hi, I'm reading "n" as a string now, but for some reason I'm keep getting a "segmentation fault (core dumped)". Do you know why is it doing this?
    n needs to be an array large enough to hold the input. E.g:

    Code:
    char n[64];
    Also, you'll have to change other parts of the code, because processing digit-by-digit is completely different than your old method. But the result will be much simpler than what you have now.

    Since I suspect this is homework, I won't post an actual solution -- but you should be able to solve the entire problem in fewer than 20 lines of code. The function I wrote which converts the number string from its given base is only three lines long.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    n needs to be an array large enough to hold the input. E.g:

    Code:
    char n[64];
    Also, you'll have to change other parts of the code, because processing digit-by-digit is completely different than your old method. But the result will be much simpler than what you have now.

    Since I suspect this is homework, I won't post an actual solution -- but you should be able to solve the entire problem in fewer than 20 lines of code. The function I wrote which converts the number string from its given base is only three lines long.
    There's even a library function to do that, right?

    By the way, is base 1 actually a valid base? What's the semantics of "base 1"?

    --
    Mats

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    By the way, is base 1 actually a valid base? What's the semantics of "base 1"?
    Tick marks.

    1 = 1
    2 = 11
    3 = 111
    4 = 1111
    5 = 11111
    etc...

    EDIT: Base 1 was probably the first method of written counting ever invented. Even more amusing is the concept of "Base 0," where each number gets its own unique symbol:

    1 = Harry
    2 = Sam
    3 = Bobby
    4 = Johnny
    5 = Terrence
    etc...
    Last edited by brewbuck; 08-30-2007 at 03:58 PM.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    For some reason if i put n = 110 and b=2, it goes into the if statement, which it is not supposed to.
    Thats becuase of this

    Code:
    n = n/b;  /* Which should have been n/10
    But any way you should ry this problem with the solution, using string. What other sugguested you.

    HINT: The thing which u are trying to do can be done using strtol function.
    ssharish2005

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Actually, I can't use char in this problem, everything has to be integer. But, I got it now! Thanks!!

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xp5 View Post
    Actually, I can't use char in this problem, everything has to be integer. But, I got it now! Thanks!!
    Consider that you may want to input base > 10, in which case the normal numbers don't suffice, so you you need to read the original number in as a string - not a single char of course, but an array of chars (which, when it's terminated with a zero(0 non '0') is a string in C).

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem I Can't solve...
    By ferniture in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 02:51 AM
  2. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 09:32 PM
  3. Question pertaining to limits...
    By Theoclymenus in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2005, 11:39 AM
  4. ok i think i missed the memo
    By kl3pt0 in forum C Programming
    Replies: 27
    Last Post: 06-12-2004, 06:11 PM
  5. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM