Update Function in C++


This code is an implementation of inserting an element in an array at a specific position. Here's how it works:

  • The user enters the size of the array (up to 10 elements) and the values of the array.
  • The program displays the values entered by the user.
  • The user is prompted to enter the position at which they want to insert an element and the value to be inserted.
  • The program creates a new array and copies the elements of the original array up to the insertion point.
  • The value to be inserted is placed in the new array.
  • The remaining elements of the original array are copied into the new array.
  • The program displays the original array and the new array, with the inserted value.

Source Code

#include<iostream>
using namespace std;
int main()
{
    cout<<"\n Welcome";
    int i,t,a[10],n,s,j=0,b[10];
    cout<<"\nEnter the Limit:";
    cin>>n;
    cout<<"\nEnter the values:";
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<"\nGiven values are:";
    for(i=0;i<n;i++)
    {

        cout<<"a["<<i<<"]="<<a[i];
    }
    cout<<"\nEnter the position to be changed:";
    cin>>t;
    cout<<"\nEnter the value to be changed:";
    cin>>s;
    for(i=0;i<t;i++)
    {
        b[j]=a[i];
        j++;
    }
    b[j]=s;
    j++;
    for(i=t;i<n;i++)
    {
        b[j]=a[i];
        j++;
    }
    cout<<"\nInserted value is:";
    for(i=0;i<=n;i++)
    {
        cout<<"\n"<<a[i]<<"="<<b[i];
    }
    return 0;
}
To download raw file Click Here

Output

Welcome
Enter the Limit:4
Enter the values:1
2
3
4
Given values are:a[0]=1a[1]=2a[2]=3a[3]=4
Enter the position to be changed:2
Enter the value to be changed:2
Inserted value is:
1=1
2=2
3=2
4=3
1907379984=4

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples