Question: Write a simple function in C++ to insert an element in an array. Given an array with const int CAPACITY = 10 and the following

Write a simple function in C++ to insert an element in an array.

Given an array with const int CAPACITY = 10 and the following parameters: the array, the number of elements in the array, the element to insert, and the index where the element needs to be inserted. You can use a FOR loop.

Example:

Initial Array: 8 4 2 6 7 8 2

Insert 80 at idx 7... Modified array: 8 4 2 6 7 8 2 80

or

Initial Array: 7 4 6 2 Insert 50 at idx 2... Modified array: 7 4 50 6 2

If the array is full, use cerr to output the following error message "Array is full.

- If the index is larger than the number of elements in the array (for example, the array contains 10 elements and the index is 11 or larger), output the error message using cerr "You can only insert contiguous elements in the array.")

- If the index exceeds the capacity, output the error message "The array cannot have more than number of elements."

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!