The Program is Addition of Large Integers.. Although This program is working But I want to know where i went wrong or what should i need to do to make it better..

# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# include <string.h>

typedef struct poly
{
   int *coeff;
   int degree;
}poly;

void init(poly *, char *);
void add (poly ,poly ,poly *);

int main(void)
{
    char num1[80],num2[80],*sum;
    int i;
    poly p1,p2,p3;
    
    printf("\n\n    Program Large Integer Arithmatics :- ADDITION");
    
    printf("\n\n    Enter 1st Number:");
    fscanf(stdin,"%s",num1);
    
    printf("\n\n    Enter 2nd Number:");
    fscanf(stdin,"%s",num2);
    
    init(&p1,num1);

    init(&p2,num2);
    
    add(p1,p2,&p3);

    for(i = 0 ;i <= p3.degree ; i++)
    	sum[i] = p3.coeff[i] + 48;
    sum[i] = '\0';
    
    printf("\n\t Addition = %s",strrev(sum));
    

    free(p1.coeff);
    free(p2.coeff);
    free(p3.coeff);
    return 0;
}

void init(poly *p, char *num)
{
     int i,len,j;

     len = strlen(num);

     p->coeff = (int *) malloc( len * sizeof(int));
     
     //printf("\n   %d",len);
     
     for ( i = len - 1 ,j = 0 ; i >= 0 ; i-- , j++)
	 p->coeff[j] = num[i] - 48;
     
     p->degree = j - 1;

}

void add(poly p1 , poly p2 , poly *p3)
{
     int i , l , carry = 0 , s;
 
     l = p1.degree > p2.degree ? p1.degree : p2.degree;

     p3->coeff = (int *) malloc ((l+1) * sizeof(int));

     for(i = 0 ; i <= l ;i++)
     {
	if(i > p1.degree)
	   s = p2.coeff[i] + carry;

	else if (i > p2.degree)
	   s = p1.coeff[i] + carry;

	else
	   s = p1.coeff[i] + p2.coeff[i] + carry;

	carry = 0;

	carry = s / 10;

	p3->coeff[i] = s % 10;
     }

     if(carry)
     {
	p3->coeff[i]=carry;
	p3->degree= i;
     }
     else
       p3->degree = i-1;

}

Any help will be appriciated.

thanks

vinit mittal

It's too complicated. If all you are doing is adding two large numbers:
1) read the numbers as you did, have one more array for the answer.
2) convert each character digit to a numerical digit (hint: '3' - '0' = 3)
3) start at the end of each number and add the values. Keep track of carrys as you move left to the next digits.

@WaltP sir,

I am actually trying to perform all Arithmatic operations on Large Integer. First i am trying to add two numbers, later i will try to multiply and other operations. that's why i am using polynominals..

I tried to simplyfy my code
here is my structure

typedef struct LargeInt
{
   int *coeff;
   int max_expo_degree; // it will be one less than the size of Largeint

}LargeInt;

My main

int main(void)
{
    char num1[79], num2[79], num3[80];
    int i;
    LargeInt first, second, third;

    // Scaning two large values
    printf("\n\n    Program Large Integer Arithmatics :- ADDITION");

    printf("\n\n    Enter 1st Number:");
    fscanf( stdin ,"%s", num1 );

    printf("\n\n    Enter 2nd Number:");
    fscanf( stdin ,"%s", num2 );

    
    init(&first , num1);  // Initializing first LargeInt
    init(&second , num2); // Initializing second

    add(first , second , &third); // adding first and second 

    // converting third LargeInt into String

    for( i = 0 ; i <= third.max_expo_degree ; i++ )
    	num3[third.max_expo_degree - i]= third.coeff[i] + '0';
    
    num3[i] = '\0';
    
    printf("\n Addition of \n  %s \n %s \n = %s \n", num1 , num2 , num3);
    
    return 0;
}

Initialisation Function

void init(LargeInt *li, char *num)
{
     int i , j , len;

     len= strlen(num);

     // Allocationg Memory space Equal to length of num string

     li->coeff = (int *) malloc((len) * sizeof(int));
     

     for (i=len-1,j=0; i>=0; i--,j++)
	  li->coeff[j] = num[i] - '0';

     li->max_expo_degree = len-1; // one less than size
}

and My Add Function

void add( LargeInt first , LargeInt second , LargeInt *third )
{
     int i,l,carry=0,s=0;
     
     // finding max size

     l = first.max_expo_degree > second.max_expo_degree ?
	    first.max_expo_degree : second.max_expo_degree;

     // Allocating memory space +1
     third->coeff = (int*) malloc ((l+1) * sizeof(int));

     for( i = 0 ; i <= l ; i++ )
     {
	if( i > first.max_expo_degree)
	     s = second.coeff[i] + carry;

	else if (i > second.max_expo_degree)
	     s = first.coeff[i] + carry;

	else
	   s = first.coeff[i] + second.coeff[i] + carry;
	
         
         carry = s / 10;
	third->coeff[i] = s % 10;
     }
     
     if(carry)
     {
	third->coeff[i]=carry;
	third->max_expo_degree= i;
     }
     else
       third->max_expo_degree = i-1;

}

This Program will Work Like this

Enter First value
997890
Enter Second value
7654

now
LargeInt first will be Initialised
first (c,e)= (0 , 0) (9 , 1) (8 , 2) (7 , 3) (9 , 4) (9 ,5)

second (c,e)=(4 , 0) (5 , 1) (6 , 2) (7 , 3)

after Addition

Third (c,e)= (4 , 0) (4 , 1) (5 , 2) (5 , 3) (0 , 4) (0 , 5) (1 , 6)

and after converting num3 = 1005544

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.