Question

(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. The function should return a pointer to the new array.

Hint: You must create a “new” array in the freestore ( the heap ) for this to work correctly.

Here is the required starting code below.

// Element Shifter

#include

using namespace std;

// Prototype

int *shift(int[], int);

void showArray(int[], int);

int main()

{

const int SIZE = 5;

int values[SIZE] = { 1,2,3,4,5 };

// Display the contents of the array.

cout << "The contents of the original array are:\n";

showArray(values, SIZE);

// Call the shift function to make a copy of

// the array, with the elements shifted one

// position toward the end if the array.

int *arrCopy = shift(values, SIZE); //Gets address of the new array in freestor

   // Display the contents of the original array

//to prove it was not damaged

cout << "The contents of the original array are:\n";

showArray(values, SIZE);

// Display the contents of the new array.

cout << "The contents of the new array are:\n";

showArray(arrCopy, SIZE+1);

return 0;

}

// The shift function accepts an int

// array and an int indicating the array's

// size. The function returns a pointer to

// an array that is one element larger than

// the array that was passed as an argument.

// The elements of the argument array are

// copied to the new array, shifted one

// position toward the end of the array.

int *shift(int arr[], int size)

{

int junk = 0; //Simkin temp junk code so it will compile

return &junk; //Simkin temp junk code so it will compile

}

// The showArray function displays the contents

// of an int array.

void showArray(int arr[], int size)

{

cout << "inside showArray()" << endl; //Simkin temp junk code so it will compile

}

/* proof

The contents of the original array are:

1 2 3 4 5

The contents of the original array are:

1 2 3 4 5

The contents of the new array are:

0 1 2 3 4 5

*/

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

Answer :- The C code can be written as-

#include <iostream>

using namespace std;

int * shift(int arr[], int size); /* prototype of the shift function */

void showArray(int arr[], int size); /* prototype of the showArray function */

int main()

{

    int numbers[5], *new_arr;

    cout << "Enter 5 numbers: ";

    // Storing 5 number entered by user in an array

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

    {

        cin >> numbers[i];

    }

    //showArray(numbers, 5);

    new_arr = shift(numbers, 5);

    cout<<"The new array content is-"<<endl;

    showArray(new_arr, 6);

    return 0;

}

int * shift(int arr1[], int size) /* definition of the shift function */

{

    int *arr2;

    arr2 = (int*) malloc((size+1)*sizeof(int));

    arr2[0] = 0;

    for(int i = 1; i < (size+1); i++){

        arr2[i] = arr1[i-1];

    }

    return arr2;

}

void showArray(int arr[], int size)

{

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

        cout<<" "<<arr[i];

    }

}

OUTPUT :-

Enter 5 numbers: 1
12
13
14
15
The new array content is-
0 1 12 13 14 15

Add a comment
Know the answer?
Add Answer to:
(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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

  • Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write...

    Can someone help me understand this step by step (C++) Array Shifter and Array Reversal Write a function that accepts an array of doubles 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 element 1 of the new array, element 1 of the argument array...

  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments:...

    c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

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