Check The Programming Section
We needs to build some mechanism to check a given array (displayed above) is full or not. For this purpose, we will take help from a count variable, which will increment when an item is inserted and any point of program execution count variable can inform us about the status of the array.
The first row represents an array after declaration, filled with garbage values and count is set to 0. Thereafter, an item 3 is given as input and the count is increment by 1. Thereafter,13 is given as input and count is further incremented by 1. So at any given point of execution, we can check array is full of not.
#include<iostream>
using namespace std;
int main(){
int num[10];
int size = sizeof num/sizeof num[0];
int count = 0;
char status;
do{
cin>>num[count];
count++;
cout<<"Do you wants to enter more item? y/n";
cin>>status;
}while(count<size && status=='y');
cout<<"array has "<<count<<" items";
}
the variable size has the length of the array num.
the count variable declared and initialized to 0
the do-while loop can execute maximum 10 times (equal to the size of the array), for 10 inputs with condition ‘y’ for yes or ‘n’ for no
cin>>num[count]; take a input through console
count++; increments the value of count by 1 after given one input
cin>>status; will ask, that do you wants to enter another item or not as ‘y’ for yes and ‘n’ for no
If count is equal to the size of the array, then insertion is not possible. Now let's start insertion an item to the array and can be possible any of these three possible location of the array:
insert at the begaining of the array at index 0
insert at the end of the array at index size-1
insert in between two existing elements
Suppose, we wants to insert 20 at the begining of the array. So, the following steps we needs to perform:
Before insert 20 at index [0], we needs to make space for 20 at index [0].
To make space for new item we needs to shift all the items by next corresponding position. See the image below:
Insert an item at the begining of the array
We needs to shift the items from back side. Means item at position [1] needs to be shift first and then item at position [0]. Number of shifting is equal to the value of count.
#include<iostream>
using namespace std;
int main(){
int num[10];
int size = sizeof num/sizeof num[0];
int count = 0;
int item;
char status;
do{
cout<<"Enter an item ::";
cin>>num[count];
count++;
cout<<"Do you wants to enter more item? y/n::";
cin>>status;
}while(count<size && status=='y');
cout<<"array has "<<count<<" items"<<endl;
cout<<"Enter an item for insert at benigning ::";
cin>>item;
//first check array is full or not
if(count<size){
//items are shifted to next corresponding position
for(int i=count-1;i>=0;i--){
num[i+1] = num[i];
}
num[0] = item;
count++;
cout<<"After inserting an item at begining...."<<endl;
for(int i=0;i<count;i++){
cout<<num[i]<<" ";
}
}
else{
cout<<"Arrary is full";
}
}
the condition if(count<size) will check array is full or not. If not, then shifiting of array items is performed inside the for loop.
the statement int i = count-1; set the the loop at position 2 of the array
the statement insdie loop num[i+1] = num[i]; shifts the array item of i-th position to its next corresponding position i+1
shifting will continue till the first item of the array and it is checked by the condition i>=0;
once shifting is over, then the new item is inserted at the begining of the array with statement num[0] = item; and count is incrementd by 1
Like previous scenario, inserting an item in between two items also required shifting of items and the followings things we needs to do:
To insert 10 at position 2, we needs to shift all the items from back side of the arary till the given position 2.
The number of shifting operation is equal to count - pos = 1
Inserting 10 at position 3
#include<iostream>
using namespace std;
int main(){
int num[10];
int size = sizeof num/sizeof num[0];
int count = 0;
int item, pos;
char status;
do{
cout<<"Enter an item ::";
cin>>num[count];
count++;
cout<<"Do you wants to enter more item? y/n::";
cin>>status;
}while(count<size && status=='y');
cout<<"array has "<<count<<" items"<<endl;
cout<<"Enter a position to insert ::";
cin>>pos;
cout<<"Enter an item for insert ::";
cin>>item;
if(count<size){
for(int i=count-1;i>=pos;i--){
num[i+1] = num[i];
}
//inserting an item to a given position
num[pos] = item;
count++;
}
cout<<"After inserting an item at begining...."<<endl;
for(int i=0;i<count;i++){
cout<<num[i]<<" ";
}
}
The shifting is started from the last position, the loop variable set to the index of last element int i = count -1;
Items are shifted till the position for insertion of the new item i>=pos;
Loop is iterate in backward direction. So the loop variable is decremented by 1
After shifting of the elements the new items is inserted to a given position by num[pos] = item; and count is incremented by 1.
This is one of the easiest way of inserting a new item at the end of the array. No shifting operation is required before insertion. We simply put the new item at the position pointing by count variable and rerquired code is (edit and try)
#include<iostream>
using namespace std;
int main(){
int num[10];
int size = sizeof num/sizeof num[0];
int count = 0;
int item;
char status;
do{
cout<<"Enter an item ::";
cin>>num[count];
count++;
cout<<"Do you wants to enter more item? y/n::";
cin>>status;
}while(count<size && status=='y');
cout<<"array has "<<count<<" items"<<endl;
cout<<"Enter an item for insert at the end::";
cin>>item;
if(count<size){
num[count] = item;
count++;
}
cout<<"After inserting an item at begining...."<<endl;
for(int i=0;i<count;i++){
cout<<num[i]<<" ";
}
}
No loop is required insdie the if statement, like previous two scenario. Because no shifting operation is required
Item is inserted by num[count] = item; and count is incremented by 1.