Pages

Wednesday, September 14, 2011

Array Data Type & Functions

 An array is a systematic arrangement of objects, usually in rows and columns.

Generally, a collection of data items that can be selected by indices computed at run-time, including:   
  • Array data structure, an arrangement of items at equally spaced addresses in computer memory
  • Array data type, used in a programming language to specify a variable that can be indexed
  • Associative array, an abstract data structure model that generalizes arrays to arbitrary indices


Objective: Given an array of N integers, write a program to perform following operations on the array based user's choice:
          -Insertion of elements at perticular location
          -Deletion of specific element
          -searching of perticular element
          -merging of list of elements
          -array traversing               
  

          //  Source Code   //

#include<stdio.h>     //including header files//
#include<conio.h>     //including header files//
void insertion(int[],int);         //function declaration//
void deletion(int[],int);          //function declaration//
void duplidel(int[],int);          //function declaration//
void search(int[],int);            //function declaration//
void merge(int[],int[],int);       //function declaration//


int main()
{
int a[20],b[20],n,e,i;

printf("enter the no of elements you wanna enter:");
scanf("%d",&n);                    //enter no of elements of array//
printf("enter array elements in assending order:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);                 //entering elements of array//
printf("select\n 1: insertion \n 2: deletion \n 3: deletion of duplicate \n 4:search \n 5:merge");
scanf("%d",&e);
switch (e)              //testing user's choice //
{
case 1:
insertion( a,n);        //calling insertion function//
break;
case 2:
deletion( a, n);       //calling deletion function//
break;
case 3:
duplidel( a, n);      //calling duplicate delete function//
break;

case 4:

search ( a, n);     //calling search function//
break;
case 5:
{
printf("enter the second array:\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
merge( a,b,n);
}
break;
default:
printf("invalid selection");
break;
}
getch();
}

void insertion (int a[20],int n)  //function definition//
{ int e,pos,t,i;                  //variable declaration//
printf("enter the element to be inserted ");
scanf("%d",&e);
printf("enter the position at which the above element is to be inserted");
scanf("%d",&pos);
for(i=pos-1;i<n+1;i++)
{
t=a[i];
a[i]=e;
e=t;
}

  printf("the array after insertion is:");
  for(i=0;i<n+1;i++)
  printf("%d\t",a[i]);
  getch();
  }
void deletion( int a[20], int n)  //function definition//
{  int i,e,t,pos;
printf("enter the element u wanna delete:");
scanf("%d",&e);
for(i=0;i<n;i++)
{
if( a[i]==e)
{
pos=i;
break;
}
}
for(i=pos;i<n;i++)
a[i]=a[i+1];
for(i=0;i<n-1;i++)
printf("%d\t",a[i]);
getch();
}
void search(int a[],int n)        //function definition//
 {
 int i, flag=0,no;
 printf("enter the no u wanna search:");
 scanf("%d",&no);
 for(i=0;i<n;i++)
 {
 if (a[i]==no);
        {
            printf("%d found at %d position",no,i+1);
            flag=1;
            break;
        }
}
if (flag==0)
printf("%d not found",no);
getch();
}
void merge(int a[],int b[],int n)     //function definition//
{  int c[20],i,j;
for(i=0;i<n;i++)
c[i]=a[i];
for(j=0;j<n;j++)
c[j+i]=b[j];
printf("The array after merging is:");
for(i=0;i<2*n;i++)

printf("%d\t",c[i]);
}
void duplidel(int a[], int n)
{
int i, j, k,temp,m=0,pos;
for(i=0;i<n-m;i++)
{
    temp = a[i];
    for(j=i+1;j<n-m;j++)
    {
        if(a[j]==temp)
        {
            m++;
            for(k=j;k<n-m;k++)
            a[k]=a[k+1];
        }
    }

}
printf("The array after deletion of duplicate element is:");
for(i=0;i<n-m;i++)
printf("%d\t",a[i]);
}

1 comment:

  1. explain duplicate deletion....i didnt understand

    ReplyDelete