Register to reply 
Separate digits in C++ 
Share this thread: 
#1
Aug607, 05:20 PM

P: 8

I don't know much of programming but i know a few things...so please be gentle on me
I need help (in C++) with separating the diggits of a number without using any divisions (no '%10', no '/10', etc), just something that reads the number and gives me the diggits. don't know if it helps but those numbers which i want to separate, are in a loop. I would really appreciate your help people. an example would be great. thx. 


#2
Aug607, 05:40 PM

Emeritus
Sci Advisor
PF Gold
P: 10,427

What do you mean by "reads the number?" Are you obtaining a number from the keyboard? And what do you mean by numbers being "in a loop?"
You need to ask clearer questions.  Warren 


#3
Aug607, 05:49 PM

P: 322

Copy it to a string .
No honestly, it would probably help saying what kind of numbers you're talking about (type and range), why you don't want to use /10 and %10, what you need it for, what the relevant part of your program looks like (or is supposed to look like),... 


#4
Aug607, 06:34 PM

HW Helper
P: 7,131

Separate digits in C++
A very large array of pointers to strings representing the digits of the index into the arrray of pointers would work. With a mere 1,188,888,826 bytes (1.189GB) of ram, 4 bytes per pointer, 1 byte per digit, using sequential pointers to calculate string lengths (no string terminators), you could quickly convert the numbers from 0 to 99,999,999 into strings.



#5
Aug607, 06:36 PM

P: 235

I agree that the original poster needs to be more precise in defining the problem. Does "number" mean that the input is an integer, a floatingpoint number, or possibly either type? I also think that it's rather strange that you would not be able to use division or modular arithmetic, but under those restrictions, I agree with Timo that your best bet is to convert the number into a string.



#6
Aug607, 06:40 PM

P: 235




#7
Aug707, 06:46 AM

P: 8

ok. i'm sorry. i'll be more specific. the program i'm doing is supposed to test if numbers are primes or not but without any need of divisons. for those who know, the divisions are what takes longer to calculate so i don't want to use them. it stars from, the user uses the keybord to say whats the maximum point for the program to run, then the program does it all by itself, running from 0 (or 1) to that number. i was thinking of something like cin.get(), then puting the numbers into an array or something. thx for ur quick replys. PS: the numbers can become really large, like 100 digits and more, but for now their just until 10 digits. and...i hope its not asking much but could u give an example.



#8
Aug707, 06:55 AM

P: 8

i've read the posts where it says to put numbers into a string and it seems to be a good idea...i just dont know how to do that.



#9
Aug707, 07:40 AM

Sci Advisor
HW Helper
P: 3,684




#10
Aug707, 11:40 AM

P: 322

 Conversion of integers to strings is not much of a problem. A c++ solution is piping the number into a stringstream and then obtaining it as a string from there. Another more clike version is using printf(). Should be easy finding examples by using google (keyword being something like " "c++" integer string conversion").
 The usage of %x and /x is probably not slow compared to anything similar you're going to write yourself.  Most importantly: With everincreasing size of the numbers you want to check you will sooner or later run into the problem how to store the numbers (integers are relatively limited in their range)  and that's only the first problem you run into. One possibility is storing the numbers in a string all the time and defining the appropriate mathematical operations (addition, division) on string objects. That's a solution but performance wise far from being a good one. 


#11
Aug707, 02:50 PM

Emeritus
Sci Advisor
PF Gold
P: 10,427

You don't need stringstreams or any other garbage. C'mon. A "number," as typed by the user on a keyboard, is just a string of characters. It's a word.
Use something like this: char number[20]; cin >> number; The first digit is number[0], the second digit is number[1], etc. I still have no idea how this is going to help you test for primality, though.  Warren 


#12
Aug707, 03:21 PM

P: 322




#13
Aug707, 03:30 PM

Emeritus
Sci Advisor
PF Gold
P: 10,427

Okay, so use atoi(), or the frustratingly stupid C++ equivalent.
So why is he going to use the number as a loop condition, but then also look at individual digits of it? Perhaps I don't understand the "algorithm" he's attempting to build.  Warren 


#14
Aug707, 03:49 PM

P: 8

all right, so what do u guys think: for number with, for example 20 digits, is it faster for the computer to make %10 or %100 (whatever), or to separate the digits other way than that? maybe it's a stupid question but let me remind u...i'm a n00b at this things :P
don't feel bad not to understand the algorithm...i invented it :D that's why i need this program. i need to know if it really works. 


#15
Aug707, 03:52 PM

Emeritus
Sci Advisor
PF Gold
P: 10,427

Separating the digits requires essentially zero time. After all, they're typed in by the user as separate digits!
 Warren 


#16
Aug707, 04:03 PM

P: 8

no, no, no!! that's what i've written before (u didn't read it maybe). the only number i type is the maximum value of my loop which starts in 0. now imagine i say to the program the maximum value is 1000000. it'll start with zero, and all by itself will get to 999999 and finally 1000000. now if it's in...12345, i didn't type that number. got it? it must separate the digits of the numbers which itself increases with the loop.



#17
Aug707, 04:10 PM

Emeritus
Sci Advisor
PF Gold
P: 10,427

Okay. Well, all of the methods used to convert integers into decimal strings involve divisions, which appears to be against your philosophy.
I would suggest simply using itoa() or sprintf().  Warren 


#18
Aug707, 04:12 PM

P: 8

So what ur telling me is that the other ways to do what i need are dependent on those %10, etc?
i don't know what itoa() is :P. is it something like cin.get()? 


Register to reply 
Related Discussions  
How to separate CO2 into C and O2  Chemistry  35  
Separate digits in C++  Programming & Computer Science  7  
Another ODE, can't separate variables  Differential Equations  3  
Ok, so now how do you separate this one?  Calculus & Beyond Homework  8  
All One, or All Separate?  General Discussion  16 