Hello all, this is a tough request but I am stuck on a question I found online
and I really can't think of a way to do it.
here is the question
Write a program that duplicates the elements of an array. The program should include the following:
A function called duplicate that takes in an array of integers as a parameter and its size. The function should dynamically allocate a new array that will contain duplicates of every element in the argument array.
#include <iostream>
usingnamespace std;
void duplicate(int arr[], int *ptr);
int main() {
int arr[5];
cout << "Fill the array: ";
for (int i = 0; i < 5; ++i) {
cin >> arr[5];
}
}
void duplicate(int arr[], int *ptr) {
int dynamic[10];
ptr = dynamic;
for (int i=0; i<10; ++i)
for (int j = 0; j < 10; ++j) {
if (arr[i] == arr[j]) {
}
}
}
//really stuck here can't think of a way
1. In the main function, declare an array of integers of size 5, ask the user to fill the array
Code this, compile it and test it. Then do the next step
The function should dynamically allocate a new array that will contain duplicates of every element in the argument array.
The function should return the dynamic array to the caller through a return statement.
1 2 3 4 5 6 7 8 9
int* duplicate(int arr[], int size)
{
int* duplicates = newint[size * 2];
// now you loop through arr and write each value twice to duplicates
return duplicates;
}