Question

Homework Question Write a void function called transformArray that takes two parameters - a reference to...

Homework Question

Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was passed in was {4, 2, 5}, then it should be replaced by {4, 2, 5, 8, 4, 10}. The address of the new array should be assigned to the pointer that was passed as a parameter.  The function should prevent any memory leaks. Remember to also prevent memory leaks in the main you use for testing.

For example, it could be used like this:

   int* myArray = new int[3];
   myArray[0] = 4;
   myArray[1] = 2;
   myArray[2] = 5;

   transformArray(myArray, 3);

   for (int i=0; i<6; i++)
      cout << myArray[i] << endl;

   delete []myArray;

The file must be named transformArray.cpp.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

#include <iostream>

using namespace std;

int * transformArray(int* p, int size){

//creating solution array with double size

int* mySol = new int[size*2];

int i=0;

//storing original array values to solution array

while(i<size){

mySol[i] = p[i];

i++;

}

int temp=0;

//the remaining half of solution array will be double of

//values of the array passed

while(i<2*size){

mySol[i] = p[temp]*2;

temp++;

i++;

}

//changing the pointer

p = mySol;

//return the pointer

return p;

//delete solution array

delete []mySol;

}

int main() {

int* myArray = new int[3];

myArray[0] = 4;

myArray[1] = 2;

myArray[2] = 5;

myArray = transformArray(myArray, 3);

for (int i=0; i<6; i++)

cout << myArray[i] << endl;

delete []myArray;

}

Add a comment
Know the answer?
Add Answer to:
Homework Question Write a void function called transformArray that takes two parameters - a reference to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • C++ Code Pass By Reference Practice Write the following functions for basic array operations based on...

    C++ Code Pass By Reference Practice Write the following functions for basic array operations based on the function descriptions given below. Write the following functions for basic array operations based on the function descriptions given below. FindMinArray: This function will search an array of integers (passed to the function as a parameter) for its minimum value, then return that value. SumArray: This function will sum an array of integers (passed to the function through a parameter) then return that sum....

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...

    Question 14; Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks) Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and...

  • #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int...

    #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT