Cover Image

Wednesday, 1 October 2014

Program that accepts two integers as input and prints their sum as output. Each integer will have at least 25 digits.(Done in Turboc3)


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

struct list
{
                int num;
                struct list *next;
};
typedef struct list node;
void main()
{
                node* getnum(void);
                node* add(node*,node*);
                node* rev_num(node*);
                void putnum(node*);

                node *num1=NULL,*num2=NULL,*sum=NULL;

                clrscr();

                printf("Enter any number : ");
                num1=getnum();
                printf("Enter another number : ");
                pum2=getnum();
                putnum(num1);
                printf(" + ");
                putnum(num2);
                printf(" = ");
                sum=add(num1,num2);
                putnum(sum);
                getch();
}
node* getnum(void)
{
                char *str;
                node *head=NULL,*temp;
                gets(str);
                while(*str!='\0')
                {
                                if(head==NULL)
                                {
                                                head=(node*)malloc(sizeof(node));
                                                head->num=(*str)-'0';
                                                head->next=NULL;
                                                temp=head;
                                }
                                else
                                {
                                                temp->next=(node*)malloc(sizeof(node));
                                                temp->next->num=(*str)-'0';
                                                temp->next->next=NULL;
                                                temp=temp->next;
                                }
                                str++;
                }
                printf("\n");
                return head;
}
void putnum(node *num)
{
                node *temp=num;
                while(temp)
                {
                                printf("%d",temp->num);
                                temp=temp->next;
                }
}
node* rev_num(node *num)
{

                node *temp=num,*nxt,*prev=NULL;

                while(temp!=NULL)
                {
                                nxt=temp->next;
                                temp->next=prev;
                                prev=temp;
                                temp=nxt;
                }
                num=prev;
                return num;
}
node* add(node *num1,node *num2)
{
                int carry=0;
                node *sum=NULL,*temp;
                num1=rev_num(num1);
                num2=rev_num(num2);
                while(num1!=NULL||num2!=NULL)
                {
                                if(sum==NULL)
                                {
                                                sum=(node*)malloc(sizeof(node));
                                                if(num1==NULL) sum->num=num2->num+carry;
                                                else if(num2==NULL) sum->num=num1->num+carry;
                                                else sum->num=num1->num+num2->num+carry;
                                                if(sum->num>=10)
                                                {
                                                                sum->num%=10;
                                                                carry=1;
                                                }
                                                else carry =0;

                                                sum->next=NULL;
                                                temp=sum;

                                }
                                else
                                {
                                                temp->next=(node*)malloc(sizeof(node));
                                                if(num1==NULL) temp->next->num=num2->num+carry;
                                                else if(num2==NULL) temp->next->num=num1->num+carry;
                                                else temp->next->num=num1->num+num2->num+carry;

                                                if(temp->next->num>=10)
                                                {
                                                                temp->next->num%=10;
                                                                carry=1;

                                                }
                                                else carry=0;
                                                temp=temp->next;
                                                temp->next=NULL;
                                }
                                if(num1==NULL) num2=num2->next;
                                else if(num2==NULL) num1=num1->next;
                                else
                                {
                                                num1=num1->next;
                                                num2=num2->next;
                                }

                }
                if(carry)
                {
                                temp->next=(node*)malloc(sizeof(node));
                                temp->next->num=carry;
                                temp=temp->next;
                                temp->next=NULL;
                }

                return sum=rev_num(sum);
}

Output :


Wednesday, 24 September 2014

Sparse matrix 3 tuple representation

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,mat[5][5],nonzero=0,row,column;
printf("Enter the size of matrix (max 5x5) : ");
scanf("%dx%d",&row,&column);
printf("Enter the elements of the matrix %dx%d :\n",row,column);
for(i=0;i<row;i++)
for(j=0;j<column;j++)
{
scanf("%d",&mat[i][j]);
if(mat[i][j]!=0)
nonzero++;
}
// 3 tuple representation
printf("\nThree tuple representation\n\n");
printf("%5d%5d%5d",row,column,nonzero);
printf("\n----------------------------\n");
for(i=0;i<row;i++)
for(j=0;j<column;j++)
{
if(mat[i][j]!=0)
printf("%5d%5d%5d\n",i,j,mat[i][j]);
}
}

Output:


Triangular matrix (using array)

