Dynamic memory allocation/reallocation of an Integer array. Detailed Description
ID: 3611802 • Letter: D
Question
Dynamic memory allocation/reallocation of an Integer array.
Detailed Description:
Write a program in which you have to:
1.Dynamically allocate an array of integers.
2. Takearray size as input from user and allocate memory according to thissize.
3. Takevalues of array elements as an input from user.
4. Print allarray values.
5. In thisstep; double the previous array size (taken in step 2) andreallocate memory for the array according to new array size. Forexample if user provided array size is 4; now it will become 8,i.e. allocate memory for array having size 8.
6. Now againtake values for second half of the array as an input from the userto fill the increased size of an array.(You have already taken thevalues of first half of the array elements in step 3, now takevalues for remaining elements i.e. Second half).
7. Print allvalues of the array.
8. Also confirm that first half values of arrayare same in both printout statements (step 3 and step 7).
Sample Output
Program to dynamically Allocates an array of integers.
Enter the size of the array: 4
Enter a value: 1
Enter a value: 2
Enter a value: 3
Enter a value: 4
The elements of the array are:
1 2 3 4
Enter the elements in the array after reallocation:
Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter a value: 8
Dynamic memory allocation/reallocation of an Integer array.
Detailed Description:
Write a program in which you have to:
1.Dynamically allocate an array of integers.
2. Takearray size as input from user and allocate memory according to thissize.
3. Takevalues of array elements as an input from user.
4. Print allarray values.
5. In thisstep; double the previous array size (taken in step 2) andreallocate memory for the array according to new array size. Forexample if user provided array size is 4; now it will become 8,i.e. allocate memory for array having size 8.
6. Now againtake values for second half of the array as an input from the userto fill the increased size of an array.(You have already taken thevalues of first half of the array elements in step 3, now takevalues for remaining elements i.e. Second half).
7. Print allvalues of the array.
8. Also confirm that first half values of arrayare same in both printout statements (step 3 and step 7).
Sample Output
Program to dynamically Allocates an array of integers.
Enter the size of the array: 4
Enter a value: 1
Enter a value: 2
Enter a value: 3
Enter a value: 4
The elements of the array are:
1 2 3 4
Enter the elements in the array after reallocation:
Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter a value: 8
Explanation / Answer
Dynamic memory allocation/reallocation of an Integer array. Detailed Description
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.