Chapter 13: Problem 14
a. Write a statement that declares table to be a pointer to a pointer of type double. b. Write \(\mathrm{C}++\) statements that create table to be a two-dimensional array of five rows and seven columns.
Short Answer
Expert verified
a. `double **table;` b. First allocate: `table = new double*[5];`, then `for` loop: `table[i] = new double[7];`.
Step by step solution
01
Declare Pointer to Pointer
In C++, a pointer to a pointer is declared by using a double asterisk. For a pointer to a pointer of type double, the declaration will look like this:
```cpp
double **table;
```This statement means that `table` is a pointer that points to another pointer, which in turn points to a `double` value.
02
Allocate Memory for Rows
Next, to create a two-dimensional array, first allocate memory for the row pointers. Since the array has five rows, you allocate space for five pointers, each pointing to a row. Use the `new` keyword for allocation:
```cpp
table = new double*[5];
```This creates an array of 5 pointers to `double`, one for each row in the two-dimensional array.
03
Allocate Memory for Columns
Now allocate space for the columns for each row. Since each row contains seven `double` elements, iterate over each row and assign memory:
```cpp
for (int i = 0; i < 5; ++i) {
table[i] = new double[7];
}
```This loop assigns an array of 7 `double` elements to each of the 5 row pointers, completing the allocation of the 2D array.
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Pointer to pointer
Pointers in C++ are crucial for dynamic memory allocation as they allow you to directly access memory locations. A particularly interesting type of pointer is the "pointer to pointer." This is essentially a pointer that holds the address of another pointer. This capability can be quite advantageous when managing multi-level data structures, such as dynamically allocated 2D arrays or any structure involving multiple layers of indirection.
In C++, a pointer to a pointer of type `double` can be declared using a double asterisk (`**`), like so:
In C++, a pointer to a pointer of type `double` can be declared using a double asterisk (`**`), like so:
- `double **table;`
Dynamic memory allocation
Dynamic memory allocation in C++ allows for the flexibility of allocating memory during runtime. This is particularly useful when the size of the data structures can't be determined at compile time or when re-sizing arrays as needed.
When allocating memory dynamically, C++ provides the `new` keyword, which assigns memory from the heap. It's not only crucial to allocate memory but also to ensure that each allocated memory segment is deallocated using `delete` to prevent memory leaks. For example, to allocate memory for a 5-row array of pointers we use:
When allocating memory dynamically, C++ provides the `new` keyword, which assigns memory from the heap. It's not only crucial to allocate memory but also to ensure that each allocated memory segment is deallocated using `delete` to prevent memory leaks. For example, to allocate memory for a 5-row array of pointers we use:
- `table = new double*[5];`
- `for (int i = 0; i < 5; ++i) { table[i] = new double[7]; }`
Two-dimensional arrays
Two-dimensional arrays in C++ are essentially arrays of arrays. Each element in the outer array points to an inner array. When dynamically allocating a 2D array using pointers, you build it layer-by-layer starting with pointers to rows.
The capacity to allocate memory dynamically allows C++ programmers to manage the precise size and storage of their data structures. For example, after declaring a pointer to a pointer (`double **table;`), you allocate space for the row pointers with:
The capacity to allocate memory dynamically allows C++ programmers to manage the precise size and storage of their data structures. For example, after declaring a pointer to a pointer (`double **table;`), you allocate space for the row pointers with:
- `table = new double*[5];`
- `for (int i = 0; i < 5; ++i) { table[i] = new double[7]; }`
Data types in C++
Data types are fundamental to any programming language as they define the nature of data a variable can store. In C++, data types such as `int`, `char`, `float`, and `double` form the building blocks of data manipulation and storage.
Among these, `double` is often used for high precision floating-point numbers, which makes it very suitable for mathematical computations requiring a significant degree of accuracy. When working with pointers, and dynamically allocated arrays where precision is key, `double` is a frequently chosen type.
Choosing the appropriate data type in C++ is crucial. It affects not only the precision of the computations but also the memory efficiency of the application. `double` precision floating-point numbers, while offering more precision, consume more memory compared to `float`. Understanding these nuances of data types and their compatibility with pointers is an essential skill when learning C++ programming.
Among these, `double` is often used for high precision floating-point numbers, which makes it very suitable for mathematical computations requiring a significant degree of accuracy. When working with pointers, and dynamically allocated arrays where precision is key, `double` is a frequently chosen type.
Choosing the appropriate data type in C++ is crucial. It affects not only the precision of the computations but also the memory efficiency of the application. `double` precision floating-point numbers, while offering more precision, consume more memory compared to `float`. Understanding these nuances of data types and their compatibility with pointers is an essential skill when learning C++ programming.