05/09/2017
ALL POSSIBLE ARRAY OPERATIONS:
// array_all.cpp : Defines the entry point for the console application.
//
void insert(int*,int);
void del(int*,int);
void display(int*,int);
void reverse(int*, int);
void remove_duplicate(int*, int);
void search(int*, int);
void replace(int*, int);
int main()
{
int a[20],no, choice,b[20],i,e=1,no1;
printf("how many no to be inserted: ");
scanf_s("%d", &no);
for (i = 0; i < no; i++)
{
printf("no[%d]=", e);
scanf_s("%d", &a[i]);
e++;
}
system("cls");
printf("1=DISPLAY 2=INSERTION 3=DELETION 4=REVERSE 5=REMOVE DUPLICATE 6=SEARCH 7=REPLACE 0=EXIT\n");
while (1)
{
printf("\nWHAT DO YOU WANT TO DO: ");
scanf_s("%d", &choice);
switch (choice)
{
case 1:display(a, no);
break;
case 2:insert(a, no);
no = no + 1;
break;
case 3:del(a, no);
no = no - 1;
break;
case 0:exit(0);
break;
case 4:reverse(a, no);
break;
case 5:remove_duplicate(a, no);
break;
case 6:search(a, no);
break;
case 7:replace(a, no);
break;
default:printf(".......III......");
break;
system("cls");
}
}
_getch();
}
void display(int *a,int no)
{
int j, f = 1;
printf("elements are: ");
for (j = 0; j < no; j++)
{
printf("\nno[%d]=%d", f, *(a+j));
f++;
}
}
void insert(int *a, int no)
{
int pos, val,temp,c;
printf("enter the pos: ");
scanf_s("%d",&pos);
printf("enter thye valoe: ");
scanf_s("%d",&val);
for (c = no - 1; c >= pos - 1; c--)
a[c + 1] = a[c];
a[pos - 1] = val;
}
void del(int *a, int no)
{
int element,found=0,j,pos;
printf("Enter the element to be deleted\n");
scanf_s("%d", &element);
for (j = 0; j < no; j++)
{
if (a[j] == element)
{
found = 1;
pos = j;
break;
}
}
if (found == 1)
{
for (j = pos; j < no - 1; j++)
{
a[j] = a[j + 1];
}
}
else
printf("Element %d is not found in the vector\n", element);
}
void reverse(int *a,int no)
{
int k, j, temp;
for (k = 0, j = k + no - 1; k < j; k++, j--)
{
temp = *(a + k);
*(a + k) = *(a + j);
*(a + j) = temp;
}
}
void remove_duplicate(int *a, int no)
{
int i,j,e=1,c,position,n,temp=0;
for (i = 0; i < no-1; i++)
{
for (j = i + 1; j < no; j++)
{
if (a[i] == a[j])
{
position = j;
for (c = position; c < no - 1; c++)
{
a[c] = a[c + 1];
}
no = no - 1;
}
}
}
for (c = 0; c < no; c++)
{
printf("%d ", a[c]);
}
}
void search(int *a, int no)
{
int search,i;
printf("ENTER THE ELEMENT TO SEARCH: ");
scanf_s("%d", &search);
i = 0;
while (i < no && search != a[i]) {
i++;
}
if (i < no)
{
printf("Number found at the location = %d", i);
}
else
{
printf("Number not found");
}
}
void replace(int *a, int no)
{
int x, y,i;
display(a,no);
printf("\nenter the element to replace : ");
scanf_s("%d", &x);
printf("\nreplace with : ");
scanf_s("%d", &y);
i = 0;
while (i < no && x!= a[i])
{
i++;
}
if (i < no)
{
a[i] = y;
}
else
{
printf("Number not found");
}
}