1) a) Give C++ code for dynamically declaring and allcoating a 5 by 7 array of i
ID: 3596143 • Letter: 1
Question
1) a) Give C++ code for dynamically declaring and allcoating a 5 by 7 array of intergers using the new operator b) Give the C++ code for deallocating the array allocated in part a 1) a) Give C++ code for dynamically declaring and allcoating a 5 by 7 array of intergers using the new operator b) Give the C++ code for deallocating the array allocated in part a a) Give C++ code for dynamically declaring and allcoating a 5 by 7 array of intergers using the new operator b) Give the C++ code for deallocating the array allocated in part a b) Give the C++ code for deallocating the array allocated in part a b) Give the C++ code for deallocating the array allocated in part aExplanation / Answer
1a) For declaring and allocating a 2D array of integers, a double pointer of integer needs to created and using new allocated with a pointer array whose size is number of rows. Then using another new, a pointer of size as number of columns need to be allocated for each of the row pointers
int **array = new int*[5];
for(int i = 0; i < 5; i++) {
array[i] = new int[7];
}
b) For deallocating a 2D dynamically allocated array, deallocate each row using delete keyword. After that deallocate the array
for(int i = 0; i < 5; i++) {
delete [] array[i];
}
delete [] array;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.