You will write a function, copy_array , that takes a pointer to an array and an
ID: 3793641 • Letter: Y
Question
You will write a function, copy_array, that takes a pointer to an array and an integer length and returns a pointer to a copy of that array. The first unit test should be used to get the function working, then use the output comparison test to do the specified steps in main. Obviously you could easily "fake" the output comparison, but there wouldn't be much point in that.
Since you're going to need to dynamically allocate memory, be aware that memory-related errors will probably cause you to not see feedback from the unit test.
Note that passing in a pointer to an array and passing in an array are the same thing. For example, these two headings are really the same. In either case, you have a parameter called a that holds a memory address and can be used like an array or like a pointer (because arrays are pointers). Only use what is given.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// Finish the copy_array function
double * copy_array(double * arr, int len)
{
// Stub so that it compiles
return arr;
}
// use main for the output comparison test, does not impact the unit test
int main()
{
// An array to work with
double source[5] = { 6.0, 7.0, 8.0 };
int len = 3;
// A pointer to work with
double * copy = NULL;
// Set copy to point at source
// hint: remember that an array is a pointer (it holds an address value)
// Using the copy pointer, change the first element in the array to 9.0
// hint: remember that a pointer can be used just like an array!
// Print the elements of source and copy as comma-and-space separated lists
// Don't worry about getting rid of the trailing comma and space, they're fine
// hint: they will be the same
cout << "The arrays: " << endl;
// Use copy_array to set copy to point at a copy of source
// Change the first element of source to 10.0
// Print the elements of source and copy again as comma-and-space separated lists
// Don't worry about getting rid of the trailing comma and space, they're fine
cout << "The arrays: " << endl;
return 0;
}
Explanation / Answer
Hi there here is the code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double * copy_array(double * arr, int len) { return arr; }
/**
* This function prints the array to the screen.
*/
void print_array(double * arr, int len) {
for (int i=0; i<len; i++) {
cout << arr[i];
cout << ", ";
}
cout << "" << endl;
}
int main() {
// An array to work with
double source[5] = { 6.0, 7.0, 8.0 };
int len = 3;
// A pointer to work with
double * copy = NULL;
// Set copy to point at source
// hint: remember that an array is a pointer (it holds an address value)
copy = source;
// Using the copy pointer, change the first element in the array to 9.0
// hint: remember that a pointer can be used just like an array!
copy[0] = 9.0;
// Print the elements of source and copy as comma-and-space separated lists
// Don't worry about getting rid of the trailing comma and space, they're fine
// hint: they will be the same
cout << "The arrays: " << endl;
print_array(source, 3);
print_array(copy, 3);
// Use copy_array to set copy to point at a copy of source
copy = copy_array(source, 3);
// Change the first element of source to 10.0
source[0] = 10.0;
// Print the elements of source and copy again as comma-and-space separated lists
// Don't worry about getting rid of the trailing comma and space, they're fine
cout << "The arrays: " << endl;
print_array(source, 3);
print_array(copy, 3);
return 0;
}
Please comment if you have any doubts.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.