Dynamic Allocated Arrays

Jul 25, 2015 at 4:03am
I am trying to dynamically create a new array where each item in the original array is duplicated the given number of times.

How do I make a variable that represents the index of the next spot to fill in the new, larger array, and where do I put in function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int expandArray(const int arr[],const int length,const int duplication, int*newArraySize)
{

            int *arr2=new int[length*duplication];
    for(int i=0;i<length;i++)
    {
        for(int j=0;j<duplication;j++)
        {
           arr2[j]= arr[i];
            *arr2 = arr[j];

        }
    }

    *newArraySize=length*duplication;
    return arr2;
}
Jul 25, 2015 at 8:24am
closed account (48T7M4Gy)
Try using 'length'. ie arr2[x] = arr[x + length] for the duplicates.

eg 5,2,8 length = 3 -> 5,2,8,5,2,8 and length is now 6
Jul 25, 2015 at 4:09pm
sorry I forgot to include that if duplication is 2 and length 3, then it would be [1,1,2,2,3,3] and then then length of the array is 6.


I think I need to make another variable for index to fill in spot for the new array but I don't know how to make that flow into a function.
Last edited on Jul 25, 2015 at 4:15pm
Jul 25, 2015 at 5:38pm
You could use pointers, but index math is nice too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

int main() {
  using std::cout;
  using std::setw;

  const char foo[] = "world";
  const size_t length = 5; // number of elements in the array
  const size_t repl = 3;   // how many times each element must be replicated
  for ( size_t elem = 0; elem < length; ++elem ) {
    for ( size_t copy = 0; copy < repl; ++copy ) {
      cout << setw(3) << repl*elem + copy
           << setw(2) << elem
           << setw(2) << copy << setw(2) << foo[elem] << '\n';
    }
  }

  return 0;
}

Edit: Logic error.
Last edited on Jul 27, 2015 at 6:56am
Jul 25, 2015 at 10:55pm
closed account (48T7M4Gy)
arr[2*x] = arr[x] and arr[2*x + 1] = arr[x] for x = 0 ... 2, gives 6 elements in new array in the right order. No special index required.
Jul 26, 2015 at 3:57pm
I created the index variable but I have a problem with the return. It says invalid conversion from int* to int.

I think the error has to do with the line that contains:

arr2[*iptr] = arr2[j];

or


when I initialized the index

Here is my code:
Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int expandArray(const int arr[],const int length,const int duplication, int*newArraySize)
{
    int *iptr=nullptr;
    iptr=new int;


            int *arr2=new int[length*duplication];
    for(int i=0;i<length;i++)
    {
        for(int j=0;j<duplication;j++)
        {
           arr2[j]= arr[i];
            arr2[*iptr] = arr2[j];

        }
    }

    *newArraySize=length*duplication;
    return arr2;
}
Jul 26, 2015 at 5:54pm
That error occurs in this function too:
1
2
3
4
int foo() {
  int * bar;
  return bar;
}

The 'bar' is a pointer, but the return type of the function (on line 1) is an integer.

In your case you probably do not want to return an integer, but a pointer. Therefore, your function should return a pointer.

The rest of your code is worse than before. You do create a pointer 'iptr'. Then you set it to point to dynamically allocated integer, whose value you do not initialize.

The only thing that you do with the pointer is to dereference it during each iteration to get the same uninitialized value that you use as index to an array. That can be an out-of-range error. Anyway, the entire line 13 should not exists. The magic must exist on line 12.

Finally, you don't deallocate the single int that you allocate on line 4.

You could make the newArraySize a reference argument rather than a pointer.


Try the program that I did post. Look at the values that it does print. Do you spot any pattern?
Jul 26, 2015 at 6:12pm
for your program the first line going downwards has a pattern of +1,+1,+3, the second line has a pattern that repeats the same numbers 3 times and the third line has a pattern that repeats the same letters 3 times.

i thought on line 13 i initialized it to arr[j].
Last edited on Jul 26, 2015 at 6:17pm
Jul 27, 2015 at 7:01am
  0 0 0 w
  1 0 1 w
  2 0 2 w
  3 1 0 o
  4 1 1 o
  5 1 2 o
  6 2 0 r
  7 2 1 r
  8 2 2 r
  9 3 0 l
 10 3 1 l
 11 3 2 l
 12 4 0 d
 13 4 1 d
 14 4 2 d

Four "columns".

The second column shows index to the original array and the fourth column shows the value (a character) at the corresponding position. Each unique value repeats three times (on three rows) because the program is set to replicate everything 3 times (you call it "duplication".

There are three copies of each original value and the third column shows their count; {0,1,2}.

The real deal is the first column. (I had a logical error there, which is now fixed.) It shows continuous range of values from 0 to 14. 15 unique values and length*repl == 15. Those can be used as indices on the larger array.
Jul 28, 2015 at 12:55am
ok check out what I did I finally got my code to do exactly what I want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int *expandArray(const int arr[],const int length,const int duplication, int*newArraySize)
{
    int p=0;



    int *arr2=new int[length*duplication];
    for(int i=0; i<length; i++)
    {
        for(int j=0; j<duplication; j++)
        {
            arr2[p++]= arr[i];
            //arr2[jptr] = arr2[j];

        }
    }

    *newArraySize=length*duplication;
    return arr2;
}


I had an issue with the return statement(line 1), I was supposed to initialize it as a pointer to integer, when originally I had it as a integer.

( I am refferring back to the code I put up before)
And I took your advice, removed line 13 and for line 12 [j] got rid of the variables that I assigned to it. So what I did was I deleted line 3 and 4, and created a normal integer variable and initialized it to 0 to hold the variables for my new array.
Jul 28, 2015 at 6:15am
Good. Here is both your method and two others:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int * expandArray( const int arr[], const int length,
                   const int duplication, int& newArraySize )
{
    newArraySize = length*duplication;
    int * arr2 = new int[ newArraySize ];
    int p = 0;
    int * pos = arr2;
    for(int i=0; i<length; i++)
    {
        const int rowstart = duplication * i;
        for(int j=0; j<duplication; j++)
        {
            arr2[p] = arr[i];
            std::cout << arr2[p] << " = "
                      << *pos << " = "
                      << arr2[ rowstart + j ] << '\n';
            ++pos;
            ++p;
        }
    }
    return arr2;
}
Topic archived. No new replies allowed.