Sunday, 9 March 2014

Example of passing structure to function.

Example of passing structure to function.

Code for Example of passing structure to function in C Programming

struct student
     {
    name char[30];
    marks float;
     }
main ( )
{
    struct student student1;
    student1 = read_student ( )
    print_student( student1);
    read_student_p(student1);
    print_student (student1);
 }
struct student read_student( )    \\ A
{
    struct student student2;
    gets(student2.name);
    scanf(“%d”,&student2.marks);
    return (student2);
}
  void print_student (struct student student2)    \\ B
{
printf( “name is %s\n”, student2.name);
printf( “marks are%d\n”, student2.marks);
}
  void read_student_p(struct student student2)    \\ C
{
    gets(student2.name);
    scanf(“%d”,&student2.marks);

}

Program to maintain a threaded binary tree.

Program to maintain a threaded binary tree.

Code for Program to maintain a threaded binary tree in C Programming

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

enum boolean
{
    false = 0,
    true = 1
} ;

struct thtree
{
    enum boolean isleft ;
    struct thtree *left ;
    int data ;
    struct thtree *right ;
    enum boolen isright ;
} ;

void insert ( struct thtree **, int ) ;
void delete ( struct thtree **, int ) ;
void search ( struct thtree **, int, struct thtree **,
                struct thtree **, int * ) ;
void inorder ( struct thtree * ) ;
void deltree ( struct thtree ** ) ;

void main( )
{
    struct thtree *th_head ;

    th_head = NULL ;  /* empty tree */


    insert ( &th_head, 11 ) ;
    insert ( &th_head, 9 ) ;
    insert ( &th_head, 13 ) ;
    insert ( &th_head, 8 ) ;
    insert ( &th_head, 10 ) ;
    insert ( &th_head, 12 ) ;
    insert ( &th_head, 14 ) ;
    insert ( &th_head, 15 ) ;
    insert ( &th_head, 7 ) ;

    clrscr( ) ;
    printf ( "Threaded binary tree before deletion:\n" ) ;
    inorder ( th_head ) ;

    delete ( &th_head, 10 ) ;
    printf ( "\nThreaded binary tree after deletion:\n" ) ;
    inorder ( th_head ) ;

    delete ( &th_head, 14 ) ;
    printf ( "\nThreaded binary tree after deletion:\n" ) ;
    inorder ( th_head ) ;

    delete ( &th_head, 8 ) ;
    printf ( "\nThreaded binary tree after deletion:\n" ) ;
    inorder ( th_head ) ;

    delete ( &th_head, 13 ) ;
    printf ( "\nThreaded binary tree after deletion:\n" ) ;
    inorder ( th_head ) ;

    deltree ( &th_head ) ;

    getch( ) ;
}

/* inserts a node in a threaded binary tree */
void insert ( struct thtree **s, int num )
{
    struct thtree *p, *z, *head = *s ;

    /* allocating a new node */

    z = malloc ( sizeof ( struct thtree ) ) ;

    z -> isleft = true ;  /* indicates a thread */

    z -> data = num ;  /* assign new data */

    z -> isright = true ;  /* indicates a thread */
/* if tree is empty */
if ( *s == NULL )
    {
        head = malloc ( sizeof ( struct thtree ) ) ;

        /* the entire tree is treated as a left sub-tree of the head node */

        head -> isleft = false ;
        head -> left = z ;  /* z becomes leftchild of the head node */

        head -> data = -9999 ;  /* no data */

        head -> right = head ;  /* right link will always be pointing
                                    to itself */

        head -> isright = false ;

        *s = head ;

        z -> left = head ;  /* left thread to head */

        z -> right = head ;  /* right thread to head */

    }
    else/* if tree is non-empty */

    {
        p = head -> left ;

        /* traverse till the thread is found attached to the head */
while ( p != head )
        {
            if ( p -> data > num )
            {
                if ( p -> isleft != true )  /* checking for a thread */

                    p = p -> left ;
                else
                {
                    z -> left = p -> left ;
                    p -> left = z ;
                    p -> isleft = false ;  /* indicates a link */

                    z -> isright = true ;
                    z -> right = p ;
                    return ;
                }
            }
            else
            {
                if ( p -> data < num )
                {
                    if ( p -> isright != true )
                        p = p -> right ;
                    else
                    {
                        z -> right = p -> right ;
                        p -> right = z ;
                        p -> isright = false ;  /* indicates a link */

                        z -> isleft = true ;
                        z -> left = p ;
                        return ;
                    }
                }
            }
        }
    }
}

