Array Operation

Problem statement
Write a c program to do the following
Takes inputs from use and stores them in an array
search() :search an element in the array
sinsert(): insert a element in the array
sdelete(): deletes given element from the array
printarray(): prints the array
intersection(): takes three array and stores intersection of two arrays in the third array
difference():takes three array and stores that element in the third array which are not common in two arrays

#include<stdio.h>
int search (int key, int arr[]);
int sinsert(int n,int arr[] );
int sdelete(int n,int arr[]);
void printarray(int arr[]);
void intersection(int arr1[],int arr2[],int arr3[]);
void difference(int arr1[],int arr2[],int arr3[]);
int main()
{
  int size1,size2,arr1[20],arr2[20],arr3[20],ans,to_del,to_add,to_search,i,y;
  printf("enter the dimention of string one \n");
  scanf("%d",&size1);
  printf("now enter the array \n");
  arr1[0]=size1;
  for(i=1;i<=size1;i++)
    {
      scanf("%d",&arr1[i]);
    }
  printf("enter the number to be deleted \n");
  scanf("%d",&to_del);
  ans=sdelete(to_del,arr1);
  if(ans==1)
    {
      printf("number deleted \n");
      printarray(arr1);
    }
  else
    {
      printf("number does not exist");
    }
  printf("enter the number to be added \n");
  scanf("%d",&to_add);
  ans=sinsert(to_add,arr1);
  if(ans==1)
    {
      printf("number added to the list \n");
      printarray(arr1);
    }
  else
    {
      printf("number already exist \n");
    }
  printf("enter the number to be searched  \n");
  scanf("%d",&to_search);
  ans=search(to_search,arr1);
  if(ans==-1)
    {
      printf("not found \n");
    }
  else
    {
      printf("the key found at %d \n",ans);
    }
  printf("enter the dimention of string two \n");
  scanf("%d",&size2);
  printf("now enter the array \n");
  arr2[0]=size2;
  for(i=1;i<=size2;i++)
    {
      scanf("%d",&arr2[i]);
    }
  intersection(arr1,arr2,arr3);
  difference(arr1,arr2,arr3);
  scanf("%d",&y);
  return 0;
}
int search (int key, int arr[])
{
  int i=1;
  while(arr[i]<key)
    {
      i++;
    }
  if(arr[i]==key)
    {
      return i;
    }
  else
    {
      return -1;   // this returns the actual position of key in the array
    }
}
int sinsert(int n,int arr[] )
{
  int i=1,temp;
  if(search(n,arr)==(-1))
    {
      while(arr[i]<n)
{
 i++;
}
      for(temp=arr[0]+1;temp>i;temp--)
      {
arr[temp]=arr[temp-1];
      }
      arr[i]=n;
      arr[0]=arr[0]+1;
      return 1;
    }
    else
      {
        return 0;
      }
}
int sdelete(int n,int arr[])
{
  int temp,i;
  temp=search(n,arr);
  if(temp==(-1))
    {
      return 0;
    }
  else
    {
      for(i=temp;i<arr[i];i++)
        {
 arr[i]=arr[i+1];
        }
      arr[0]=arr[0]-1;
      return 1;
    }
}
void printarray(int arr[])
{
  int i;
  printf("the array is \n");
  for(i=1;i<=arr[0];i++)
    {
      printf("%d ",arr[i] );
    }
  printf("\n");
  return ;
}  
void intersection(int arr1[],int arr2[],int arr3[])
{
  int temp,index=1;
  arr3[0]=0;
  for(temp=1;temp<=arr1[0];temp++)
    {
      if(search(arr1[temp],arr2)!=-1)
        {
          arr3[index]=arr1[temp];
 arr3[0]=arr3[0]+1;
 index++;
}
    }
  printf("intersetion list \n");
  printarray(arr3);
  return ;
}
void difference(int arr1[],int arr2[],int arr3[])
{
  int temp1,temp2,index=1,flag;
  arr3[0]=0;
  for(temp1=1;temp1<=arr1[0];temp1++)
    {
      flag=0;
      for(temp2=1;temp2<=arr2[0];temp2++)
{
 if(arr1[temp1]==arr2[temp2])
   {
     flag=1;
     break;    
   }
}
      if(flag==0)
{
 arr3[index]=arr1[temp1];
 index++;
 arr3[0]++;
}
    }
  printf("difference list \n");
  printarray(arr3);
  return ;
}



No comments:

Post a Comment