Hi guys, can anyone suggest an alternative method to splitting an integer into separate digits besides using the modulus"%" and division"/" technique or using itoa() or such functions to covert it to a string and then split it. i want to do it without the aid of STL or any other library functions. You know like for instance for swaping two variables "a" and "b" we define a "temporary" variable and do the swap but we can also do it without a temporary variable using arithmetic like this:-

variable a=3,b=6
a=a*b
b=a/b;
a=a/b;

plus can anyone suggest me a site or book from which i can get these kind of alternative or better ideas to implement programs so i can change my thinking a little bit.

Thanks!

Powers of 10? Assuming you want a decimal representation.

You'll have to define 'digit', due to this ambiguity.
0xFF vs 255, 2 'digits' vs 3

How about just reading it as a string? Then you have an array of numeric character.

ok thanks but i was trying to tell thats i know those two methods i.e " string to character" and using "modulus and division method" can u suggest any other besides these. i shall be thankful.

Ok then Note that :

1234 = 4 * 10^0 + 3 * 10^1 + 2 * 10^2 + 1 * 10^3

And in general suppose a natural number N = a_1a_2....a_n , where a_i is a digit from 0 to 9.

Then we can represent N as follows:
N = a_1...a_n = a_n * 10^0 + a_(n-1) * 10^ 1 ... a_1 * 10^(n-1)

where the "^" means the "power of"

Edited 6 Years Ago by firstPerson: n/a

Comments
He knows maths more than i know my girl friend

@firstPerson: Nice math lesson, but it doesn't tell us how to extract the digitis without using the % or / operators, or converting to a string, or " without the aid of STL or any other library functions" :) I'd be interested in seeing the solution too -- if there is one.

Edited 6 Years Ago by Ancient Dragon: n/a

Just a little proof of concept, unrefined example of modifying base 2 to base 10

#define bin_1    (1<<0)
#define bin_2    (1<<1)
#define bin_4    (1<<2)
#define bin_8    (1<<3)
#define bin_16   (1<<4)
#define bin_32   (1<<5)
#define bin_64   (1<<6)
#define bin_128  (1<<7)
unsigned char a = 219;
double dig_1(0), dig_2(0), dig_3(0);
if( a & bin_128 ) {
  dig_3 = 1.28;
  dig_2 = 2.8;
  dig_1 = 8;
}
if( a & bin_64 ) {
  dig_3 += 0.64;
  dig_2 += 6.4;
  dig_1 += 4;
  if( dig_1 > 10 ) dig_1 -= 10;
}
if( a & bin_32 ) {
  dig_3 += 0.32;
  dig_2 += 3.2;
  dig_1 += 2;
  if( dig_3 > 10 ) dig_3 -= 10;
  if( dig_2 > 10 ) dig_2 -= 10;
  if( dig_1 > 10 ) dig_1 -= 10;
}
if( a & bin_16 ) {
  dig_3 += 0.16;
  dig_2 += 1.6;
  dig_1 += 6;
  if( dig_3 > 10 ) dig_3 -= 10;
  if( dig_2 > 10 ) dig_2 -= 10;
  if( dig_1 > 10 ) dig_1 -= 10;
}
if( a & bin_8 ) {
  dig_3 += 0.08;
  dig_2 += 0.8;
  dig_1 += 8;
  if( dig_3 > 10 ) dig_3 -= 10;
  if( dig_2 > 10 ) dig_2 -= 10;
  if( dig_1 > 10 ) dig_1 -= 10;
}
if( a & bin_4 ) {
  dig_3 += 0.04;
  dig_2 += 0.4;
  dig_1 += 4;
  if( dig_3 > 10 ) dig_3 -= 10;
  if( dig_2 > 10 ) dig_2 -= 10;
  if( dig_1 > 10 ) dig_1 -= 10;
}
if( a & bin_2 ) {
  dig_3 += 0.02;
  dig_2 += 0.2;
  dig_1 += 2;
  if( dig_3 > 10 ) dig_3 -= 10;
  if( dig_2 > 10 ) dig_2 -= 10;
  if( dig_1 > 10 ) dig_1 -= 10;
}
if( a & bin_1 ) {
  dig_3 += 0.01;
  dig_2 += 0.1;
  dig_1 += 1;
  if( dig_3 > 10 ) dig_3 -= 10;
  if( dig_2 > 10 ) dig_2 -= 10;
  if( dig_1 > 10 ) dig_1 -= 10;
}
unsigned int res_3(dig_3), res_2(dig_2), res_1(dig_1); // unsigned int will FLOOR decimal places
std::cout << "Third digit: " << res_3 << " Second digit: " << res_2 << " First digit: " << res_1 << "\n\r";

There are more optimized ways of doing this, such as reading the 128 bit, then checking to see if 64 and any bit equal to or higher than 8 is flagged (digit must be 2), etc
I mostly am highlighting that you can simply read the binary and convert it to your friendly neighborhood base if you are so inclined.

Edit: Also, you don't need to use floats, I was just being lazy.
If you didn't use floats you would just carry, no big deal.

Example without floats, shortened:

unsigned char a = 219; // again
// same dig 1/2/3
if( a & 128 ) {
  dig_1 = 1;
  dig_2 = 2;
  dig_3 = 8;
}
if( a & 64 ) {
  dig_2 += 6;
  dig_1 += 4;
  if( dig_1 > 10 ) {
    dig_1 -= 10;
    dig_2 += 1; // carry
  }
}
// ... etc

Edited 6 Years Ago by Unimportant: n/a

Just some technical loop holes in the requirements. No division or modulus operator
used.

#include <iostream>
using namespace std;

int myDiv(int a, int b){
	int ans = 0;
	while(b < a){ ++ans; a -=b; }
	return ans;
}
int myMod(int a, int b){
	return a - (b * int(myDiv(a,b)));
}
void printDigits(int num){
	if( abs(num) != 0 ){
		printDigits( myDiv(num,10) );
		cout << myMod(num,10) << endl;
	}
}
int main(){	
	printDigits(12345);
}
This article has been dead for over six months. Start a new discussion instead.