Hi,
When you calculate the sum of each of the digits, you have not
given a provision for having a sum that is of 2 digits.i.e., while
adding 12345 and 98764, you dont get the intended results. (on adding
6+4 in the last but one position, the result is displayed as 10).
Instead, try using some modulo function for separating a 2 digit
number in any position and to have the corresponding carry, like:
s[k] = a[i] + b[j]+ carry;
if(s[k]>=10)
{
temp=s[k];
rem=temp%10;
quotient=temp/10;
s[k]=rem;
carry=quotient;
}
Regards,
Krishna
--- In [email protected], Haohao <[EMAIL PROTECTED]> wrote:
>
> hello!
> I'm new to C programming.And I've got to make an adder with numbers
beyond the integer storage limits.My program can excute successfully
on VC.But it gets wrong on cygnus or cygwin.I don't kown why.Could you
please give me some help?Thanks very much.
>
>
> PS: #include<stdio.h>
> #include<stdlib.h>
> void sum(int a[30],int b[30]);
> int i = 0,j = 0,k2;
> int s[30]={0};
> int main()
> {
> char c1,c2;
> int x;
> int num1[30],num2[30];
>
> printf("enter the first number:");
> while( ( c1 = getchar() ) != '\n'){
>
> i++;
> num1[i] = atoi(&c1);
>
> }
>
> printf("enter the second number:");
> while( ( c2 = getchar() ) != '\n'){
> j++;
> num2[j] = atoi(&c2);
> }
>
> if(i >= j)
> k2 = i + 1;
> else
> k2 = j + 1;
>
> sum(num1,num2);
>
> for(x = 1;x <= k2;x++){
> printf("%d",s[x]);
> }
> return 0;
> }
>
> void sum(int a[30],int b[30])
> {
> int y,k;
>
> if(i >= j){
> k = i + 1;
>
> while(i != 0 && j != 0){
> s[k--] = a[i--] + b[j--];
> }
>
> while(k != 0 && i != 0){
> s[k--] = a[i--];
> }
> }/*end if*/
>
> else{
> k = j + 1;
>
> while(i != 0 && j != 0){
> s[k--] = a[i--] + b[j--];
> }
> while(k != 0 && j != 0){
> s[k--] = b[j--];
> }
>
> }/*end else*/
>
> for(y = k2;y >= 1;y-- ){
> if(s[y] > 10){
> s[y] = s[y] - 10;
> s[y - 1] = s[y - 1] + 1;
> }/*end if*/
> }/*end for*/
> }/*end sum*/
>
>
> ___________________________________________________________
> @yahoo.cn æ°ååãæ ééï¼å¿«æ¥æ¢æ³¨ï¼
> http://mail.yahoo.cn/
>
> [Non-text portions of this message have been removed]
>