Topics to Cover
- Static arrays
- cin.get()/fin.get() does not skip any whitespace character
- Practice with structs
Static Arrays
Dynamic arrays vs static arrays
Creating an Array of 6 ints |
| Static Array |
Dynamic Array |
int A[6];
|
int* A = new int[6];
|
| Increase the size of the array to 10 |
Not possible.
|
int* B = new int[10];
for(int i=0; i<6; i++)
B[i] = A[i];
delete [] A;
A = B;
|
|
There are two types of arrays in C++:
-
Dynamic arrays: What we've seen so far. The size of the arrays can
change from run to run of the program.
-
Static arrays: Once created, the size of the array cannot be changed.
- Using the array after its been created is pretty much the same for either.
- The array cannot be too large. For example, you cannot create an array of
10 million integers.
|
Here's a simple toy program that uses a static array.
#include <iostream>
using namespace std;
void foo(int n);
int main()
{
int n;
cin >> n;
foo(n);
return 0;
}
void foo(int n)
{
cout << "Enter " << n << " numbers: ";
int A[n];
for(int i=0; i<n; i++)
cin >> A[i];
cout << "The numbers in reverse: " << endl;
for(int i=n-1; i>=0; i--)
cout << A[i] << " ";
cout << endl;
// control_point_1
}
|
|
~$ ./a.out
4
Enter 4 numbers: 1 3 0 7
The numbers in reverse:
7 0 3 1
Diagram of the program state at control_point_1
|
Q: For a static array A, do you have to delete []
A;, when you don't need it anymore?
No. As shown in the above diagram, the array is inside the stack frame
of function foo(). When foo() returns, the entire array will be discarded
automatically.
Using Static Arrays in struct Definitions
Anything you can do with static arrays you can do with dynamic
arrays, and then some. However, in some instances, using static arrays
is simpler. For example,
Difference between dynamic and static arrays
Note the main difference between dynamic and static arrays:
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:
-
In the dynamic version, the pointer (instead of the array) is embedded
in the
Quad object; the array is outside of the Quad object,
somewhere else in memory.
-
In the static version, the entire array
vert is embedded in the
Quad object.
Compare the static array version of main
with the dynamic array version of main.
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
Quick check
Let's look at one example to see what consequences arise from this picture.
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 then print out
S and then R. What will I get?
A:
It depends whether I'm using the static version of
Quad or the dynamic version. (Drag your mouse for answers)
Static Version |
Q (0,0) (1,0) (1,1) (0,1)
Q (0,0) (1,0) (1,1) (0,1)
P (1,0) (2,0) (2,1) (1,1)
|
Dynamic Version |
Q (0,0) (1,0) (1,1) (0,1)
Q (1,0) (2,0) (2,1) (1,1)
P (1,0) (2,0) (2,1) (1,1)
|
|
// step 1
Quad S;
... // S has 'Q' and (0,0), (1,0), (1,1), and (0,1)
print(S); // It will print Q (0,0) (1,0) (1,1) (0,1)
// step 2
Quad R;
R = S; // Copying will have different meanings!
R.label = 'P';
// step 3
for(int i = 0; i < 4; i++)
R.vert[i].x++;
print(S);
print(R);
|
Why the difference? Look at the picture!
| Dynamic Version
| Static Version
|
|
|
This is not a reason to use static over dynamic, but it is a good
example of how and why they behave differently.
cin.get() and fin.get()
The function as the following prototype:
int get();
- The function reads a character from the input and returns the
ASCII code of the letter.
- More importantly, it will also read whitespace
charcters.
Consider the following programs:
| ex.cpp
| ex2.cpp
|
#include <iostream>
using namespace std;
int main()
{
int N = 10;
char* A = new char[N];
for(int i=0; i < N; i++)
cin >> A[i];
for(int i=0; i < N; i++)
cout << A[i];
cout << endl;
delete [] A;
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
int N = 10;
char* A = new char[N];
for(int i=0; i < N; i++)
A[i] = cin.get();
for(int i=0; i < N; i++)
cout << A[i];
cout << endl;
delete [] A;
return 0;
}
|
$ ./ex
* * * * * * * * * *
**********
|
$ ./ex2
* * * * * * * * * *
* * *
|
Where would it be useful?
In your project!.
You will need to read a map data where a blank means a lot.
Other sample program?
Check out the notes on "Class 23: 2D-Arrays". There is a practice problem of
drawing a picture.
Mandatory Practice Problem
Be the bank! Write a program that manages account
information for a simple bank.
- You have the file
BankAccts.txt, which lists all
your bank account information at the beginning of the year.
-
You also have the file
Transactions.txt, which lists
all the deposit transactions for the year. Each transaction
consists of
- the date
- the account number, and
- the $'s deposited.
Your program should print out an end of the year report that lists all
the accounts with their account numbers, owner name, and end of year balance in
exactly the same format as used in the input file.
$ ./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 the solution.