Question

/* 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 = 5;
int* expandArray(int *a, int expFactor);

int main()
{
    int origArr[NUM_ELEM], n;
    for (int i = 0; i < NUM_ELEM; ++i)
        origArr[i] = i;
    cout << "Original array" << endl;
    for (int i = 0; i < NUM_ELEM; ++i)
        cout << origArr[i] << " ";
    cout << endl;

cout << "By how many times should the new array grow" << endl;

cin >> n;
    int *newArr = expandArray(origArr, n);
    for (int i = 0; i < n*NUM_ELEM; ++i)
        cout << newArr[i] << " ";
    cout << endl;
    delete [] newArr;
    return 0;
}

int* expandArray(int *a, int expFactor)
{
        // Your code goes here
}

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

Dear Student,

below i have completed the given C++ program as per the requirement.

Please note that the below program is tested on ubuntu 14.04 sytem and compiled in g++ compiler. This program will also work on other IDE's

================================================================

Program:

================================================================

#include <iostream>

using namespace std;

const int NUM_ELEM = 5;

int* expandArray(int *a, int expFactor);

int main()
{
    int origArr[NUM_ELEM], n;

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

        origArr[i] = i;

    cout << "Original array" << endl;

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

        cout << origArr[i] << " ";

    cout << endl;

    cout << "By how many times should the new array grow" << endl;

     cin >> n;

    int *newArr = expandArray(origArr, n);

    for (int i = 0; i < n*NUM_ELEM; i++)

    cout << *(newArr+i) << " ";

    cout << endl;

    //delete [] newArr;

    return 0;
}

//complete defintion

int* expandArray(int *a, int expFactor)
{
        // Your code goes here

int *copy_array = new int[NUM_ELEM * expFactor];

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

{

*(copy_array+i) = *(a+i);

}

return copy_array;

}


================================================================

Output:

nirmalsharma@ubuntu:-/HomeworkLib solutions/04_02_2018$ g++ resize_array.cpp nirmalsharna@ubuntu:: ~/HomeworkLib-solutions/04-02-20185 ..

================================================================

Kindly Check and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
/* Array expander: Write a function that accepts an int array as argument. The function should...
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
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