Problem Statement: Dynamic memory allocation/reallocation of an Integer array. D
ID: 3611650 • Letter: P
Question
Problem Statement:
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. Take array size as input from user and allocate memory according tothis size.
3. Take values of array elements as an input from user.
4. Print all array values.
5. Inthis step; 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. Nowagain take values for second half of the array as an input from theuser to fill the increased size of an array.(You have already takenthe values of first half of the array elements in step 3, now takevalues for remaining elements i.e. Second half).
7. Print all values of the array.
8. Also confirm that first half values ofarray are 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
Problem Statement:
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. Take array size as input from user and allocate memory according tothis size.
3. Take values of array elements as an input from user.
4. Print all array values.
5. Inthis step; 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. Nowagain take values for second half of the array as an input from theuser to fill the increased size of an array.(You have already takenthe values of first half of the array elements in step 3, now takevalues for remaining elements i.e. Second half).
7. Print all values of the array.
8. Also confirm that first half values ofarray are 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
Hi 007,Read input from the user using scanf and dynamically allocatememory using malloc.
You can solve the rest of the questions by using simpleloops.
I don't know how to handle question 8 but you might try justlooping through the values or comparing the two arrays directlyusing memcmp.
If you're still stuck try something along the lines ofthis...
#include <stdio.h>#include <stdlib.h>#include <string.h>int promptForInt(char *promptString) { int inValue; printf("%s: ", promptString); scanf("%d",&inValue); return inValue;}/* Print elements of int array * * size is number of elements in array * arrayPtr is a pointer to the array of integers */void printArray(int size, int *arrayPtr) { int i; printf("The elements in the array are: "); for(i = 0; i < size; i++) { printf("%d ", arrayPtr[i]); } printf(" ");}/* Allocate an chunk of memory for an array of integers (Question 1) * * size is the number of integer elements that the array will hold */int *allocateArray(int size) { int *buf; if(NULL == (buf = malloc(size * sizeof(int)))) { printf("Not able to allocate memory for array "); exit(1); } return buf;}int main(int argc, char **argv) { int i; printf("Program to dynamically Allocates an array of integers. "); // Take array size as input from user and allocate memory // according to this size (Question 2) int intArraySize = promptForInt("Enter the size of the array"); int *intArrayPtr = allocateArray(intArraySize); // Take values of array elements as an input from user (Question 3) for(i = 0; i < intArraySize; i++) { intArrayPtr[i] = promptForInt("Enter a value"); } // Print all array values (Question 4) printArray(intArraySize, intArrayPtr); // Allocate an array of integers twice the size of the original array (Question 5) int *intArrayDoubleSizePtr = allocateArray(2*intArraySize); // Copy values from small array to first half of double sized array (Question 5) for(i = 0; i < intArraySize; i++) { intArrayDoubleSizePtr[i] = intArrayPtr[i]; } // Take values for second half of the array as an input from the user // to fill the increased size of an array (Question 6) printf("Enter the elements in the array after reallocation: "); for(i = 0; i < intArraySize; i++) { intArrayDoubleSizePtr[intArraySize+i] = promptForInt("Enter a value"); } // Print all array values in the long array printArray(intArraySize*2, intArrayDoubleSizePtr); // Confirm that first half values of array are same in both arrarys (Question 8) // Let's compare the memory regions held by the short and long array directly if( 0 != memcmp ( intArrayPtr, intArrayDoubleSizePtr, intArraySize * sizeof(int) )) { printf("The two arrays contains different data! "); } free(intArrayPtr); free(intArrayDoubleSizePtr); return 0;} Remember adding error checking - notably on the routine thatreads input from the user.
Cheers, niels Hi 007,
Read input from the user using scanf and dynamically allocatememory using malloc.
You can solve the rest of the questions by using simpleloops.
I don't know how to handle question 8 but you might try justlooping through the values or comparing the two arrays directlyusing memcmp.
If you're still stuck try something along the lines ofthis...
Remember adding error checking - notably on the routine thatreads input from the user.
Cheers, niels
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.