/* deletes a node from the binary search tree */
void delete ( struct thtree **root, int num )
{
    int found ;
    struct thtree *parent, *x, *xsucc ;

    /* if tree is empty */
if ( *root == NULL )
    {
        printf ( "\nTree is empty" ) ;
        return ;
    }

    parent = x = NULL ;

    /* call to search function to find the node to be deleted */

    search ( root, num, &parent, &x, &found ) ;

    /* if the node to deleted is not found */
if ( found == false )
    {
        printf ( "\nData to be deleted, not found" ) ;
        return ;
    }

    /* if the node to be deleted has two children */
if ( x -> isleft == false && x -> isright == false )
    {
        parent = x ;
        xsucc = x -> right ;

        while ( xsucc -> isleft == false )
        {
            parent = xsucc ;
            xsucc = xsucc -> left ;
        }

        x -> data = xsucc -> data ;
        x = xsucc ;
    }

    /* if the node to be deleted has no child */
if ( x -> isleft == true && x -> isright == true )
    {
        /* if node to be deleted is a root node */
if ( parent == NULL )
        {
            ( *root ) -> left = *root ;
            ( *root ) -> isleft = true ;

            free ( x ) ;
            return ;
        }

        if ( parent -> right == x )
        {
            parent -> isright = true ;
            parent -> right = x -> right ;
        }
        else
        {
            parent -> isleft = true ;
            parent -> left = x -> left ;
        }

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only rightchild */
if ( x -> isleft == true && x -> isright == false )
    {
        /* node to be deleted is a root node */
if ( parent == NULL )
        {
            ( *root ) -> left = x -> right ;
            free ( x ) ;
            return ;
        }

        if ( parent -> left == x )
        {
            parent -> left = x -> right ;
            x -> right -> left = x -> left ;
        }
        else
        {
            parent -> right = x -> right ;
            x -> right -> left = parent ;
        }

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only left child */
if ( x -> isleft == false && x -> isright == true )
    {
        /* the node to be deleted is a root node */
if ( parent == NULL )
        {
            parent = x ;
            xsucc = x -> left ;

            while ( xsucc -> right == false )
            xsucc = xsucc -> right ;

            xsucc -> right = *root ;

            ( *root ) -> left = x -> left ;

            free ( x ) ;
            return ;
        }

        if ( parent -> left == x )
        {
            parent -> left = x -> left ;
            x -> left -> right = parent ;
        }
        else
        {
            parent -> right = x -> left ;
            x -> left -> right = x -> right ;
        }

        free ( x ) ;
        return ;
    }
}

/* returns the address of the node to be deleted, address of its parent and
    whether the node is found or not */
void search ( struct thtree **root, int num, struct thtree **par,
                struct thtree **x, int *found )
{
    struct thtree *q ;

    q = ( *root ) -> left ;
    *found = false ;
    *par = NULL ;

    while ( q != *root )
    {
        /* if the node to be deleted is found */
if ( q -> data == num )
        {
            *found = true ;
            *x = q ;
            return ;
        }

        *par = q ;

        if ( q -> data > num )
        {
            if ( q -> isleft == true )
            {
                *found = false ;
                x = NULL ;
                return ;
            }
            q = q -> left ;
        }
        else
        {
            if ( q -> isright == true )
            {
                *found = false ;
                *x = NULL ;
                return ;
            }
            q = q -> right ;
        }
    }
}

/* traverses the threaded binary tree in inorder */
void inorder ( struct thtree *root )
{
    struct thtree *p ;

    p = root -> left ;

    while ( p != root )
    {
        while ( p -> isleft == false )
            p = p -> left ;

        printf ( "%d\t", p -> data ) ;

        while ( p -> isright == true )
        {
            p = p -> right ;

            if ( p == root )
                break ;

            printf ( "%d\t", p -> data ) ;

        }
        p = p -> right ;
    }
}

void deltree ( struct thtree **root )
{
    while ( ( *root ) -> left != *root )
        delete ( root, ( *root ) -> left -> data ) ;
}

Program to insert and delete a node from the binary search tree.

Program to insert and delete a node from the binary search tree.

Code for Program to insert and delete a node from the binary search tree in C Programming

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

#define TRUE 1
#define FALSE 0

struct btreenode
{
    struct btreenode *leftchild ;
    int data ;
    struct btreenode *rightchild ;
} ;

void insert ( struct btreenode **, int ) ;
void delete ( struct btreenode **, int ) ;
void search ( struct btreenode **, int, struct btreenode **,
                struct btreenode **, int * ) ;
void inorder ( struct btreenode * ) ;

void main( )
{
    struct btreenode *bt ;
    int req, i = 0, num, a[ ] = { 11, 9, 13, 8, 10, 12, 14, 15, 7 } ;

    bt = NULL ;  /* empty tree */


    clrscr( ) ;

    while ( i <= 8 )
    {
        insert ( &bt, a[i] ) ;
        i++ ;
    }
    clrscr( ) ;
    printf ( "Binary tree before deletion:\n" ) ;
    inorder ( bt ) ;

    delete ( &bt, 10 ) ;
    printf ( "\nBinary tree after deletion:\n" ) ;
    inorder ( bt ) ;

    delete ( &bt, 14 ) ;
    printf ( "\nBinary tree after deletion:\n" ) ;
    inorder ( bt ) ;

    delete ( &bt, 8 ) ;
    printf ( "\nBinary tree after deletion:\n" ) ;
    inorder ( bt ) ;

    delete ( &bt, 13 ) ;
    printf ( "\nBinary tree after deletion:\n" ) ;
    inorder ( bt ) ;
}

/* inserts a new node in a binary search tree */
void insert ( struct btreenode **sr, int num )
{
    if ( *sr == NULL )
    {
        *sr = malloc ( sizeof ( struct btreenode ) ) ;

        ( *sr ) -> leftchild = NULL ;
        ( *sr ) -> data = num ;
        ( *sr ) -> rightchild = NULL ;
    }
    else/* search the node to which new node will be attached */

    {
        /* if new data is less, traverse to left */
if ( num < ( *sr ) -> data )
            insert ( &( ( *sr ) -> leftchild ), num ) ;
        else/* else traverse to right */

            insert ( &( ( *sr ) -> rightchild ), num ) ;
    }
}

/* deletes a node from the binary search tree */
void delete ( struct btreenode **root, int num )
{
    int found ;
    struct btreenode *parent, *x, *xsucc ;

    /* if tree is empty */
if ( *root == NULL )
    {
        printf ( "\nTree is empty" ) ;
        return ;
    }

    parent = x = NULL ;

    /* call to search function to find the node to be deleted */

    search ( root, num, &parent, &x, &found ) ;

    /* if the node to deleted is not found */
if ( found == FALSE )
    {
        printf ( "\nData to be deleted, not found" ) ;
        return ;
    }

    /* if the node to be deleted has two children */
if ( x -> leftchild != NULL && x -> rightchild != NULL )
    {
        parent = x ;
        xsucc = x -> rightchild ;

        while ( xsucc -> leftchild != NULL )
        {
            parent = xsucc ;
            xsucc = xsucc -> leftchild ;
        }

        x -> data = xsucc -> data ;
        x = xsucc ;
    }

    /* if the node to be deleted has no child */
if ( x -> leftchild == NULL && x -> rightchild == NULL )
    {
        if ( parent -> rightchild == x )
            parent -> rightchild = NULL ;
        else
            parent -> leftchild = NULL ;

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only rightchild */
if ( x -> leftchild == NULL && x -> rightchild != NULL )
    {
        if ( parent -> leftchild == x )
            parent -> leftchild = x -> rightchild ;
        else
            parent -> rightchild = x -> rightchild ;

        free ( x ) ;
        return ;
    }

    /* if the node to be deleted has only left child */
if ( x -> leftchild != NULL && x -> rightchild == NULL )
    {
        if ( parent -> leftchild == x )
            parent -> leftchild = x -> leftchild ;
        else
            parent -> rightchild = x -> leftchild ;

        free ( x ) ;
        return ;
    }
}

/*returns the address of the node to be deleted, address of its parent and
   whether the node is found or not */
void search ( struct btreenode **root, int num, struct btreenode **par, struct
        btreenode **x, int *found )
{
    struct btreenode *q ;

    q = *root ;
    *found = FALSE ;
    *par = NULL ;

    while ( q != NULL )
    {
        /* if the node to be deleted is found */
if ( q -> data == num )
        {
            *found = TRUE ;
            *x = q ;
            return ;
        }

        *par = q ;

        if ( q -> data > num )
            q = q -> leftchild ;
        else
            q = q -> rightchild ;
    }
}

/* traverse a binary search tree in a LDR (Left-Data-Right) fashion */
void inorder ( struct btreenode *sr )
{
    if ( sr != NULL )
    {
        inorder ( sr -> leftchild ) ;

        /* print the data of the node whose leftchild is NULL or the path  has
            already been traversed */

        printf ( "%d\t", sr -> data ) ;

        inorder ( sr -> rightchild ) ;
    }
}

Program to maintain a heap.

Program to maintain a heap.

Code for Program to maintain a heap in C Programming


#include <stdio.h>
#include <conio.h>

void restoreup ( int, int * ) ;
void restoredown ( int, int *, int ) ;
void makeheap ( int *, int ) ;
void add ( int, int *, int * ) ;
int replace ( int, int *, int ) ;
int del ( int *, int * ) ;

void main( )
{
    int arr [20] = { 1000, 7, 10, 25, 17, 23, 27, 16,
                    19, 37, 42, 4, 33, 1, 5, 11 } ;
    int i, n = 15 ;

    clrscr( ) ;
    makeheap ( arr, n ) ;

    printf ( "Heap:\n" ) ;
    for ( i = 1 ; i <= n ; i++ )
        printf ( "%d\t", arr [i] ) ;

    i = 24 ;
    add ( i, arr, &n ) ;

    printf ( "\n\nElement added %d.\n", i ) ;
    printf ( "\nHeap after addition of an element:\n" ) ;
    for ( i = 1 ; i <= n ; i++ )
        printf ( "%d\t", arr [i] ) ;

    i = replace ( 2, arr, n ) ;
    printf ( "\n\nElement replaced %d.\n", i ) ;
    printf ( "\nHeap after replacement of an element:\n" ) ;
    for ( i = 1 ; i <= n ; i++ )
        printf ( "%d\t", arr [i] ) ;

    i = del ( arr, &n ) ;
    printf ( "\n\nElement deleted %d.\n", i ) ;
    printf ( "\nHeap after deletion of an element:\n" ) ;
    for ( i = 1 ; i <= n ; i++ )
        printf ( "%d\t", arr [i] ) ;

    getch( ) ;
}

void restoreup ( int i, int *arr )
{
    int val ;
    val = arr [i] ;
    while ( arr [i / 2] <= val )
    {
        arr [i] = arr [i / 2] ;
        i = i / 2 ;
    }
    arr [i] = val ;
}

void restoredown ( int pos, int *arr, int n )
{
    int i, val ;
    val = arr [pos] ;
    while ( pos <= n / 2 )
    {
        i = 2 * pos ;
        if ( ( i < n ) && ( arr [i] < arr [i + 1] ) )
            i++ ;
        if ( val >= arr [i] )
            break ;
        arr [pos] = arr [i] ;
        pos = i ;
    }
    arr [pos] = val ;
}

void makeheap ( int *arr, int n )
{
    int i ;
    for ( i = n / 2 ; i >= 1 ; i-- )
        restoredown ( i, arr, n ) ;
}

void add ( int val, int *arr, int *n )
{
    ( *n ) ++ ;
    arr [*n] = val ;
    restoreup ( *n, arr ) ;
}
int replace ( int i, int *arr, int n )
{
    int r = arr [1] ;
    arr [1] = i ;
    for ( i = n / 2 ; i >= 1 ; i-- )
        restoredown ( i, arr, n ) ;
    return r ;
}

int del ( int *arr, int *n )
{
    int val ;
    val = arr [1] ;
    arr [1] = arr [*n] ;
    ( *n ) -- ;
    restoredown ( 1, arr, *n ) ;
    return val ;
}

Write a program to calculate x raise to y or power(x,y) using while loop.

Code for Program to calculate x raise to y or power(x,y) using while loop in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>

void main()
{
    int x,y;
    double power();
    clrscr();

    printf("Enter value of x : ");
    scanf("%d",&x);
    printf("Enter value of y : ");
    scanf("%d",&y);

    printf("%d to power %d is = %f\n",x,y,power(x,y));
    getch();
}
double power(x,y)
int x,y;
{
    double p;
    p=1.0;
    if(y>=0)
        while(y--)
            p*=x;
    elsewhile(y++)
            p/=x;
    return(p);
}

WRITE A PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY THOSE PERSONS WHOSE BETWEEN 50 AND 60.

Code for PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY THOSE PERSONS WHOSE BETWEEN 50 AND 60 in C Programming

#include<stdio.h>
#include<conio.h>

void main()
{
    int i,n,age[100],count=0;

    clrscr();

    printf("Enter the number of persons  ::  ");
    scanf("%d",&n);

    for (i=1;i<=n;i++)
    {
        printf("\nEnter age of %d persons :: ",i);
        scanf("%d",&age[i]);
    }

    for (i=1;i<=n;i++)
    {
        if(age[i]>50 && age[i] < 60)
        count++;
        elsecontinue;
    }

    printf("\n\nNumber of persons whose age between 50-60 are :: %d",count);
    getch();
}


/*
    **********
    OUTPUT
    **********

    Enter the number of persons  ::  6

    Enter age of 1 persons :: 10

    Enter age of 2 persons :: 20

    Enter age of 3 persons :: 30

    Enter age of 4 persons :: 55

    Enter age of 5 persons :: 51

    Enter age of 6 persons :: 56


    Number of persons whose age between 50-60 are :: 3

Write a program to interchange the Small and Capital Letters.




Code for Program to interchange the Small and Capital Letters in C Programming

 # include <dos.h>


 void interrupt (*OldInterruptFunction)( );
 void interrupt NewInterruptFunction( );


 int main( )
 {
    OldInterruptFunction=getvect(0x17);
    setvect(0x17,NewInterruptFunction);

    keep(0,(_SS+(_SP/16)-_psp));

    return 0;
 }

 /*************************************************************************///---------------------  NewInterruptFunction( )  -----------------------///*************************************************************************/void interrupt NewInterruptFunction( )
 {
    if(_AH==0x00)
    {
       if(_AL>='a' && _AL<='z')
      _AL-=32;

       elseif(_AL>='A' && _AL<='Z')
      _AL+=32;
    }

    (*OldInterruptFunction)( );
 }

write a program that takes a number from user and calculates its logarithm value to the base 10 and e, exponentiation, sin value, cosine value and square root.

write a program that takes a number from user and
calculates its logarithm value to the base 10 and
e, exponentiation, sin value, cosine value and
square root.

Code for program that takes a number from user and calculates its logarithm value to the base 10 and e, exponentiation, sin value, cosine value and square root in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
        float n,log,ex,sinv,cosv,sq;
        clrscr();
        printf("\n\n PLEASE ENTER THE VALUE OF N: ");
        scanf("%f",&n);
        log=log10(n);
        ex=exp(n);
        sinv=sin(n);
        cosv=cos(n);
        sq=sqrt(n);
        printf("\n\n THE VALUE OF N IS %.4f .",n);
        printf("\n\n THE VALUE OF LOGARITHAM BASE 10 IS %.4f .",log);
        printf("\n\n THE VALUE OF EXPONENTIATION IS %.4f .",ex);
        printf("\n\n THE VALUE OF SIN VALUE IS %.4f .",sinv);
        printf("\n\n THE VALUE OF COSINE VALUE IS %.4f .",cosv);
        printf("\n\n THE VALUE OF SQURE ROOT IS %.4f .",sq);
    getch();
}


*********************** OUTPUT*************************************************

     PLEASE ENTER THE VALUE OF N: 5


     THE VALUE OF N IS 5.0000 .

     THE VALUE OF LOGARITHAM BASE 10 IS 0.6990 .

     THE VALUE OF EXPONENTIATION IS 148.4132 .

     THE VALUE OF SIN VALUE IS -0.9589 .

     THE VALUE OF COSINE VALUE IS 0.2837 .

     THE VALUE OF SQURE ROOT IS 2.2361 .

calculate average of the elements of an array and then the average deviation using barrier.

Write a program to calculate average of the elements of an array and then the average deviation using barrier.



Code for Program to calculate average of the elements of an array and then the average deviation using barrier in C Programming

# include <stdio.h>
# include <math.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"
# include "barrier.h"int main()
{
    int arr[100];
    int *bararr;        // Barrier Arrayint arrSize;        //Size of arrayint iCount;        //Counter Variableint id;            //Process IDint nProc=3;        //number of Processesfloat sum=0;
    
    float *deviation,*avg;  // Shared Variablesint *lock;        // Shared Variable for Spinlockint shmidavg,shmidlock,shmiddeviation,shmidbararr;    // Shmid for Shared Variables
    
    printf("Enter the Size of an Array :");
    scanf("%d",&arrSize);

    for(iCount=0;iCount<arrSize;iCount++)
    {
        printf("Enter arr[%d] :",iCount);
        scanf("%d",&arr[iCount]);
    }

    /* Allocate Shared memory */
    avg=(float*)sshared(sizeof(float),&shmidavg);
    deviation=(float*)sshared(sizeof(float),&shmiddeviation);
    lock=(int*)sshared(sizeof(int),&shmidlock);
    bararr=(int*)sshared(sizeof(int)*4,&shmidbararr);

    spin_lock_init(lock);        // Spin Lock Initialization
    
    barrier_init(bararr,nProc);    // Barrier Initialization
    
    *avg=0;

    id=process_fork(nProc);        // Forking Processes/* Partial Sum using Loop Spliting */for(iCount=id;iCount<arrSize;iCount=iCount+nProc)
    {
        sum = sum + arr[iCount];
    }

    spin_lock(lock);
        /* Critical Region */
        *avg = *avg + ( sum /arrSize);    // Calculate Average from Partial Sums
    spin_unlock(lock);

    /*    ---------------------------------------------------------------------------    Barrier Should be here, So that all Processes have to wait until Final    Average is Calculated.    ---------------------------------------------------------------------------    */
    
    barrier(bararr);    // Barrier Called
    
    
    sum=0;
    /* Calculate Sum(Xi - Mean) Using Loop Spliting */for(iCount=id;iCount<arrSize;iCount=iCount+nProc)
    {
        sum = sum + pow(((float)arr[iCount] - *avg),2);
    }
    
    spin_lock(lock);
        /* Critical Region */
        *deviation = *deviation + (sum / arrSize-1); // Calculate Final Deviation
    spin_unlock(lock);

    
    process_join(nProc,id);        // Joining the Process

    *deviation=sqrt(*deviation);
    printf("Deviation : %f\n",*deviation);

    /* Cleaning the Shared Region */
    cleanup_memory(&shmidavg);
    cleanup_memory(&shmidlock);
    cleanup_memory(&shmiddeviation);
    cleanup_memory(&shmidbararr);
    return 0;
}

Program to add two polynomials.

Program to add two polynomials.

Code for Program to add two polynomials in C Programming

#include <stdio.h>
#include <conio.h>

#define MAX 10

struct term
{
    int coeff ;
    int exp ;
} ;

struct poly
{
    struct term t [10] ;
    int noofterms ;
} ;


void initpoly ( struct poly * ) ;
void polyappend ( struct poly *, int c, int e ) ;
struct poly polyadd ( struct poly, struct poly ) ;
void display ( struct poly ) ;

void main( )
{
    struct poly p1, p2, p3 ;

    clrscr( ) ;

    initpoly ( &p1 ) ;
    initpoly ( &p2 ) ;
    initpoly ( &p3 ) ;

    polyappend ( &p1, 1, 7 ) ;
    polyappend ( &p1, 2, 6 ) ;
    polyappend ( &p1, 3, 5 ) ;
    polyappend ( &p1, 4, 4 ) ;
    polyappend ( &p1, 5, 2 ) ;

    polyappend ( &p2, 1, 4 ) ;
    polyappend ( &p2, 1, 3 ) ;
    polyappend ( &p2, 1, 2 ) ;
    polyappend ( &p2, 1, 1 ) ;
    polyappend ( &p2, 2, 0 ) ;

    p3 = polyadd ( p1, p2 ) ;

    printf ( "\nFirst polynomial:\n" ) ;
    display ( p1 ) ;

    printf ( "\n\nSecond polynomial:\n" ) ;
    display ( p2 ) ;

    printf ( "\n\nResultant polynomial:\n" ) ;
    display ( p3 ) ;

    getch( ) ;
}

/* initializes elements of struct poly */
void initpoly ( struct poly *p )
{
    int i ;
    p -> noofterms = 0 ;
    for ( i = 0 ; i < MAX ; i++ )
    {
        p -> t[i].coeff = 0 ;
        p -> t[i].exp = 0 ;
    }
}

/* adds the term of polynomial to the array t */
void polyappend ( struct poly *p, int c, int e )
{
    p -> t[p -> noofterms].coeff = c ;
    p -> t[p -> noofterms].exp =  e ;
    ( p -> noofterms ) ++ ;
}

/* displays the polynomial equation */
void display ( struct poly p )
{
    int flag = 0, i ;
    for ( i = 0 ; i < p.noofterms ; i++ )
    {
        if ( p.t[i].exp != 0 )
            printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;
        else
        {
            printf ( "%d", p.t[i].coeff ) ;
            flag = 1 ;
        }
    }
    if ( !flag )
        printf ( "\b\b  " ) ;

}

/* adds two polynomials p1 and p2 */
struct poly polyadd ( struct poly p1, struct poly p2 )
{
    int i, j, c ;
    struct poly p3 ;
    initpoly ( &p3 ) ;

    if ( p1.noofterms > p2.noofterms )
        c = p1.noofterms ;
    else
        c = p2.noofterms ;

    for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
    {
        if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
            break ;
        if ( p1.t[i].exp >= p2.t[j].exp )
        {
            if ( p1.t[i].exp == p2.t[j].exp )
            {
                p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
                p3.t[p3.noofterms].exp = p1.t[i].exp ;
                i++ ;
                j++ ;
            }
            else
            {
                p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
                p3.t[p3.noofterms].exp = p1.t[i].exp ;
                i++ ;
            }
        }
        else
        {
            p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
            p3.t[p3.noofterms].exp = p2.t[j].exp ;
            j++ ;
        }
    }
    return p3 ;
}