A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write C++ program to insert an element in array
Q:

Write C++ program to insert an element in array

0

Write C++ program to insert an element in array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#define MAX_SIZE 100 //Maximum size of the array
using namespace std;
 
int main()
{
 
    int arr[100];
    int i, num, size, position;
 
    // Reading size and elements of array
    cout<<"Enter size of the array :";
    cin>>size;
    cout<<"Enter elements in array : ";
    for(i=0; i<size; i++)
    {
        cin>>arr[i];
    }
 
    //Reading element to insert & position of the element
 
    cout<<"Enter element to insert : ";
    cin>>num;
    cout<<"Enter the element position : ";
    cin>>position;
 
    //checking elements valis position
 
    if(position>size+1 || position<=0)
    {
        cout<<"Invalid position! Please enter position between 1 to "<<num;
    }
    else
    {
        //Inserting element in an array & increasing the size of the array
 
        for(i=size; i>=position; i--)
        {
            arr[i] = arr[i-1];
        }
        arr[position-1] = num;
        size++;
 
         // Printing new array with new element
 
        cout<<"Array elements after insertion : ";
        for(i=0; i<size; i++)
        {
           cout<<arr[i]<<"\t";
        }
    }
 
    return 0;
}

Result:

Enter size of the array :5

Enter elements in array : 1

2

3

4

5

Enter element to insert : 20

Enter the element position : 2

Array elements after insertion : 1      20      2       3       4       5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now