Question

In: Computer Science

Explain how a full array in C can be expanded to hold more values. In your...

Explain how a full array in C can be expanded to hold more values. In your answer make sure you mention the function calls required, and anything else that is necessary to perform this task. Give a small example where an array of 100 integers is resized to be able to now store 200 integers.

Solutions

Expert Solution

Array in general is a collection of homogenous elements which are stored at continous location and normally the size of the array is fixed.

But what is there we need to change the size of the array. For example, 3 more students records are added to the array containing their height.

Hence for these purposes dynamic memory allocation is required. Under <stdlib.h> there are few function that help you to perform dynamic memory allocation in C.

1) malloc - this function will let you allocate memory dynamically while creating array. The return type of this function is a void pointer which can be cast to any type.

example -

int * ptr = (int*) malloc ( 10 * sizeof(int));

assuming the size of integer is 4 bytes then this will allocate a block of 40 bytes of memory and will return the pointer to that location.

2) realloc() - size of the dynamically allocated memory can be changed using realloc(). it should only be used for those arrays which are dynamically allocated, Otherwise it will show so weird behavior.

what realloc does is it deallocate the old object pointed by the pointer and makes the pointer pointer to the new object. The values stored in the new object are same as the previous values.

example - in the previous example we had array of size 10, suppose we need to add 5 more values . This is how we are going to solve the problem using realloc.

ptr_new=(int*) realloc(ptr,sizeof(int)*5);

code -

#include<stdio.h>
#include<stdlib.h>


int main()
{
   int * ptr=(int *)malloc(sizeof(int)*100);
   int i;
   for(i=0;i<100;i++)
   {
       ptr[i]=i;
   }
   printf("after malloc \n");

   for(i=0;i<100;i++)
   {
       printf("%d ",ptr[i]);
   }
   int * newptr=(int *)realloc(ptr,sizeof(int)*200);
   printf("\n");
   for(i=100;i<200;i++)
   {
       newptr[i]=i;
   }
   printf("after realloc \n");
   for(i=0;i<200;i++)
   {
       printf("%d ",newptr[i]);
   }

}

output -


Related Solutions

Activity 12: Array What is an Array? An array is a special variable, which can hold...
Activity 12: Array What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: var car1 = "Saab"; var car2 = "Volvo"; var car3 = "BMW"; However, what if you want to loop through the cars and find a specific one? And what if you had not...
C programming What is an array? Explain by taking an example that how can we take...
C programming What is an array? Explain by taking an example that how can we take input and output in 1D array?
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything
Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student...
C++ Task 1 Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Input Validation: Do not accept negative numbers for test scores. Task 2 Modify the program so the lowest...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT