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;
}
?
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?
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?
int *doubleArray (int arg[], int size) {
int rsize= size*2;
int* array = newint[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
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
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 = newint[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.
// 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