Question

In: Computer Science

In c++ Array expander Write a function that accepts an int array and the arrays size...

In c++

Array expander

Write a function that accepts an int array and the arrays size as arguments.

The function should create a new array that is twice the size of the argument array.

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.

Additional Requirements:

  • Demonstrate your function in a program by initializing an array in main and passing it and it's size to your function. Use the pointer returned by your function to print the contents of the array in main. Don't forget to delete the array at the end of main using the pointer.
  • Your function should not interact with the user in any way (no cin or cout statements).
  • Do not use global variables.
  • Your function must use dynamic memory allocation to create the array. If it does not, you will receive 0 points for the assignment.
  • Do not use smart pointers.

A 20-Point Sample Run:

8 6 7 5 3 0 0 0 0 0

In the sample run, the original array was initialized with 8 6 7 5 3.

Solutions

Expert Solution

#include <iostream>

using namespace std;

int* doubleArray(int* A, int n){
    // creating the array dynamically
    // double the size of original array
    int* B = new int[2 * n];

    // initializing the first n elements with
    // the corresponding elements of original array
    for(int i = 0; i < n; i++)
        B[i] = A[i];

    // initializing the remaining elements with 0
    for(int i = n; i < 2 * n; i++)
        B[i] = 0;
    return B;
}
int main(){
    int n = 5;
    int A[n] = {8, 6, 7, 5, 3};

    // calling the doubleArray method
    int* B = doubleArray(A, n);

    for(int i = 0; i < 2 * n; i++)
        cout << B[i] << " ";

    // deleting the array
    delete B;
    return 0;
}

OUTPUT:-

if you have any doubt, feel free to ask in the comments.


Related Solutions

In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 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 a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
Write a function that accepts an int array and the array's size as arguments.
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 int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter 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 element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
Element Shifter: Write a function that accepts an int array and the array’s size as arguments....
Element Shifter: 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 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...
C++ Write a function that accepts an array of doubles and the array's size as arguments....
C++ Write a function that accepts an array of doubles and the array's size as arguments. The function should display the contents of the array to the screen.
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
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.
C++ ProgramWrite 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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT