0

I'm currently trying to learn C++, and one of the training exercises I'm doing asks that I do the following:

Create a dynamic array, add 100 int values to it.

Write a function that calculates the square of each array element, and save this int value as position 100 + element_index in the array.

At the current moment I have created a dynamic array and filled it with pseudo-random values. What I want to do is to calculate the square of these random values, and "append" them at the end of the array.

The variable firstArray is defined earlier, but is set to be 100.

typedef int* intPtr;
...
...
srand((unsigned)time(0));

intPtr myArray;
myArray = new int[firstArray];
for (int i = 0; i < firstArray; i++)
    ptr[i] = (rand() % 10);

This creates my initial dynamic array, and gives each location in the array a random value between 0 and 10.

If I don't have to use a function, I can easily create a new dynamic array, copy the first 100 values in, and then calculate the squares and place them at the end. I have an attempt at some pseudo-code for the exercise, but I'm unsure as how to properly implement it.

Create dynamic array of size 100, called myArray
Fill each indexed location with a random value between 0 and 10
Pass the dynamic array into a function

Function creates a new dynamic array of size 200
The values on location 0-99 from myArray are copied over
Calculate the square of the value on location n, and write it to location n+100
Return the dynamic array

Delete [] myArray

Create new dynamic array of size 200, called myArray
Copy the values from the array returned by my function into myArray
Delete the array returned from my function

My question relates to passing the information into a function, and returning the new information:

How do I create a function that I can pass a dynamic array into, and have it return another dynamic array?

If it is not possible to have this question answered, I would also very much like feedback on structure, information included in the question and if this is not the right type of question to ask, so I can ask better questions in the future.

5
  • If you are using C++(instead of C) I am sure some of the responses will be that you should use vectors. See cplusplus.com/reference/vector/vector Commented Dec 23, 2014 at 16:40
  • "How do I create a function that I can pass a dynamic array into, and have it return another dynamic array?": int* GetNewArray(int* oldArray,int oldSize,int newSize). Commented Dec 23, 2014 at 16:42
  • Don't bother looking at realloc, especially since you're using new[] and delete[]. Commented Dec 23, 2014 at 16:43
  • typedef int* intPtr; stop this silliness Commented Dec 23, 2014 at 16:46
  • I'd recommend finding a better c++ training. It is not just that the exercise is pointless and is likely to result in implementing a memory leak, but it is also leading you into other very wrong C++ programming habits. One of the strengths of C++ is that it saves you from all unnecessary explicit memory allocations and deallocations. std::vector is certainly more relevant to c++ than explicit new[] and delete[] on 'dynamic arrays` Commented Dec 23, 2014 at 17:01

2 Answers 2

0

Function that takes a dynamic array and returns a dynamic array (of ints) would have this signature:

int* newArray(int* array, int size);

An implementation would then start with:

int* newArray(int* array, int size)
{
    int* ret = new int[size * 2]; // double the size

    // stuff to populate ret

    return ret;
}

int* myBiggerArray = newArray(myArray, firstArray);
// use myBiggerArray
delete [] myBiggerArray;

Also, stay away from typedefing things like int*. int* is already clear and concise enough.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this, will do as a written in another comment and "stop this silliness" with regards to the typedef thing. Your answer worked just fine :)
0

I don't see any requirement that the array needs to be allocated twice. You can allocate all the memory once.

// Allocate all the memory.
intPtr myArray = new int[firstArray*2];

// Fill the first part with random numbers
for (int i = 0; i < firstArray; i++)
    ptr[i] = (rand() % 10);

1 Comment

Thanks for the answer, this is also basicly what I would do, but I'm following along with some friends, so we agreed to stay within the parameters given by the exercise, in order to better compare what we made. Outside of this exercise I would probably just have used a vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.