In C++Create a function called CopyAndInsertArray.  It should take in two arrays (the first array should be of size n and the second array should be of size n+1).  The function should also take in a position and a number. It should copy the first array into the second array and place the new number at the proper position.  In other words, you are creating a function that inserts a value into an array.

Question
Asked Sep 18, 2019
36 views

In C++

Create a function called CopyAndInsertArray.  It should take in two arrays (the first array should be of size n and the second array should be of size n+1).  The function should also take in a position and a number. It should copy the first array into the second array and place the new number at the proper position.  In other words, you are creating a function that inserts a value into an array.

check_circle

Expert Answer

Step 1

Program design

The overall program design is as follows:

  • The function CopyAndInsertArray would accept 4 parameters – source array, destination array, insert position and element to be inserted
  • the function would use a insert (or shift) status flag to indicate if we are past the insert position
  • using the insert / shift status flag we undertake the copy operation
  • elements before the insert position are copied into the same index of the destination array, elements after the insert position are copied into a shifted index position in the destination array
  • the main function tests the use of the function in the given C++ program

The program logic is explained in detail in the embedded program comments.

Step 2

Program

#include <iostream>

 

using namespace std;

 

//assuming the original array size

int const SIZE = 10;

 

void CopyAndInsertArray(int a[], int b[],int p, int n);

 

int main(){

    //declare and initialize the source array

    int a1[SIZE] = {1,2,3,4,5,6,7,8,9,10};

    //declare the destination array

    int a2[SIZE + 1];

    //make function call for copy and insert array

    CopyAndInsertArray(a1,a2,7,11);

    //demonstrate the funtion use by

    //printing out the destination array

    for (int j=0; j < SIZE+1; j++){

        cout << a2[j] << " ";

    }

    cout << endl;

}

 

//required function

void CopyAndInsertArray(int a[], int b[],int p, int n){

    //declare array index for manipulation

    int i;

    //declare flag for indicating shift status

    //will be true from insert position onwards

    //initialzied to false value

    bool shifted = false;

    //iterate over the source array elements

    for (i = 0; i < SIZE; i++){

        //do the following if the cuurent array element is

        //before or at the insert position

        if(!shifted){

            //if the current element is at

            //the insert position

            if (i == p - 1){

                //assign the current position

                //of the destination array with the 

                //element to be inserted

                b[i] = n;

                //turn shift status flag to true

                shifted = true;

                //assign the element of the current position

                //in the source array to the next position

                //of the destination array

                b[i+1] = a[i];

            }

            //if the current position is not at the 

            //insert position but any position before it

            else

                b[i] = a[i];

        }

        //if the current position is beyond the insert position

        //i.e. shift status is true

        //then assign the current (i)-th element of the source array

        //to the position of the destination incremented by 1 position

        //i.e. to (i+1)-th position

        else

            b[i+1] = a[i];

    }

}

Step 3

Program im...

#include <iostream>
using namespace std
4
/assuming the original array size
int const SIZE 10;
6
7
8
CopyAndInsertArray(int a[], int b[],int p, int n);
int main()
//declare and initialize the source array
10
11
int al[SIZE] {1,2,3,4,5,6,7,8, 9, 10};
1/declare the destination array
int a2[SIZE 1];
/make function call for copy and insert array
12
13
14
15
CopyAndInsertArray (a1,a2,7,11);
//demonstrate the funtion use by
//printing out the destination array
(int j-0; < SIZE+1; j){
cout << a2[l " ";
16
17
18
19
20
21
22
cout << endl;
23
}
24
/required function
25
help_outline

Image Transcriptionclose

#include <iostream> using namespace std 4 /assuming the original array size int const SIZE 10; 6 7 8 CopyAndInsertArray(int a[], int b[],int p, int n); int main() //declare and initialize the source array 10 11 int al[SIZE] {1,2,3,4,5,6,7,8, 9, 10}; 1/declare the destination array int a2[SIZE 1]; /make function call for copy and insert array 12 13 14 15 CopyAndInsertArray (a1,a2,7,11); //demonstrate the funtion use by //printing out the destination array (int j-0; < SIZE+1; j){ cout << a2[l " "; 16 17 18 19 20 21 22 cout << endl; 23 } 24 /required function 25

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: 21,22,23

A: An image contains white, black or different shades of grey from black (maximum gray) to white (minim...

question_answer

Q: In C++ Create a function called QuadraticFormula that takes in a,b, and c and does the quadratic for...

A: Program AlgorithmRead the value of coefficients and call the function QuadraticFormula (num1, num2, ...

question_answer

Q: Use the class, WhatPrints, to view what prints for each of the method calls below:doSomething(5);doS...

A: Explanation:In the given program, there are four methods which has the same method name with differe...

question_answer

Q: Is a honeypot entrapment or is it a way of observing the details of illegal computer activities?

A: Honeypot:A honeypot is a computer security mechanism set up as a decoy for luring malicious attacker...

question_answer

Q: 1) Create a array of Fahrenheit temperatures, as follows int fahr []= {0,1,2,3,4,5,6,7,8,9,10,32,33,...

A: Program Instructions:Define the given array in code and calculate the size of the array.Use for loop...

question_answer

Q: What is the output of the following code: public class A{public int methodOfA(int i){i += 10;return ...

A: Given program:File name: A.java

question_answer

Q: Complete the following list comprehensionlc5 = [ for c in 'bu be you']so that it produces...

A: Let’s define a python function which outputs True if the alphabet is ‘b’ or ‘u’, because the above l...

question_answer

Q: Develop pseudocode to read in the radius of a sphere, calculate the volume of the sphere (using the ...

A: Pseudocode is an informal approach of programming description that does not need any exact programmi...

question_answer

Q: Construct npda that accept the following regular language: L1 = L (aaa∗bab).

A: Non-deterministic pushdown automaton (NPDA):Given: aaa*bab To construct NPDA, when “a” comes then pu...