The maximum size of my array is 20 but I want the array to display only items that have been entered. I dont want the zeros to be considered in the array in all my operations like sorting and printing etc. Can anyone help.
#include<iostream>
#include<iomanip>
using namespace std;
void newelement_pos (int [], int);
void newelement_end(int [], int);
int linearsearch(int [], int);
int binarysearch(int [], int);
int sortcheck (int [], int);
void delete_pos(int [], int);
void delete_no(int[], int);
void selsort(int *, int);
void print (int [],int);
void bubblesort (int *, int);
const int size=20;
int a[size]={};
int main ()
{
int n=0,h,k;
cout<<"Please type in the element \n";
cin>>a[0];
while (n!=9)
{
cout<<"please enter your choice \n "<<"1. Insert new element at a certain position \n"<<"2. Insert new element at end \n"<<"3. Find an element using linear search \n"<<"4. Find an element of a sorted list using binary search \n"<<"5. Delete an element that is at a certain position \n"<<"6. Delete an element by using its ISBN number \n"<<"7. Sort the list by the ISBN numbers (using selection sort) \n"<<"8. Sort the list by the ISBN numbers (using bubble sort \n"<<"9. enter 9 to exit \n";
cin>>n;
switch (n)
{
case 1:
newelement_pos(a, size);
break;
case 2:
newelement_end(a,size);
break;
case 3:
h=linearsearch(a,size);
if(h!=-1)
cout<<"the no is present at:"<<h<<endl<<endl;
else
cout<<"the no is not present \n";
break;
case 4:
k=binarysearch(a,size);
if(k!=-1 && k!=-2)
cout<<"the no is present at:"<<k<<endl;
else if (k==-1)
cout<<"the no is not present \n";
else
cout<<"the list is not sorted \n";
break;
case 5:
delete_pos(a,size);
break;
case 6:
delete_no(a,size);
break;
case 7:
selsort(a,size);
break;
case 8:
bubblesort(a,size);
break;
default:
if (n!=9)
cout<<"wrong choice \n";
break;
}
}
return 0;
}
void newelement_pos (int a[],int size)
{ int pos, no;
cout<<" enter the position to be filled \n";
cin>>pos;
cout<<" enter the ISBN No: \n";
cin>>no;
if (pos<=20)
{
if (a[pos-1]==0)
{
a[pos-1]=no;
}
else
{
for (int i=size-1; i>=pos-1;i--)
{ a[i+1]=a[i];}
a[pos-1]=no;
}
cout<<" the new book list \n";
print(a,size);
}
else
cout<<"the position is out of bounds \n";
}
void print (int a[], int size)
{cout<<"the list now is \n";
for (int i=0;i<size;i++)
{
cout<<a[i]<<setw(5);
}
cout<<endl;
}
void newelement_end(int a[], int)
{int z=0,no;
cout<<"Please enter the ISBN No \n";
cin>>no;
for( int i=0;( a[i]!=0);i++)
z=i+1;
a[z]=no;
print(a,size);
}
int linearsearch(int a[], int size)
{ int no;
cout<<"enter the ISBN no to be searched \n";
cin>>no;
for(int j=0;j<size;j++)
if(a[j]==no)
return j+1;
return -1;
}
int binarysearch(int a[], int size)
{ int no, upper=size-1, lower=0, middle,k;
k=sortcheck(a,size);
if(k==1)
{
cout<<"enter the ISBN no to be searched \n";
cin>>no;
while(lower<=upper)
{
middle=(upper+lower)/2;
if(no==a[middle])
return middle+1;
else
if(no<a[middle])
upper=middle-1;
else
lower=middle+1;
}
return -1;
}
else
return -2;
}
void delete_pos(int a[],int size)
{
int pos;
cout<<"enter the position of the element to be deleted \n";
cin>>pos;
if (pos<=20)
{
a[pos-1]=0;
for (int i=pos;i<size;i++)
a[i-1]=a[i];
print(a,size);
}
else
cout<<"position is out of bounds /n";
}
void delete_no(int a[], int size)
{ int no,l;
cout<<"enter the no to be deleted \n";
cin>>no;
for(int j=0;j<size;j++)
if(a[j]==no)
l=j;
for (int i=l+1;i<size;i++)
a[i-1]=a[i];
print(a,size);
}
void selsort(int *a, int size)
{ int temp,j,smallest,i=0,sm;
for(int i=0;i<size;i++)
{
smallest=*(a+i);
sm=i;
for (j=i+1;j<size;j++)
if(*(a+j)<smallest)
{
smallest=a[j];
sm=j;
}
temp=*(a+i);
*(a+i)=*(a+(sm));
*(a+(sm))=temp;
}
print(a,size);
}
void bubblesort (int *a, int size)
{ int temp, j;
for(int i=0;i<size-1;i++)
for (j=0;j<size-1;j++)
if(*(a+j)>*(a+(j+1)))
{
temp=*(a+j);
*(a+j)=*(a+(j+1));
*(a+(j+1))=temp;
}
print(a,size);
}
int sortcheck(int a[], int size)
{
for (int i=1;i<size;i++)
if(a[i]<=a[i+1])
{
return 1;
}
return -1;
}