What are various ways where you would use a dynamic array vs an automatic array?
|
Here would be an example from my line of work, which is forestry. One of my programs produces output such as this, which is referred to as a Stand Table...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
Block #2 Board Foot Volume Per Acre For All Trees
Dbh RM RO SO WO CO BB YP Asp Totals
============================================================================================
12 58 0 73 0 290 101 0 122 645
13 0 44 0 83 417 242 0 71 857
14 78 54 154 79 242 50 0 0 657
15 0 60 74 0 62 77 0 0 272
16 0 83 0 0 181 0 0 0 264
17 0 0 79 0 0 53 0 0 132
18 0 0 0 0 359 0 199 0 557
19 0 0 0 0 62 0 0 0 62
20 0 0 0 0 210 0 0 0 210
21 0 0 0 0 0 0 0 0 0
22 0 94 0 0 0 0 0 0 94
============================================================================================
136 335 380 161 1823 524 199 194 3751
Block #2 Board Voot Volume For Live Trees
Dbh RM RO SO WO CO BB YP Asp Totals
============================================================================================
12 58 0 0 0 235 101 0 122 517
13 0 44 0 83 417 242 0 71 857
14 78 54 74 79 242 50 0 0 577
15 0 60 74 0 62 77 0 0 272
16 0 83 0 0 181 0 0 0 264
17 0 0 0 0 0 53 0 0 53
18 0 0 0 0 359 0 199 0 557
19 0 0 0 0 62 0 0 0 62
20 0 0 0 0 210 0 0 0 210
21 0 0 0 0 0 0 0 0 0
22 0 94 0 0 0 0 0 0 94
============================================================================================
136 335 148 161 1768 524 199 194 3463
Block #2 Board Voot Volume For Dead Trees
Dbh RM RO SO WO CO BB YP Asp Totals
============================================================================================
12 0 0 73 0 55 0 0 0 128
13 0 0 0 0 0 0 0 0 0
14 0 0 80 0 0 0 0 0 80
15 0 0 0 0 0 0 0 0 0
16 0 0 0 0 0 0 0 0 0
17 0 0 79 0 0 0 0 0 79
18 0 0 0 0 0 0 0 0 0
19 0 0 0 0 0 0 0 0 0
20 0 0 0 0 0 0 0 0 0
21 0 0 0 0 0 0 0 0 0
22 0 0 0 0 0 0 0 0 0
============================================================================================
0 0 232 0 55 0 0 0 287
|
The numbers in the first column are tree diameters (Diameter At Breast Height) and the letters across the top are species abbreviations. RM stands for Red maple, RO for Red oak, SO for Scarlet oak, WO for White oak, BB for Black birch, YP for Yellow poplar, and Asp for aspen.
Now, our (where I work) timber sales oftentimes contain thousands of marked trees. They are stored in a database. My programs must read and process that data and generate tables such as above. But the thing is, we have many timber sales, and each sale has different species and tree diameters. There are eight species in the batch of tables seen above, but our sales typically can have anywhere from about five species per timber sale to about 20 species. Also, the range of diameters represented by the above data is 12 tp 22 inches. Sometimes we have ranges of 10 to 50 inches.
You have to realize that the above tables represent data summaries. Each tree has to be read out of the database or file and have its specific board foot volume assigned to one of the cells in the above table. If you look at the top table you'll see a 58 in the cell for 12 inch Red maple. A 12 inch tree has about 50 to 60 board feet, so that number would represent only one tree. But in the middle or CO (Chestnut oak) column for 12 inch trees you see the number 290. That likely represents five or six trees. Where this fits in to your question about dynamic arrays is that to perform processing as such as I've described above to produce tables such as above, memory has to be dynamically allocated in exactly the right amount to contain the processed data. Before processing a timber sale one must run code to determine what the ranges of diameters are, and to determine how many distinct species codes are in a given sale. Once that number is determined, one can allocate the arrays of just the right size.
Note above that a tabular two dimensional table or matrix such as represented by those tables requires two dimensional arrays, which can be represented in C or C++ like so...
As an aside, in my actual code which created those tables above its considerably more complicated than that. Note this heading...
|
Block #2 Board Foot Volume Per Acre For All Trees
|
All our timber sales are compartmentalized into Cutting Blocks. A timber sale can have from 1 to maybe 30 blocks. So that introduces a third dimension. So if that sale above had 10 cutting blocks the array might be like so...
And that isn't the end either. Note above there is mention of 'Live Trees' and 'Dead Trees' (there are actually a couple more possible designations). That introduces a 4th dimension...
|
int iVolume[12][9][11][3];
|
As another aside, I can tell from your code you posted you are using GCC or at least a non Microsoft compiler, right? Your code won't compile with any Microsoft compiler I have. For this code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// cl MistahMoose.cpp /O1 /Os /EHsc
//MistahMoose.cpp
#include <iostream>
using namespace std;
int main()
{
int SIZE;
cin >> SIZE;//get array size
int arr[SIZE];
for(int i = 0; i <SIZE; i++)//fill array
{
arr[i] = 1;
}
for(int i = 0; i <SIZE; i++)//print array
{
cout << arr[i];
}
return 0;
}
|
...I get these errors...