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 ?
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 ?
Convert it to a string and peel off each character.
is there any way to do it with the integer division and modulus operator
To address that question, you might want to work out the remainder on dividing 987987 by 10.
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.
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.
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
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 algorithmCode:#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 % 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; }
Last edited by manzoor; 05-12-2008 at 08:04 AM.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
to evalutate the number of digits in the number:
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 algorithmCode: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%base; x/=base; }
Last edited by m37h0d; 05-12-2008 at 08:57 AM.
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.