Topics to Cover

Static Arrays

Dynamic arrays vs static arrays

There are two types of arrays in C++:
Creating an Array of 6 ints
Static Array Dynamic Array

int A[6];

int* A = new int[6];
Using the array after its been created is pretty much the same for either.

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]). 

You can sometimes write simpler code using static arrays

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:

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 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 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();
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 3!. You will need to read a map data where a blank means a lot.

Other sample program?

Check out the practice problem of drawing a picture.

Mandatory Practice Problem - not related to today's new content

Use structs to be the bank! Write a program that manages account information for a simple bank. 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 the format shown below.
$ ./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.

Practice Problems

  1. Write a program that reads a date in mm/dd/yyyy format and prints it out in "dd monthname yyyy" format.

    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.
  2. Drawing a picture (possibly flipped). See apple.txt

    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
    
    
                              $$$$
                            $$$$$$
                           $$$$$$
                           $$$$
                           $$
              $$$$$$$$$$$$$ $$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
          $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
             $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
               $$$$$$$$$$$$$$$$$$$$$$$$$
                 $$$$$$$$$$$$$$$$$$$$$
                   $$$$$$$$  $$$$$$$
    
    > vflip
    
                   $$$$$$$$  $$$$$$$
                 $$$$$$$$$$$$$$$$$$$$$
               $$$$$$$$$$$$$$$$$$$$$$$$$
             $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
          $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
              $$$$$$$$$$$$$ $$$$$$$$$$$$$
                           $$
                           $$$$
                           $$$$$$
                            $$$$$$
                              $$$$
    
    > quit
    
    Solution