Sunday, June 8, 2008

A program demonstrating various operations on integer array


/* Subhranath Chunder - A program demonstrating various operations on integer array */

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

/* Funtion declarations */
void printarray1(int[],int);
void printarray2(int[],int);
int maximum(int[],int);
int minimum(int[],int);
void ascend(int[],int);
void descend(int[],int);
int search(int,int[],int);
void insertion(int,int,int[],int);
void deletion(int,int[],int);
void merge(int,int[],int,int[]);
void reverse(int,int[]);

/* Main Program */
void main()
{
int inp[100],n,option=0,i,temp1,temp2;
clrscr();

/* User inputs the elements of the array */
printf("Enter the no. of elements you would like to enter: ");
scanf("%d",&n);
printf("\nEnter the numbers: ");
for(i=0;i<n;++i)
scanf("%d",&inp[i]);

/* Main Menu - where the user chooses what he wants to do */
while(option==0)
{
clrscr();
printf("You can perform the following options on the array.\n\n1. Find the maximum and minimum elements.\n2. Sort.\n3. Search.\n4. Insertion.\n5. Deletion.\n6. Merging.\n7. Reversing.\n8. Exit\n\nEnter your option: ");
scanf("%d",&option);

/* Shows the maximum and minimum element of the array */
if(option==1)
{
clrscr();
printf("The maximum element of the array is: %d",maximum(inp,n));
printf("\n\nThe minimum element of the array is: %d",minimum(inp,n));
getch();
option=0;
}

/* Sort the array either in ascending or descending order */
while(option==2)
{
clrscr();
printf("Sort the array:\n\n1. In Ascending Order.\n2. In Descending Order.\n\nEnter Choice: ");
scanf("%d",&option);
if(option==1 || option==2)
{
if(option==1)
ascend(inp,n);
if(option==2)
descend(inp,n);
printarray2(inp,n);
getch();
option=0;
}
else
option=2;
}

/* Provides the option to search for the position of an element in the array */
if(option==3)
{
clrscr();
printf("Enter the value of the element you want to search: ");
scanf("%d",&temp1);
temp2=search(temp1,inp,n);
if(temp2==-1)
printf("\n\nValue not found.");
else
printf("\n\n%d was found at the following position: %d",temp1,temp2);
getch();
option=0;
}

/* Inserts an element at any user defined position of the array */
if(option==4)
{
clrscr();
printarray1(inp,n);
printf("\n\nEnter the position of the array where you want to insert a new element: ");
scanf("%d",&temp1);
printf("\nEnter the element: ");
scanf("%d",&temp2);
insertion(temp2,temp1,inp,n);
++n;
printarray2(inp,n);
getch();
option=0;
}

/* Removes an element from any given position of the array */
if(option==5)
{
clrscr();
printarray1(inp,n);
printf("\n\nEnter the position of the element that you want to delete: ");
scanf("%d",&temp1);
deletion(temp1,inp,n);
--n;
printarray2(inp,n);
getch();
option=0;
}

/* Merges a set of elements with the existing array */
if(option==6)
{
int inp2[100];
clrscr();
printarray1(inp,n);
printf("\n\nEnter the no elements you would like to merge with the existing array: ");
scanf("%d",&temp1);
printf("Enter the elements: ");
for(i=0;i<temp1;++i)
scanf("%d",&inp2[i]);
merge(n,inp,temp1,inp2);
n=n+temp1;
printarray2(inp,n);
getch();
option=0;
}

/* Reverses an element of the array */
if(option==7)
{
clrscr();
printarray1(inp,n);
printf("\n\nEnter the position of the element that you want to reverse: ");
scanf("%d",&temp1);
reverse(temp1,inp);
printarray2(inp,n);
getch();
option=0;
}

/* Exits the program */
if(option==8)
break;

/* Returns the control back to main menu if an invalid option is selected from the main menu */
else
option=0;
}
}
/* Main Program Ends */

/* Prints the structure of the array */
void printarray1(int a[],int n)
{
int i;
printf("\nThe structure of the array is given below:\n\n------------------\n%8s%10s\n------------------","Position","Element");
for(i=0;i<n;++i)
printf("\n%8d%10d",i,a[i]);
printf("\n------------------\n");
}

/* Prints the structure of the array after any modification */
void printarray2(int a[],int n)
{
int i;
printf("\nThe structure of the modified array is given below:\n\n------------------\n%8s%10s\n------------------","Position","Element");
for(i=0;i<n;++i)
printf("\n%8d%10d",i,a[i]);
printf("\n------------------\n\n<Press any key to continue>\n");
}

/* Returns the value of the maximum element of an array */
int maximum(int a[],int n)
{
int max,i;
max=a[0];
for(i=1;i<n;++i)
{
if(a[i]>max)
max=a[i];
}
return(max);
}

/* Returns the value of the minimum element of an array */
int minimum(int a[],int n)
{
int min,i;
min=a[0];
for(i=1;i<n;++i)
{
if(a[i]<min)
min=a[i];
}
return(min);
}

/* Sorts an array in ascending order */
void ascend(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;++i)
{
for(j=0;j<n-1-i;++j)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

/* Sorts an array in descending order */
void descend(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;++i)
{
for(j=0;j<n-1-i;++j)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

/* Searches for the position of any particular value from an array */
int search(int key,int a[],int n)
{
int i;
for(i=0;i<n;++i)
{
if(a[i]==key)
return(i);
}
return(-1);
}

/* Inserts an element at any given position of an existing array */
void insertion(int element,int pos,int a[],int n)
{
int i;
for(i=n;i>pos;--i)
a[i]=a[i-1];
a[pos]=element;
}

/* Deletes an element from any given position of an existing array */
void deletion(int pos,int a[],int n)
{
int i;
for(i=pos;i<n-1;++i)
a[i]=a[i+1];
}

/* Merges a set of elements with an existing array */
void merge(int n,int a[],int m,int b[])
{
int i,j;
for(i=n,j=0;i<m+n;++i,++j)
a[i]=b[j];
}

/* Reverse the value of an element at any given position of an array */
void reverse(int pos,int a[])
{
int i,sum=0;
while(a[pos]>0)
{
sum=sum*10+a[pos]%10;
a[pos]/=10;
}
a[pos]=sum;
}

No comments: