C Programmers: Help!
|
View this Thread in Original format
GelatinPufF |
I need to write a program in C that can calculate additions of numbers that are greater then 30 digits in length(hell, I think the test data will have digits that are greater then 1000 digits in length :().
Ie.
3000000023420234234234023434200234 + 23423423034333334223234232342324
As you know, the C int operator can’t store (and then calculate) numbers this big, so if anyone knows or has tips on an easy way to do it, please help:(
So far I’m thinking of hacking away at it by storing them both in separate arrays, then adding each individual array element together(carrying where necessary etc.) but this seems like to many conditional staments and loops to think about so late at night–cries!-
So if anyone has hints, tips, or a solution, leave a post:(
cheers~ |
|
|
dukes |
you know how you are trying them as "int" change it and try it as "long int"
edit*
i forgot to add if you want floating point declare as "double"
and if they are only going to be positive then try "unsigned int" first
still not enough then do "unsigned long int" for huge numbers....
i was a tad hasty with my first answer so i thougth i would add a little bit to get you there faster |
|
|
chavs |
long won't work. I can't remember if double will handle this down...
I think you have to read the numbers as strings and do your own addition algorithm. |
|
|
dukes |
quote: | Originally posted by chavs
long won't work. I can't remember if double will handle this down...
I think you have to read the numbers as strings and do your own addition algorithm. |
i recon the "unsigned long int" should be enough. |
|
|
chavs |
quote: | Originally posted by GelatinPufF
(hell, I think the test data will have digits that are greater then 1000 digits in length :(). |
I don't think an unsigned long int can handle 1000 digits. What kind of work is this (I mean a work for a class, something your boss tell you...)? |
|
|
dukes |
quote: | Originally posted by chavs
I don't think an unsigned long int can handle 1000 digits. What kind of work is this (I mean a work for a class, something your boss tell you...)? |
ok i just read 30.....1000 is a tad excessive
if its gonna be that long you will defo have to create strings and loops and crap for it
yes what is this work for? you might be able to round off a load of digits depending how acurate you REALY need to be. |
|
|
GelatinPufF |
quote: | Originally posted by dukes
ok i just read 30.....1000 is a tad excessive
if its gonna be that long you will defo have to create strings and loops and crap for it
yes what is this work for? you might be able to round off a load of digits depending how acurate you REALY need to be. |
Well I'm only starting on the program now, but it's ment to be able to calculate HUGE decimals values in Reverse Polish Notation.(ie. 33 44 + = 77 // Basically the operator goes last.
In the later stages of the program however, it's ment to multiply and divide numbers which are in excess of what any primitive data type can handle (and that includes long ints)
ie 99929392939293929392399239293945645645646456299239 * 929392392939293392939456434534529399239293932
:nervous:
A quick rundown of the Algoritm I was thinking of using goes something like this:
Test data: 9999 + 99232 (yes it's small, but the same algorithm should work for numbers much larger)
1. Determing which number is bigger based on length(if they're equal in length, I'll have to figure that out when I get to it:()
2. Store the two numbers in two arrays
LargestNumber[] = 99232
SmallestNumber[] = 09999
3. Get the length of the array = 5
4. Create an array which will house the totals as I'm adding them: Total[5] (mind you I think the length of the array should be 5+1.
5. Then add the last values of the arrays together and store them in the total array, carrying where appropriate.
ie.
while (arrayLength>0){
LargestNumber[ArrayLength] + SmallestNumber[ArrayLength] = Total[ArrayLength]
arrayLength--;
}
6. I still have to throw a conditional statement in there to carry over numbers and :(
Anyway, thanks for the replies!:happy2: And chavs: Yes it's a C assignment I've been given:( I've had a bit of experience with Java last year, and now they're hitting up wish this:( |
|
|
DJ-Kreing^^ |
Doesnt seem that hard to me...
Although I don't do much C, more into Pascal but the main idea is what counts so here goes.
Heh, its gonna be hard explaining myself in English…
First store both numbers in STRING data base, means the digits are no longer digits but just ascii chars.
Now program a loop which goes throughout the whole string, reads the ascii charts from the first to the last and stores them in a new array data base of two dimensions, hmm its like a table with the colloms number as the number of chars in the longest string, and two lines for two numbers. I'm sure you can do this is C since in Pascal this action is a really common action.
Now after you got this sorted out, use this array typed data base to sum up the numbers stored. Just program a loop which goes throught the array data base and sumes up the numbers and if the sum is higher then ten for example 8+7 you add 1 to the next colomn.
Use one variable to contain the sum, and that’s about it.
Hope you understand anything from what I tried to explain here… heh |
|
|
GelatinPufF |
quote: | Originally posted by DJ-Kreing^^
Hope you understand anything from what I tried to explain here… heh |
Yeah thanks mate;) I understand but the hard bit is applying it to code:( The problem with C however (as apposed to Java which I have alot of experience in) is comming to grips with how it uses pointers and arrays:( |
|
|
malek |
your best bet is to use arrays of INT and write a routine to take a big ass number and split it into small ones...
recursivly:wtf: |
|
|
|
|