Question

(Pointers and Dynamic memory). Write an algorithm (in a form of pseudocode or actual code) for...

(Pointers and Dynamic memory). Write an algorithm (in a form of pseudocode or actual code) for a program that dynamically resizes an array. Assume original array, array1, is an array of ints having size current_size.

Hint – use the prototype void Resize (int array[],int& current_size); and assume array size is increased by one (i.e. to current_size + 1) when the function Resize is called.

void Resize (int array[], int & current_size){

// TODO: WRITE YOUR CODE HERE

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

Thanks for the question. Below is the function you will be needing.

========================================================

void Resize (int array[], int & current_size){
  
   int * temp = new int[current_size+1]; // create a temp array

// copy the values from array to the temp
   for(int i=0; i<current_size;i++) temp[i] = array[i];
  
   current_size++; // increment the size by 1
   array = temp; // assign the array pointer to the temp
  
}

=======================================================

Add a comment
Know the answer?
Add Answer to:
(Pointers and Dynamic memory). Write an algorithm (in a form of pseudocode or actual code) for...
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
  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • Pseudocode (to implement Algorithm); Loop Invariant 4. (15 pts) There is an algorithm called array doubling...

    Pseudocode (to implement Algorithm); Loop Invariant 4. (15 pts) There is an algorithm called array doubling that is used to increase the size of an array to accommodate new data when an array fills up. In the algorithm, when an array fills up, a new array is created dynamically that is 2x the size of the original, the data is copied to the new array, and the old array is destroyed (a) Write an algorithm to implement an underflow strategy...

  • 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...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • (C++) compute and return the sum of the ints contained in the Array table using RECURSION....

    (C++) compute and return the sum of the ints contained in the Array table using RECURSION. Start with the following function. int sum(node * head[]);    // // //This is the provided header file. //arr.h #include #include #include const int SIZE = 5;   //size of the array of head pointers struct node { int data; node * next; }; /* These functions are already written and can be called to test out your code */ void build(node * head[]); //supplied...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

  • 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...

  • Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO...

    Please answer this question clearly and correctly! the code should be handwritten if all possible!! ALSO MAKE SURE IT IS WRITTEN IN C++! thank you so much in advance and I will upvote when done right! (8 points) Create a class called resizeableArray. Include an int pointer arrayPtr variable that points to a dynamic array, a capacity variable that holds the dynamic array maximum size and a currentSize variable that has the current number of elements assigned in the dynamic...

  • Question 1: Pointers You are given the following C code and memory diagram. The “contents” column...

    Question 1: Pointers You are given the following C code and memory diagram. The “contents” column of the memory diagram shows the value of the variable. Update the “contents” column of the memory diagram for each assignment in main(). long *pt; long data;    long buffer[4]; void main(void){   pt = &buffer[1];   *pt = 1234;   data = *pt; } address      contents   variable 0x20000000 0x00000000    pt 0x20000004 0x00000000    data 0x20000008 0x00000000    buffer[0] 0x2000000C 0x00000000    buffer[1] 0x20000010 0x00000000    buffer[2] 0x20000014 0x00000000    buffer[3]...

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