The Basics of Arrays · Problem: How can the rancher easily catalog all of his cattle?

Preview:

Citation preview

http://cs.mst.edu

The Basics of Arrays

http://cs.mst.edu

Problem: How can the rancher easily catalog all of his cattle?

http://cs.mst.edu

Possible Solution

string bessie;

string mr_tongue;

string miss_fancy;

string jumper;

string blondie; Mr. Tongue

http://cs.mst.edu

New Solution: Arrays

const short HERD_SIZE=60;

string my_herd[HERD_SIZE];

http://cs.mst.edu

Array Allocation Syntax

base_type array_name[number of cells];

// examples

int student_numbers[50];

float student_grades[50];

string student_names[50];

const short NUM_FRIENDS = 2;

string my_friends_names[NUM_FRIENDS];

http://cs.mst.edu

Arrays Visualized

const int SAMPLE_SIZE=340;

float deflection_readings[SAMPLE_SIZE];

3.56 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

3.56 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

3.56 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

45.1 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

45.1 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Accessing Elements

deflection_readings[0]=45.1;

deflection_readings[4]=12.1;

cout << deflection_readings[6] << endl;

45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Access Mistakes

const int SAMPLE_SIZE=340;

float deflection_readings[SAMPLE_SIZE];

...

cout << deflection_readings[SAMPLE_SIZE];

45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

http://cs.mst.edu

Access Mistakes

const int SAMPLE_SIZE=340;

float deflection_readings[SAMPLE_SIZE];

...

cout << deflection_readings[SAMPLE_SIZE];

45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21

[0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]

?

[340]

“Walking off the array” !! - This is not your program’s memory

http://cs.mst.edu

Initializing Arrays

const int SIZE=5;

int array[SIZE]={7,2,4,5,1};

7 2 4 5 1

[0] [1] [2] [3] [4]

http://cs.mst.edu

Initializing Arrays

const int SIZE=5;

int array[SIZE]={0};

0 0 0 0 0

[0] [1] [2] [3] [4]

http://cs.mst.edu

Initializing Arrays

const int SIZE=5;

int array[SIZE]={9,1};

9 1 0 0 0

[0] [1] [2] [3] [4]

http://cs.mst.edu

Initializing Arrays

int array[]={5,2,4};

5 2 4

[0] [1] [2]

http://cs.mst.edu

Things You Can NOT Do

Return an array from a function

Output an entire array like int, float, or stringsint array[10];

cout << array << endl;

Read in to an entire arrayint array[10];

cin >> array;

http://cs.mst.edu

Things You Can NOT Do

Return an array from a function

Output an entire array like int, float, or stringsint array[10];

cout << array << endl;

Read in to an entire arrayint array[10];

cin >> array;

http://cs.mst.edu

Things You Can NOT Do

Return an array from a function

Output an entire array like int, float, or stringsint array[10];

cout << array << endl;

Read in to an entire arrayint array[10];

cin >> array;

http://cs.mst.edu

End of Session

Recommended