Tuesday, 31 May 2016

Addition and Subtraction of Matrix

//Addition and Subtraction of Matrix..

#include<stdio.h>
main()
{
int a[10][10], b[10][10], s[10][10],sub[10][10];
int r,c,r1=0, c1=0, r2=0, c2=0;

printf("Enter row no.    ");
scanf("%d",&r1);
printf("Enter column no. ");
scanf("%d",&c1);
printf("Enter the elements of first array");
for(r=0; r<r1; r++)
{
   for(c=0; c<c1; c++)
   {
       scanf("%d",&a[r][c]);
   }
}

printf("Enter row no.    ");
scanf("%d",&r2);
printf("Enter column no. ");
scanf("%d",&c2);

if(r1==r2 && c1==c2)
{
printf("Enter the elements of second array");
for(r=0; r<r2; r++)
{
   for(c=0; c<c2; c++)
   {
      scanf("%d",&b[r][c]);
   }
}

//addition & subtraction
for(r=0; r<r1; r++)
{
   for(c=0; c<c1; c++)
   {
       s[r][c] =  a[r][c]+b[r][c];
       sub[r][c]= a[r][c]-b[r][c];
   }
}

printf("Additiom is\n");
for(r=0; r<r1; r++)
{
   for(c=0; c<c1; c++)
   {
       printf("%d\t",s[r][c]);
   }
   printf("\n");
}
printf("\n\n");

printf("Subtraction is\n");
for(r=0; r<r1; r++)
{
   for(c=0; c<c1; c++)
   {
       printf("%d\t",sub[r][c]);
   }
   printf("\n");
}
}
else
{
    printf("Invalid matrix");
}
}

Tuesday, 17 May 2016

// Inserting an element in an array

#include<stdio.h>

main()
{
void insert(int[],int,int,int);

int x=0,p,top,i;
int a[20];

printf("Enetr the no. of elements you wanna insert(max 20)");
scanf("%d",&top);

printf("Enter elements in array");
for(i=0; i<=top-1; i++)
{
   scanf("%d",&a[i]);
}

printf("\nEnter the element to insert in array");
scanf("%d",&x);
printf("\nEnter the position to enter the element");
scanf("%d",&p);

if(p==19)
printf("array full");
else
insert(a,p,x,top);
}

void insert(int b[], int p, int x, int t)
{
int i=0;
for(i=0; i<=t-1; i++)
printf("%d",b[i]);
for(i=t-1; i>=p-1; i--)
{
  b[i+1]=b[i];
}
  b[p-1]=x;
for(i=0; i<=t; i++)
   printf("%d ",b[i]);
}