Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* INSTRUCTIONS: Write two functions in the space * indicated below. * * printArr

ID: 3860756 • Letter: #

Question

* INSTRUCTIONS: Write two functions in the space

* indicated below.

*

* printArray function: This should take an array of ints

* and a count. It should then print out the array of numbers

* one number per line.

*

* insert function: Write a function in the space

* indicated below that will take two arrays and an

* index. The function will insert the second array

* into the first array AFTER the given index. Note that the

* second array should not be changed; just the

* first array. Assume the size of the first array

* is large enough to hold all the data in both arrays.

*

*

* Do not use global variables/constants

* or goto's in your code. Please only make one

* function (there's really no need for more than one).

*

* Don't ask the user for information. The main

* function will get all information needed and pass it

* into your function (see main function for

* how your function will be called).

*

*

* two example runs are below:

BEFORE

Array 1

-----------

8

12

20

6

7

9

----------------

Array 2

-----------

14

15

8

----------------

index to insert at: 2

AFTER

Array 1

-----------

8

12

20

14

15

8

6

7

9

----------------

Array 2

-----------

14

15

8

----------------

BEFORE

Array 1

-----------

8

12

20

6

7

9

----------------

Array 2

-----------

14

15

8

----------------

AFTER

Array 1

-----------

8

12

20

6

7

9

14

15

8

----------------

Array 2

-----------

14

15

8

----------------

BEFORE

Array 1

-----------

7

8

9

----------------

Array 2

-----------

20

30

40

50

60

----------------

index to insert at: 0

AFTER

Array 1

-----------

7

20

30

40

50

60

8

9

----------------

Array 2

-----------

20

30

40

50

60

----------------

*

*/

#include <iostream>

using namespace std;

// PLEASE PUT YOUR FUNCTION BELOW THIS LINE

// END OF FUNCTION

int main()

{

int index;

// first array

int array1[200] = {7, 8, 9};

// second array

int array2[200] = {20, 30, 40, 50, 60};

// size of first array

int count1 = 3;

// size of second array

int count2 = 5;

cout << "BEFORE" << endl;

cout << "Array 1" << endl;

printArray(array1, count1);

cout << "Array 2" << endl;   

printArray(array2, count2);

cout << "index to insert at: ";

cin >> index;

insert(index, array1, array2, count1, count2);

cout << "AFTER" << endl;

cout << "Array 1" << endl;

printArray(array1, count1);

cout << "Array 2" << endl;   

printArray(array2, count2);

return 0;

}

Explanation / Answer

#include<iostream>

using namespace std;

//fnction declaration

void printArray(int arr[], int size);

void insert(int index, int arr1[], int arr2[], int size1, int size2);

int main()

{

int index;

// first array

int array1[200] = { 7, 8, 9 };

// second array

int array2[200] = { 20, 30, 40, 50, 60 };

// size of first array

int count1 = 3;

// size of second array

int count2 = 5;

cout << "BEFORE" << endl;

cout << "Array 1" << endl;

printArray(array1, count1);

cout << "Array 2" << endl;

printArray(array2, count2);

cout << "index to insert at: ";

cin >> index;

insert(index, array1, array2, count1, count2);

cout << "AFTER" << endl;

cout << "Array 1" << endl;

//CheggEA changed arra1 size to count1+count2

printArray(array1, count1 + count2);

cout << "Array 2" << endl;

printArray(array2, count2);

return 0;

}

void printArray(int arr[], int size)

{

for (int i = 0; i < size; i++)

{

cout << arr[i] << endl;

}

}

void insert(int index, int arr1[], int arr2[], int size1, int size2)

{

//to coppy the arr1 elements after given index to copy later

int *tmp;

int tmpSz = size1 - (index + 1);

tmp = new int[tmpSz];

for (int i = index + 1, j = 0; i < size1; i++)

{

//cout << j<<" "<<arr1[i] << endl;

tmp[j++] = arr1[i];

}

//now copy from array2 to array1

for (int i = index + 1, j = 0; j < size2; i++)

{

//cout << i<<" "<<arr2[j] << endl;

arr1[i] = arr2[j++];

}

//now copy previous items copied into tmp to arr1

for (int i = 0, j = index+size2+1; i < tmpSz; i++)

{

//cout << i<<" "<<tmp[i] << endl;

arr1[j++] = tmp[i];

}

}

---------------------------------------------------------------------------------------------------------------------------

/*
outpu1:
BEFORE
Array 1
7
8
9
Array 2
20
30
40
50
60
index to insert at: 0
AFTER
Array 1
7
20
30
40
50
60
8
9
Array 2
20
30
40
50
60
output2:
BEFORE
Array 1
7
8
9
Array 2
20
30
40
50
60
index to insert at: 2
AFTER
Array 1
7
8
9
20
30
40
50
60
Array 2
20
30
40
50
60


*/