Register to reply

Separate digits in C++

by Skainstein
Tags: digits, separate
Share this thread:
Skainstein
#1
Aug6-07, 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.
Phys.Org News Partner Science news on Phys.org
Bees able to spot which flowers offer best rewards before landing
Classic Lewis Carroll character inspires new ecological model
When cooperation counts: Researchers find sperm benefit from grouping together in mice
chroot
#2
Aug6-07, 05:40 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,428
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
Timo
#3
Aug6-07, 05:49 PM
P: 310
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),...

rcgldr
#4
Aug6-07, 06:34 PM
HW Helper
P: 7,047
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.
las3rjock
#5
Aug6-07, 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 floating-point 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.
las3rjock
#6
Aug6-07, 06:40 PM
P: 235
Quote Quote by Jeff Reid View Post
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.
This is quite possibly the most expensive and non-obvious digit-extraction algorithm I've ever heard of...
Skainstein
#7
Aug7-07, 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.
Skainstein
#8
Aug7-07, 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.
CRGreathouse
#9
Aug7-07, 07:40 AM
Sci Advisor
HW Helper
P: 3,684
Quote Quote by Skainstein View Post
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.
Reading a number from the keyboard and converting it to an integer (or longlongint or whatever) shouldn't take much time compared to checking a 100-digit number for primality.
Timo
#10
Aug7-07, 11:40 AM
P: 310
- 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 c-like 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 ever-increasing 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.
chroot
#11
Aug7-07, 02:50 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,428
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
Timo
#12
Aug7-07, 03:21 PM
P: 310
Quote Quote by chroot View Post
[...]I still have no idea how this is going to help you test for primality, though.
That's the point where your statement claiming "you can simply use a character array" breaks down (sidenote: Imho a std::string object is preferrable over a character array in almost all cases). As he said, he's going to loop from 2 to the input number, meaning that if he's keeping the input as a char array he needs to either compare the loop-counter with the number or define/program mathematical operations on the character array. Pretty much the same is true for checking if the current number is prime.
chroot
#13
Aug7-07, 03:30 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,428
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
Skainstein
#14
Aug7-07, 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.
chroot
#15
Aug7-07, 03:52 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,428
Separating the digits requires essentially zero time. After all, they're typed in by the user as separate digits!

- Warren
Skainstein
#16
Aug7-07, 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.
chroot
#17
Aug7-07, 04:10 PM
Emeritus
Sci Advisor
PF Gold
chroot's Avatar
P: 10,428
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
Skainstein
#18
Aug7-07, 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