new.
If your program creates an array to store data from a file, and the file (rather than you the programmer) is what determines how many elements ought to be in the array. The size of the array is bound to change from run to run of the program, because the file may change, and he array might be very large.
Q: For a static array A, do you have to delete []
A;, when you don't need it anymore?
No. (Only when A is created by new int [6]).
struct Quad
{
char label;
point vert[4];
};
If A is a static array, the pointer A cannot be changed.
To understand the difference between this version of
Quad and the previous version, consider this picture:
Note:
Quad object; the array is outside of the Quad object,
somewhere else in memory.
vert is embedded in the
Quad object.
The above picture really tells you all you need to know to understand the difference between using static and dynamic arrays ... when you really can use static arrays.
Q: Can you change the contents of a static array?
A: Of course, yes
Q:
Suppose that I have a Quad object S that contains the
label 'Q' and the vertices (0,0) (1,0) (1,1) (0,1). I copy S into another
Quad object R and I change the label and some point
values in R. I then print out
S and then R. What will I get?
A:
It depends whether I'm using the static version of
|
|
| Dynamic Version | Static Version |
|
|
int get();
| ex.cpp | ex2.cpp |
|
|
$ ./ex
* * * * * * * * * *
**********
|
$ ./ex2 * * * * * * * * * * * * * |
$ ./a.out
11265 Jones $225.18
15233 Smith $2593.24
17677 Adams $395.56
18820 Moon $803.9
20011 Zoom $5432.68
20202 Howe $6464.93
21300 Grace $1334.72
22544 Putnam $4124.9
23003 Jones $4129.55
24404 Marion $6600.31
Here's a solution.
Tip: It might be helpful to know that a static array can be initialized with a list of values in { }'s. For example, an array of the first 10 prime numbers can be constructed like this:
int prime[10] = {2,3,5,7,11,13,17,19,23,29};
Note: this problem is purely about static arrays, it doesn't
concern structs at all.
Here's a solution.
Note:
For this task, you need to read each individual character including
whitespaces. fin.get() will do the trick. It will return the
ASCII code of a character you read.
$ ./a.out filename: apple.txt > show $$$$ $$$$$$ $$$$$$ $$$$ $$ $$$$$$$$$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$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