Expanding an Array Function Using Pointers

Having some issues getting the output I desire here. My assignment is to create a function that accepts an array as an argument and creates an array that is twice the size.

The program accepts the array size as input, creates a random array with this size, and then creates a copy with twice as many elements.

I believe I've figured that part out, but I am then supposed to initialize all the additional new elements in the array copy to zero and now I am stuck.

Can anyone point me in the right direction? Any advice, help, guidance would be appreciated. Thank you!

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
using namespace std;

// Prototypes
void expand(int[], int);
void showArray(int[], int);

int main()
{
const int SIZE = 50;
int values[SIZE];
int n;

cin >> n;
if (n<0 || n>=SIZE)
return 0;

ifstream file;
file.open("data.txt");

showArray(values, n);
expand(values, n);

file.close();

return 0;
}



void expand(int values[], int size)
{

    int *nums = values;

    cout << "\n\nThe elements of array expanded are: \n";
     while (nums < &values[(size*2) - 1])
    {

        nums++;

        cout << *nums << " ";

    }
    cout << *nums << " ";
    cout << endl;
}


void showArray(int values[], int size)
{

    int *nums = values;

    cout << "The elements of the random array are: \n";
    cout << *nums << " ";

    while (nums < &values[size - 1])
    {
        nums++;
        cout << *nums << " ";
    }

}
You're going to need to use some dynamic memory here in order to accomplish what is required. expand is not correct. There are no new arrays created here and no elements are copied.
Last edited on
Topic archived. No new replies allowed.