#include<stdio.h>
#include<conio.h>
#define size 4
void main()
{
int mat[size][size]={0},i,j;
printf("Enter 6 elements of matrix :\n");
for(i=0;i<3;i++)
for(j=0;j<=i;j++)
scanf("%d",&mat[i+1][j]);

printf("Triangluar matrix so formed is :\n");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
printf("%3d",mat[i][j]);
printf("\n");
}
getch();
}

Output :


Application of Linked List (Addition of polynomials)

#include<iostream>
using namespace std;
typedef struct list node;
struct list
{
int coeff,expo;
node *next;
};
void disp_poly(node*);
int count(node*);
node* create_poly(int);
node* add_poly(node*,node*);
node* copyof(node*);

int main()
{
int num;
node *poly1,*poly2,*sum=NULL;
cout<<"For first polynomial enter the number of terms : ";
cin>>num;
poly1=create_poly(num);
cout<<"Polynomial so formed is : ";
disp_poly(poly1);
cout<<endl<<endl;
cout<<"For second polynomial enter the number of terms : ";
cin>>num;
poly2=create_poly(num);
cout<<"Polynomial so formed is : ";
disp_poly(poly2);
cout<<endl<<endl;
cout<<"After addition :\n";
disp_poly(poly1);
disp_poly(poly2);
cout<<" = ";
sum=add_poly(poly1,poly2);
disp_poly(sum);
return 0;
}
node* create_poly(int num)
{
node *poly=NULL,*temp;
for(int i = 1;i<=num;i++)
{
    if(poly==NULL)
    {
    poly=new node;
    poly->next=NULL;
    cout<<"Enter the coefficient and exponent for term "<<i<<" : ";
    cin>>poly->coeff>>poly->expo;
    temp=poly;
  }
  else
  {
    temp->next=new node;
    temp->next->next=NULL;
    cout<<"Enter the coefficient and exponent for term "<<i<<" : ";
    cin>>temp->next->coeff>>temp->next->expo;
    temp=temp->next;
  }
}
return poly;
}
void disp_poly(node *poly)
{
node *temp=poly;
while(temp)
{
  cout<<"+"<<temp->coeff<<"x^"<<temp->expo;
  temp=temp->next;
}
}
node* copyof(node *poly)
{
node  *sum=NULL,*temp,*temp2=poly;
while(temp2)
{
  if(sum==NULL)
  {
    sum=new node;
    sum->next=NULL;
    sum->coeff=temp2->coeff;
    sum->expo=temp2->expo;
    temp=sum;
  }
  else
   {
    temp->next=new node;
    temp->next->next=NULL;
    temp->next->coeff=temp2->coeff;
    temp->next->expo=temp2->expo;
    temp=temp->next;
  }
  temp2=temp2->next;
}
return sum;
}
int count(node *poly)
{
int c=0;
node *temp=poly;
while(temp)
{
  c++;
  temp=temp->next;
}
return c;
}
node* add_poly(node *poly1,node *poly2)
{
int sizeof_poly1,sizeof_poly2,found;
node *temp,*sum=NULL,*sum_temp;

sizeof_poly1=count(poly1);
sizeof_poly2=count(poly2);

if(sizeof_poly1>sizeof_poly2)
{
  sum=copyof(poly1);
  temp=poly2;
  while(temp)
  {
    found=0;
    sum_temp=sum;
    while(sum_temp)
    {
      if(sum_temp->expo==temp->expo)
      {
          sum_temp->coeff+=temp->coeff;
        found++;
      }
      sum_temp=sum_temp->next;
    }
    if(!found)
    {
      for(sum_temp=sum;sum_temp->next;sum_temp=sum_temp->next);// pointing to last node
      sum_temp->next=new node;
      sum_temp->next->next=NULL;
      sum_temp->next->coeff=temp->coeff;
      sum_temp->next->expo=temp->expo;
    }
    temp=temp->next;
  }
}
else
{
  sum=copyof(poly2);
  temp=poly1;
  while(temp)
  {
    found=0;
    sum_temp=sum;
    while(sum_temp)
    {
      if(sum_temp->expo==temp->expo)
      {
        sum_temp->coeff+=temp->coeff;
        found++;
       }
      sum_temp=sum_temp->next;
    }
    if(!found)
    {
      for(sum_temp=sum;sum_temp->next;sum_temp=sum_temp->next);// pointing to last node
      sum_temp->next=new node;
      sum_temp->next->next=NULL;
      sum_temp->next->coeff=temp->coeff;
      sum_temp->next->expo=temp->expo;
    }
    temp=temp->next;
  }
}
return sum;
}

Output :