string addition

B

bedifferent.girish

I want to add Two string ranging from 2-50 char, which contains the
numeric values.


For Example
a[]={1,0,0,0,0,0,0,0,0,0...........0}
+ b[]={2,0,0,0,0,0,0,0,0,0...........0}
________________________________
c[]={3,0,0,0,0,0,0,0,0,0...........0}

Plz Help Me.
 
V

Vladimir S. Oka

(e-mail address removed) opined:
I want to add Two string ranging from 2-50 char, which contains the
numeric values.


For Example
a[]={1,0,0,0,0,0,0,0,0,0...........0}
+ b[]={2,0,0,0,0,0,0,0,0,0...........0}
________________________________
c[]={3,0,0,0,0,0,0,0,0,0...........0}

These are not strings, but arrays of, presumably, `int`s.

Loop through them and add corresponding elements. Try to express that
in C, and if you have problems, come here for help. I doubt many
people here are willing to do your homework for you, and even if they
did, ask yourself whether it's going to do anything for your
knowledge. You are after knowledge, aren't you?
 
I

Ivanna Pee

I want to add Two string ranging from 2-50 char, which contains the
numeric values.


For Example
a[]={1,0,0,0,0,0,0,0,0,0...........0}
+ b[]={2,0,0,0,0,0,0,0,0,0...........0}
________________________________
c[]={3,0,0,0,0,0,0,0,0,0...........0}

Plz Help Me.

I read string addiction
 
M

Me

I want to add Two string ranging from 2-50 char, which contains the
numeric values.


For Example
a[]={1,0,0,0,0,0,0,0,0,0...........0}
+ b[]={2,0,0,0,0,0,0,0,0,0...........0}
________________________________
c[]={3,0,0,0,0,0,0,0,0,0...........0}

Addition's carry is either 0 or 1 (unless you're doing something
weird). Carry only occurs when all the terms are greater than the
maximum value. Putting all of this together:

T a[n], b[n], c[n+1];
....
T carry = a + b + oldcarry > maxvalue;

The above only works with math's unlimited range integers but not in
C's fixed range of integers (unless maxvalue is much less than T_MAX).
The way to work around this is to promote to a larger type (if one is
even available) or my preferred way of rearranging the terms:

T carry = a > (maxvalue - oldcarry) - b;

If T is an unsigned integer, you can use (T)-1 for maxvalue, otherwise
you have to #include <limits.h> and use T_MAX.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top