File:1.InseElem.jpg

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

1.InseElem.jpg(58 × 73 pixels, file size: 2 KB, MIME type: image/jpeg)

Captions

Captions

Add a one-line explanation of what this file represents

Summary[edit]

Description
English: /*1.Write a program to insert an element anywhere using array,

menu driven program*/

  1. include<iostream>

using namespace std;

  1. define MAX 5

void Display(int a[],int& size){

   cout<<endl;
   cout<<"Array : [ ";
   for(int i=0;i<size;i++){
       cout<<a[i];
       if(i < size-1){
           cout<<", ";
       }
   }
   cout<<" ]";

}

void Insert(int a[],int& size){

   if(size == MAX){
       cout<<endl<<"Insufficient space for insertion!";
   }else{
       int x,pos;
       cout<<"Enter the element to insert : ";
       cin>>x;
       while(true){    
           cout<<endl<<"Enter the position : ";
           cin>>pos;
           if(pos < 0 || pos > size ){
               cout<<endl<<"Invalid position....Position should be from 0 to "<<size<<"!"<<endl;
           }else{break;}
       }
       for(int i=size;i>pos;i--){
           a[i] = a[i-1];
       }
       a[pos] = x;
       size++;
       cout<<endl<<"Element ("<<x<<") succesfully inserted at position ("<<pos<<")!";
   }

}

int main(){

   int size;
   while(true){
       cout<<endl<<"Enter the size of the array : ";
       cin>>size;
       if(size > MAX || size < 1){
           cout<<endl<<"Size should be between 1 and "<<MAX<<"!..Re-Try!";
       }else{break;}
   }
   int a[MAX];
   cout<<"Enter the elements of the array :-"<<endl;
   for(int i=0;i<size;i++){
       cin>>a[i];
   }
   int choice;
   do{
       cout<<endl<<endl;
       cout<<"1. Insert Element"<<endl
           <<"2. Display Array"<<endl
           <<"0. Exit"<<endl;
       cout<<"Enter your choice : ";
       cin>>choice;
       switch(choice){
           case 0 : cout<<endl<<endl<<"Exiting.........."<<endl; exit(0); break;
           case 1 : Insert(a,size); break;
           case 2 : Display(a,size); break;
           default : cout<<"Invalid choice....Re-Try!"<<endl;
       }
   }while(choice != 0);
return 0;
}
Date
Source Own work
Author Bcyeahh

Licensing[edit]

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current19:15, 15 October 2023Thumbnail for version as of 19:15, 15 October 202358 × 73 (2 KB)Bcyeahh (talk | contribs)Uploaded own work with UploadWizard

There are no pages that use this file.

Metadata