Write a program to insert the element at the given index in an array or list | MindTap Computing for Malik's C++ Programming 8th Edition

Introduction:

Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert Item in the array at the position specified by index. If index is out of range, output an appropriate message.

Source: MindTap Computing for Malik's C++ Programming From Problem Analysis to Program Design, 8th Edition, [Instant Access] (8th)-Malik, chapter 16, programming exercises 7.
Solved!

The objective of the programming exercise is to write a function that takes 4 arguments array, size of the array, the index where the element will be inserted, element to be inserted. and insert elements to the index given in the array. size of the array will be incremented.

Program:


// write function that takes 4 argument array, size of the array,
// index where the element will be inserted, element to be inserted.
// and insert element to index given in the array. size of the array will be incremented.

#include <iostream>

using namespace std;

// insertAt() that takes array, array size, index and insertItem.
// and insert elemnet at index given
void insertAt(int arr[], int &n, int index, int insertItem)
{
    // is index is less than 0.
    if (index < 0)
    {
        // print this msg.
        cout << "Index can not be less than 0!" << endl;
       return;
    }
    // else if index is greater than or equal to n.
    else if (index >= n)
    {
        // print this msg.
        cout << "Index can not be greater than or equal to " << n << "!" << endl;
       return;
    }  

    // increment size of array n.
    n++;
    // initialize j to n.
    int j = n;
    // use while loop j form n to index with decrement of 1.
    while (j >= index)
    {
        // assign value of previous element to current element.
        arr[j] = arr[j - 1];
        // decrement of 1.
        j--;
    }
    // assign insertItem to arr[index]
    // (insert at index.)
    arr[index] = insertItem;
}

// driver code.
int main()
{
    // declare array size.
    int n;
    cout << "Enter array size(<=1000): ";
    // take user input for n.
    cin >> n;

    // declare array with size 1000.
    int arr[1000];
    cout << "Enter array element" << endl;
    // take user input using for loop.
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    // declare index, insertItem
    int index, insertItem;
    // take user input .
    cout << "Enter index where element to be inserted: ";
    cin >> index;
    cout << "Enter element needed to insert at " << index << " index: ";
    cin >> insertItem;

    // calling function insertAt.
    insertAt(arr, n, index, insertItem);
    // print array using for loop.
    cout << "After inserting element " << insertItem << " at " << index << " index: ";
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;

    // end.
    return 0;
}

Output:






Image:






Post a Comment

0 Comments