Create a dynamic array with duplicates [stuck on a problem]

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace 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

Last edited on
Maybe divide the problem into sub-problems;

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

2. ask the user to fill the array
I just don't know how to make the duplicate part

I tried my best
Last edited on
Edit:

Ah, the original post was edited while I was answering.
Last edited on
cin >> arr[5];I think you mean cin >> arr[i];

You need to read the instructions carefully.
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 = new int[size * 2];

   // now you loop through arr and write each value twice to duplicates

  return duplicates;

}

Look at the example:
index 0 1 2 3 4 5 6 7
input 3 4 2 7
dupli 3 3 4 4 2 2 7 7

Doesn't that show that value of input[2] should be copied to dupli[4] and dupli[5]?
Yes Sir it does, but I am afraid I cannot think of that logic yet.
In the input array, you will iterate over five element.

array[0], array[1], array[2], array[3], array[4]

You need to copy these values to the output array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
output[0] =  array[0];
output[1] =  array[0];

output[2] =  array[1];
output[3] =  array[1];

output[4] =  array[2];
output[5] =  array[2];

output[6] =  array[3];
output[7] =  array[3];

output[8] =  array[4];
output[9] =  array[4];


You need to write a loop such that each element in the input array is copied TWICE into the output array.
Lets take half of that task:
1
2
3
4
5
output[0] =  array[0];
output[2] =  array[1];
output[4] =  array[2];
output[6] =  array[3];
output[8] =  array[4];

and look just the indices:
Y X
===
0 0
2 1
4 2
6 3
8 4

Is there such expression f, where f(X) == Y for each row in that table?

Then other table:
Y Z
===
0 1
2 3
4 5
6 7
8 9

Is there such expression g, where g(Y) == Z for each row in that table?
Last edited on
Topic archived. No new replies allowed.