Thread: Merging into a number.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Merging into a number.

    Hello guys!

    I am trying to merge a number which is contained in seperate indexes of an array.

    So for example I have [0] = 4, [1] = 3, [2] = 7.

    That would save into a variable of type int the number "437".

    Any number of indexes is possible upto the maximum of int. Thats somewhere around 32000 right?

    I am unsure how would be the easier way to do this.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    You could either put the number in an array and convert it using atoi() - OR - you could use modular arithmetic to merge it, eg:

    437 = (4 * 100) + (3 * 10) + (7 * 1).

    When dealing with absurdly large numbers, you're gonna have to implement some kind of a handler for it yourself, or use an external library. You could also just use an array to do it and handle the carry yourself.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, let's get this right: You have a (potentially large) array of small integers (0-9), that you want to put together into a long number?

    The maximum of an int is commonly either 32767 or 2147483648. The latter is the usual case for modern machines, the former was the case on MS-DOS and Windows 2.x machines (and OS/2 1.x).

    What have you done so far?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    This is what Ive done, but it seems I was way off, and it doesnt work as planned.

    Code:
    #include <stdio.h>
    
    int array[] = {3,5,0,7,4};
    
    int main()
    {
      int x;
    
      printf("%s", array);
      x = my_getnbr(array);
      printf("\n%d", x);
    }
    
    
    
    int     my_getnbr(char *str)
    {
      int x, y, z;
      y = 0, x = 0, z = 0;
    
      while (*str != '\0')
        {
          str++;
            y++;
        }
      while (x <= y)
        {
          while (z <= *str[x])
    	{
    	if (str[x] = 0)
              z = z + 0;
            z = z + 10000;
    	}
          str++;
          while (z <= *str[x])
    	{
    	if (str[x] = 0)
              z = z + 0;
            z = z + 1000;
    	}
          str++;
          while (z <= *str[x])
            {
            if (str[x] = 0)
              z = z + 0;
            z = z + 100;
            }
          str++;
          while (z <= *str[x])
            {
              if (str[x] = 0)
                z = z + 0;
            z = z + 10;
            }
          str++;
          while (z <= *str[x])
    	{
    	if (str[x] = 0)
              z = z + 0;
            z = z + 1;
    	}
        }
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code is decidedly "too complex" (and broken) - I very much doubt that it's even compiled.

    Code:
    int array[] = {3,5,0,7,4};
    ...
      x = my_getnbr(array);
      printf("\n&#37;d", x);
    }
    
    
    
    int     my_getnbr(char *str)
    ...
    This should at the very least give you a compiler warning, as it's passing an integer array to a function that takes a character pointer. Converting arrays to pointers is fine, but don't change the type at the same time, or you'll be getting into trouble.

    This is not right:
    Code:
      while (*str != '\0')
        {
          str++;
            y++;
        }
    It compares a integer array with char, which is OK as such, but in this case isn't the right thing to do, becaue the value of '\0' is the same as 0 - and your array has a zero in the third position [index 2] (and it's valid to have zero's in an array of integers, otherwise the value 1000 couldn't be described in this model, and I don't think that's supposed to be the case).

    Code:
      while (x <= y)
        {
    x and y are unchanged within this loop, so it will never exit the loop if the condition is true when it gets to this point.

    Code:
    	if (str[x] = 0)
              z = z + 0;
    What does this do? Absolutely nothing. Adding with zero is meaningless.

    Code:
    while (z <= *str[x])
    I expect this to give a compiler error, as you are taking an indirection (*) on an array that is originally just a pointer to char.

    Edit: And you shouldn't need to duplicate the code multiple times. Imagine how this would work if you had a really large number...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    if (str[x] = 0)
    z = z + 0;What does this do? Absolutely nothing. Adding with zero is meaningless.
    Actually it does do something, just not what was inteneded. It sets str[x] to 0 which then causes the stuff in the if statement to never run, which dident matter beacuase it never did anything anyway. For a comparison it should really have been:

    if (str[x] == 0)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    Actually it does do something, just not what was inteneded. It sets str[x] to 0 which then causes the stuff in the if statement to never run, which dident matter beacuase it never did anything anyway. For a comparison it should really have been:

    if (str[x] == 0)
    Doh! Of course, you are right. But none of the other code looks particularly right either...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    I'll work on it more, and see if I can come up with something.

    Thanks for the help

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    This is the code I have now:

    It doesnt work and the compiler gives no error. I dont understand why.

    (So far it should work, if a number has no zeros in it anyway)

    Code:
    #include <stdio.h>
    
    char array[5] = {'3','5','2','7','4'};
    
    int main()
    {
      int x;
      int y;
    
      for (y = 0; y < 5; y++)
      {
          printf("&#37;c", array[y]);
      }
      
      x = my_getnbr(array);
      printf("\n%d", x);
      
      system("PAUSE");
      return 0;
      
    }
    
    
    
    int my_getnbr(char *str)
    {
      int x, y, z;
      y = 0, x = 0, z = 0;
    
      while (*str != '\0')
        {
          str++;
            y++;
        }
      if (y == 1)
         z = z * str[0];
      if (y == 2)
         z = str[1] + (str[0] * 10);
      if (y == 3)
         z = str[2] + (str[1] * 10) + (str[0] * 100);
      if (y == 4)
         z = str[3] + (str[2] * 10) + (str[1] * 100) + (str[0] * 1000);
      if (y == 5)
         z = str[4] + (str[3] * 10) + (str[2] * 100) + (str[1] * 1000) + (str[0] * 10000);
         
         return z;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    Use a loop

    int result = 0;

    Then in a loop
    result = result * 10 + ( str[ i ] - '0' );
    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.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because 'array' isn't nul-terminated.

    Use a loop.
    Last edited by zacs7; 10-04-2007 at 05:30 AM. Reason: Being the fool I am

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Use a loop

    int result = 0;

    Then in a loop
    result = result * 10 + ( str[ i ] - '0' );
    Im sorry I couldn't quite grasp that.

    Use a loop where? One for the whole function?

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, something like:

    Code:
    /* it's not a string since it's not nul-terminated */
    int my_getnbr(const char * arr, size_t n)
    {
        int result = 0;
        size_t i = 0;
    
        for(i = 0; i < n; i++)
            result = result * 10 + (arr[i] - '0');
    
        return result;
    }

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Again Im sorry but I dont understand.

    If result = 0 then wont result = result * 10 always give you zero?

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by +Azazel+ View Post
    Again Im sorry but I dont understand.

    If result = 0 then wont result = result * 10 always give you zero?
    No, the + (arr[i] - '0'); part will start by adding something to the 0 (at some point) so it will eventually be non-zero.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM