Question

Write a C++ function to insert new items into an array and move the rest of...

  1. Write a C++ function to insert new items into an array and move the rest of the contents down. If we assume that there are places left in the array:

int array[10] = {1, 2, 3};

There are 7 places left in the array to allow insertion. Below is the header for the function you are to write:

void insertInArray(int arr[], const int position, const int newItem)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code-

#include <iostream>
using namespace std;
//function declare
void insertInArray(int arr[], const int position, const int newItem);
int main()
{
//variable declare
int arr[100], position, i, n, newItem;
//ask user to enter number of elements in array
cout<<"Enter number of elements in array\n"<<endl;
cin>>n;
//enter element in array
cout<<"Enter elements "<<n<<endl;
//store element in array arr
for (i = 0; i < n; i++)
cin>>arr[i];

//location of the element to be inserted ask user
cout<<"Enter the location where you wish to insert an element\n"<<endl;
cin>>position;
//ask user value to insert
cout<<"Enter the value to insert\n"<<endl;
cin>>newItem;
//function call insertInArray
insertInArray(arr,position,newItem);
//print the array after insertion
cout<<"Resultant arr is\n"<<endl;

for (i = 0; i <= n; i++)
cout<<arr[i]<<" ";

return 0;
}
//function to insert element in array
void insertInArray(int arr[], const int position, const int newItem){
//variable declare
int i =0,n=100;
//shift the element forward
for (i = n - 1; i >= position - 1; i--)
arr[i+1] = arr[i];
//insert the element at position
arr[position-1] = newItem;

}

Screenshots -

Add a comment
Know the answer?
Add Answer to:
Write a C++ function to insert new items into an array and move the rest of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • In the blank below, write the contents of array arr after the following code is executed...

    In the blank below, write the contents of array arr after the following code is executed up to the comment indicated in the main procedure below? #include <iostream> #include <algorithm> using namespace std; void do something(int list[], const int size); int main() { const int SIZE(10); int arr[SIZE] = { 5, 8, 1, 7, 3, 9, 4, 6, 10, 2 }; do somethingCarr, SIZE); ** Write the contents of array arr in the space below when execution reaches here. */...

  • Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the...

    Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the source code (shown below) to output the following: Inserting elements to array list: The list contains 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Deleting elements: The list contains: 2 4 6 8 10 12 14 16 18 20 22 24 this is a programming problem in...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an...

    using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an array manipulation program that allows the user is to pre much whatever meant to ananas Wananching the program the user will passat are the contains a set of value and that w 2 Check to see the could be opened at the program was not opened the continue 3. Create an array and with the values from Presente en de h and powermany reded...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • Write a small program that uses a function to add array elements from an array into...

    Write a small program that uses a function to add array elements from an array into a linked list in-order. Use the following array: const int size=5; int arr[size] = { 4, 1, 7, 2 ,3 }; Output: Array out of order: 4 1 7 2 3 Array in order using Linked List : 1 ,2, 3, 4, 7 Your program should display the linked list in-order using the array given.

  • Write a C++ function that prints an array, skipping over a number of elements. Use the...

    Write a C++ function that prints an array, skipping over a number of elements. Use the following header: void printArraySkip(int array[], int elems, int skip) { } Where 'array' is an input array of 'elem' integer elements, and 'skip' is the number of elements to skip over. For example, if the function is called like this: int array[6] = { 5, 7, 11, 12, 18, 19 }; printArraySkip(array, 6, 2); The output would be: 11 19 in c++. Thanks

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT