doubleArray question

Pages: 12
Write a function called doubleArray that takes as parameters an array of Points and its size and returns a pointer to another Points array that is double the size and contains points equal to the parameter array and default points in the end. e.g. called with an array containing Points (1, 1), (2, 2) and (5, 5) and size 3 and returns an array containing Points (1, 1), (2, 2), (5, 5), (0, 0), (0, 0), (0, 0).

Write client code (in main) that calls the function above to double an array containing the points (1,1), (2, 2) and (5, 5). Iterate over the array returned array and print the x, y coordinates for each element.
Explicitly free any memory that needs to be freed when you are done.



I am having trouble with this question.

So how do I add the default (0,0)s at the end of the array after size gets doubled and return it?

Also to print the array back in the main, I was thinking about printing x values with array[0,2,4..] and array[1,3,5...] for y values. Is this a good way to do it?

So this is what I understand so far->
1
2
3
4
5
6
7
8
9
10
11
12
  double doubleArray (int arg[], int size) {
  int arrsize= size*2;

}

int main ()
{
  int arr[] = {1, 1, 2 ,2 ,5 ,5};
  doubleArray(arr,6);

  return 0;
}
Last edited on
doubleArray needs to return a pointer. int * doubleArray (int arg[], int size)

int arrsize= size*2; This won't help you.

You need to dynamically allocate a new array.
Last edited on
So I need to do this
1
2
int *array;
array=new int[size]
?
How am I going to know the size of the array that I am creating?
int arrsize= size*2; I did this so I can know the size of the new array I am going to make? Or is there way to change the size after initializing?

Btw I can't use vectors.
Last edited on
You can do int* array = new int[size*2];. Though now that I think about it, it would be useful to have that value stored in a variable (or a const).
So after I do that, how do I get the points from the passed Array to the new array?
Adding the default zeros can be done by a for loop I guess?
Is this a good way to copy from one to another?
1
2
3
 for(int i=0;i>size;i++){
		  array[i]= arg[i];
	}


And then I add the default zeros with another for loop?
Last edited on
Yes.

btw you put > instead of <.
Last edited on
Yeah thank you.


So am I doing this correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
int doubleArray (int arg[], int size) {
  
  int rsize= size*2;
  int* array = new int[rsize];

	  for(int i=0; i<size; i++){
		  array[i]= arg[i];
		}
	  for(int i=size; i<rsize; i++){
		  array[i]=0;
		}
	  return *array;
}
The return type of doubleArray has to be int*
Like this or still not quite right?

1
2
3
4
5
6
7
8
9
10
11
12
13
int *doubleArray (int arg[], int size) {
  
  int rsize= size*2;
  int* array = new int[rsize];

	  for(int i=0; i<size; i++){
		  array[i]= arg[i];
		}
	  for(int i=size; i<rsize; i++){
		  array[i]=0;
		}
	  return array;
}
That looks good. Though it is almost 1am so I may be overlooking something.
I know it is late and thank you so much for help:D


This is what it looks like right now, I still can't get the right print output but I think I can figure that out.
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

int *doubleArray (int arg[], int size) {
  
  int rsize= size*2;
  int* array = new int[rsize];//New array

	  for(int i=0; i<size; i++){
		  array[i]= arg[i];
		}//Copy the passed array to new array

	  for(int i=size; i<rsize; i++){
		  array[i]=0;
		}//Add default zeros

	  return array;//return the new array with 
					//double the size and defualy zeros

}//EndofdoubleArray

int main (){
  int arr[] = {1, 1, 2 ,2 ,5 ,5};
  cout<<doubleArray(arr,6); //print the doubleArray
  system ("PAUSE");
  return 0;
}//End
Unless you have something else I don't know about, you can't print arrays like that. You have to print each item one by one in a loop.

btw, you can store the array like this:
int * newArr = doubleArray(arr,6);
Last edited on
LOL I do not think I know something, that you do not know.
It works!

1
2
3
4
5
6
7
8
9
10
11

int main (){
  int arr[] = {1, 1, 2 ,2 ,5 ,5};
  int *newArr = doubleArray(arr,6);
  for(int i=0; i<6; i++)
  cout<<newArr[i]<<endl; //print the doubleArray
  
  system ("PAUSE");
  return 0;
}//Endofmain

Some notes:
1. You do now allocate memory dynamically. What is your deallocation plan?

2. Does your compiler support C++11? It would be good if it did, because then you could learn the current language.

3.
Write a function called doubleArray that takes as parameters an array of Points and its size and returns a pointer to another Points array

It does not look like your current program does what it says on the tin.
1.use delete[] newArr right before the main function ends?

2.My compiler supports it, but my teacher doesn't.

3.How?
doubleArray that takes as parameters an array of Points and its size
<I do take array of points and its size as parameters?
and returns a pointer to another Points array
<And I do return this?
Last edited on
1. Yes.

2. It is perfectly fine to learn the traffic rules by driving a T-Model Ford; the rules are about the same for all vehicles. The question is whether it would be better to use a car model that you are more likely to actually drive?

3. Your arrays have now ints. Have you had classes/structs recently? The Point sounds like one.
2.I agree with you on the C++11 but when asked my teacher, he said it is new and we don't need it right now. It will be much more easier for me to use C++11 since it has some cool features. For examples copying arrays would be much more easier with C++11 I guess.

3.I never thought of the Points as a class...
This is the rest of the questions and I guess you are right since he is using "Point objects".
Well back to the drawing board then.

3- Same question as 1 but now the function returns a pointer to an array that holds pointers to Point objects. The input parameters are the same as 1, just the return type is different. The returned array will contain pointers to Points objects (1, 1), (2, 2), (5, 5), (0, 0), (0, 0), (0, 0). These point objects should have nothing to do with the point objects in the parameter array. Hint: think carefully about where/how the memory is allocated.
4- Same question as 2, but now the code calls the function in 3. Explicitly free any memory that needs to be freed when you are done.
For examples copying arrays would be much more easier with C++11 I guess.

No. Standard library has had a copy algorithm already before C++11. You are learning the principle of copying, so those algorithms are ruled out.

Default initialization with brace init syntax is rather convenient (altough the explicit loop in this exercise serves two purposes):
Point * array = new int[rsize] {};

Well back to the drawing board then.

Not quite. Your code takes array of T and creates a copy. Your T is int. You are almost ready for Q1 if T is Point. (You just have to define a Point.)


The Q3 requires array of pointers that all point to something. Lovely.
The Q3 requires array of pointers that all point to something.


No, it's just an array of Point objects. Point could just be a struct.


Point * array = new int[rsize] {}; This is C++ 11, so it doesn't sound like he'd be able to use it.
True. It would be semi-ok for the first function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// C++03
T * array = new T [rsize];
for ( int i=0; i<size; ++i ){
  array[i] = arg[i];
}
for ( int i=size; i<rsize; ++i ){
  array[i] = 0; // or logical equivalent
}

// C++11
T * array = new T [rsize] {};
for ( int i=0; i<size; ++i ){
  array[i] = arg[i];
}

Alas, the second function requires a little bit extra and the C++03 version is a much better starting point for it. As nice as the brace init feature is, it is not for today.

Yay295 wrote:
No, it's just an array of Point objects. Point could just be a struct.
FlyingTr wrote:
3- Same question as 1 but now the function returns a pointer to an array that holds pointers to Point objects. The input parameters are the same as 1, just the return type is different. The returned array will contain pointers to Points objects
Pages: 12