Thread: separate integer into individual units

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    separate integer into individual units

    ok in what way do you separate the digits from a given int ??


    like if the int is 987987

    how do you separate them in to 9 8 7 9 8 7 ???

    is there any function to do this ?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Convert it to a string and peel off each character.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    is there any way to do it with the integer division and modulus operator

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To address that question, you might want to work out the remainder on dividing 987987 by 10.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    hey thanks i got it but

    how do you show the output or organize them

    like 987987 % 10 it gives 7 which is the last digit

    i store the remainder 7 and the quotient and then again modulo the quotient and store the remainder and the quotient

    but how much variables should i declare how would i know how much digits the integer has and then allocate memory for it, but i even dont know runtime memory allocation

    and the number which are obtained by the modulo are the last digits is there any way to obtain the first digit
    Last edited by manzoor; 05-12-2008 at 07:42 AM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You now know what 987987 % 10 gives you, what does 987987 / 10 give you?

    You really need a C++ book. Are people really just stumbling upon compilers on the street and firing them up?
    Last edited by medievalelks; 05-12-2008 at 07:49 AM.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    im following a c++ book

    deitel and deitel how to program c++

    and the questions aare from the book

    Write a program that inputs a five-digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each. [Hint: Use the integer division and modulus operators.] For example, if the user types in 42339, the program should print:

    4 2 3 3 9

    ok i have done it

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int x;
    
    	const int z = 10;
    
    	cout << "Enter number to be separated: ";
    	cin >> x;
    
    	int quotient = x / z;
    	int dgt5 = x &#37; z;
    
    	int dgt4 = quotient % z;
    	quotient = quotient / z;
    
    	int dgt3 = quotient % z;
    	quotient = quotient / z;
    
    	int dgt2 = quotient % z;
    	quotient = quotient / z;
    
    	int dgt1 = quotient % z;
    	quotient = quotient / z;
    	cout << dgt1 << "   " << dgt2 << "   " << dgt3 << "   " << dgt4 << "   " << dgt5 << endl;
    
    	return 0;
    }
    my problem is solved though im curious how would i be able to make one that would separate more than five digit integer using the same algorithm
    Last edited by manzoor; 05-12-2008 at 08:04 AM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could try putting that into a loop, so that you won't need separate variables to hold the results (you could print them right away).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    to evalutate the number of digits in the number:

    Code:
    int x = 987987;
    unsigned int base = 10;
    const unsigned int length = floor(log10(x))+1;
    unsigned short *digits = new unsigned short[length];
    unsigned int i = length;
    while(i)
    {
            digits[--i]=x&#37;base;
            x/=base;
    }
    since this wasn't a HW assignment, i edited this a bit to show how to parse an int of arbitrary length, and to connote better that this problem is actually the beginning of a base conversion algorithm
    Last edited by m37h0d; 05-12-2008 at 08:57 AM.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'd suggest a good integer exclusive implementation. (As opposed to using the floating point implementation provided by the standard library.)

    I'm fairly certain this came from HD, but I don't remember for certain.

    Edit: http://www.hackersdelight.org/HDcode.htm

    Soma

    Code:
    int base_10_integer_logarithm
    (
      const unsigned long value_f
    )
    {
      if(99 < value_f)
      {
        if (1000000 > value_f)
        {
          if (10000 > value_f)
          {
            return(3 + (((signed long)(value_f - 1000)) >> 31));
          }
          return(5 + (((signed long)(value_f - 100000)) >> 31));
        }
        if (100000000 > value_f)
        {
          return(7 + (((signed long)(value_f - 10000000)) >> 31));
        }
        return(9 + (((signed long)((value_f - 1000000000) & (~value_f))) >> 31));
      }
      if(9 < value_f)
      {
        return(1);
      }
      return(((signed long)(value_f - 1)) >> 31);
    }
    Last edited by phantomotap; 05-12-2008 at 10:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM