0

Sum of char arrays.

I have two char arrays str1 and str2. They are defined like this: char *str1 = "9998"; char *str2 = "3"; I want to sum them and return it in an array. How could you do that? An example code would be nice. The output of said function must be "10001".

27th Jun 2016, 4:14 PM
Garme Kain
Garme Kain - avatar
21 Respuestas
+ 2
http://www.sololearn.com/app/cplusplus/playground/cfuJgxA2eXf6/ without using any library functions and dynamic memory
27th Jun 2016, 6:24 PM
Ramesh Marisa
0
I don t know why you use type int like char ?!
27th Jun 2016, 4:20 PM
Khalil El Khabbaoui
Khalil El Khabbaoui - avatar
0
I want to sum big numbers. And by big i mean really big. 10000 digits numbers or more. So a built-in data type does NOT apply here.
27th Jun 2016, 4:21 PM
Garme Kain
Garme Kain - avatar
0
do you know how to write in file? you write all characters on seperate file(str1 in file1 and str2 in file2) and then pick the two numbers by using 2 long double variables...... there is another method but that is more complicated....you will use for loops and the function expr () add numbers by multiplying with 10^i to a single double variable. I never tried it but I am pretty sure it'll work I am sorry I read your post after I wrote this answer
27th Jun 2016, 4:43 PM
Mukul Kumar
Mukul Kumar - avatar
0
Mukul: Long double variables cant store a 1000 digit number. the maximum is 1.79769e+308, hence, 308 digits.
27th Jun 2016, 4:51 PM
Garme Kain
Garme Kain - avatar
0
I already edited my answer .......
27th Jun 2016, 4:53 PM
Mukul Kumar
Mukul Kumar - avatar
0
atoi coverts the string to integers. with 1000+ digit numbers that doesnt work.
27th Jun 2016, 5:11 PM
Garme Kain
Garme Kain - avatar
0
how it will store upto 1000+ digits?
27th Jun 2016, 5:14 PM
Ramesh Marisa
0
there is a possible data loss....@ramesh
27th Jun 2016, 5:15 PM
Mukul Kumar
Mukul Kumar - avatar
0
try dynamic allocated memory.....if you only wanna add then creat some dynamic int pointers
27th Jun 2016, 5:18 PM
Mukul Kumar
Mukul Kumar - avatar
0
With respect to ur question, how many digits needed or largest number?
27th Jun 2016, 5:18 PM
Ramesh Marisa
0
Converting to built in datatype doesnt apply here. It is possible to do operations with 10000+ digits. I've just done it. This is like an exercise. I will upload the code in a little bit.
27th Jun 2016, 5:21 PM
Garme Kain
Garme Kain - avatar
0
@ramesh: lets put 10000 digits max.
27th Jun 2016, 5:22 PM
Garme Kain
Garme Kain - avatar
0
example give one number
27th Jun 2016, 5:25 PM
Ramesh Marisa
0
@ramesh: actually, i have a real max value. the maximum number of digits is the maximum posible value of unsigned int data-type.
27th Jun 2016, 5:25 PM
Garme Kain
Garme Kain - avatar
0
@ramesh: i dont understand your question.
27th Jun 2016, 5:27 PM
Garme Kain
Garme Kain - avatar
0
unsigned int can hold max 10 digit number : 4294967295. Is it my understanding correct?
27th Jun 2016, 5:32 PM
Ramesh Marisa
0
thats right. So the numbers must have less than 2^32 digits. Actually you wrote 2^32 as the max value of unsigned int. that is not correct. unsigned int can store 2^32 values, but one of them is 0, so the max value is 2^32 - 1.
27th Jun 2016, 5:35 PM
Garme Kain
Garme Kain - avatar