#include<stdio.h>
#include<iostream>
using namespace std;
#define MAX 100
void show(int array[], int sz);
int main()
{
int array[MAX], i, sz=0, choice, value, ind, c;
bool exit_flag = false;
while(true)
{
printf("Enter Choice:\n");
printf("1\t-\tShow\n");
printf("2\t-\tSearch\n");
printf("3\t-\tInsert\n");
printf("4\t-\tDelete\n");
printf("5\t-\tUpdate\n");
printf("0\t-\tExit\n");
scanf("%d", &choice);
switch(choice)
{
case 0: //exit
exit_flag = true;
break;
case 1: //show
printf("Elements: ");
show(array, sz);
break;
case 2: //search
printf("Value to Search: ");
scanf("%d", &value);
for(i=0;i<sz;i++)
{
if(array[i] == value) //found
{
printf("%d found at index %d\n", value, i);
break;
}
}
if(i==sz) //not found
printf("%d not found!", value);
break;
case 3: //insert
printf("Where do you want to insert?\n");
printf("1\t-\tBeginning\n");
printf("2\t-\tEnd\n");
printf("3\t-\tSpecific Index\n");
scanf("%d", &c);
printf("Value to Insert: ");
scanf("%d", &value);
if(c==1) //beginning
{
sz++;
for(i=sz-1;i>=1;i--)
array[i] = array[i-1];
array[i] = value;
cout<<"\n insert arr :"<<array[i];
}
else if(c==2) //end
{
sz++;
array[sz-1] = value;
}
else if(c==3) //index
{
printf("Enter Index: ");
scanf("%d", &ind);
if(ind > sz-1)
{
printf("Invalid Index to Insert\n");
}
else
{
sz++;
for(i=sz-1;i>=ind;i--)
array[i] = array[i-1];
array[ind] = value;
}
}
printf("\nElements after insert: ");
show(array, sz);
break;
case 4: //delete
printf("From where do you want to delete?\n");
printf("1\t-\tBeginning\n");
printf("2\t-\tEnd\n");
printf("3\t-\tSpecific Index\n");
scanf("%d", &c);
if(c==1)
{
for(i=0;i<sz-1;i++)
array[i] = array[i+1];
sz--;
}
else if(c==2)
sz--;
else if(c==3)
{
printf("Enter Index: ");
scanf("%d", &ind);
if(ind > sz-1)
{
printf("Invalid Index!\n");
}
else
{
for(i=ind;i<sz-1;i++)
array[i] = array[i+1];
sz--;
}
}
printf("\nElements after delete: ");
show(array, sz);
break;
case 5: //update
printf("Enter index to update: ");
scanf("%d", &ind);
printf("Enter value to update: ");
scanf("%d", &value);
if(ind >=sz)
printf("Invalid Index\n");
else
{
array[ind] = value;
printf("\nElements after update: ");
show(array, sz);
}
break;
default:
printf("Invalid Choice!\n");
}
if(exit_flag)
break;
}
return 0;
}
void show(int array[], int sz)
{
for(int i=0;i<sz;i++)
{
if(i>0)
printf(", ");
printf("%d", array[i]);
}
printf("\n\n");
}