is there any other way ? cause my program that I am working on takes two strings like "1234567" and "654321" and puts them into two int arrays which are Num1[] and Num2[] ,
so here for example Num1[0]= 7 (I reversed them so i can add them later, so it is the last number)
and Num2[0]=1
when i am adding them together like x= Num1[0] + Num2[0];
it is giving me a very weird number (-8.589...e) something weird. and every time the same number whether Num1[0] or Num1[1]
@Duoas. I did not forget about the carry, i actually have it in my loop.... but like i said the initial sum is not working from the first place it is giving me a weird number.
well i did the exact same thing to enter the string into the array but I wanted to create a function for putting them in the arrays, and another for summing them, in the other function there is no need to put them in an array again, i just need to sum them.
but thanks anyway.
any other ideas?
bool sum(
char a[], int alen, // c = a + b
char b[], int blen,
char c[], int clen )
{
// It helps if alen >= blen
if (alen < blen) return sum( b, blen, a, alen, c, clen );
// Also, clen must be greater than or equal to (alen + 1)
if (clen <= alen) returnfalse;
// Now you are ready to add
int carry = 0;
int i;
// First, add the stuff that is in both arrays
for (i = 0; i < blen; i++)
{
}
// Next, keep adding carry through the longer array (a)
for (; i < alen; i++)
{
}
// Next, carry until no carry remains
if (carry != 0)
c[ clen - 1 - i ] = carry + '0';
returntrue;
}
Notice how I index the arrays as x[ xlen - 1 - i ]. Remember, you have to work from least-significant digit to most-significant.