23
http://cs.mst.edu The Basics of Arrays

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

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

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

http://cs.mst.edu

The Basics of Arrays

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

http://cs.mst.edu

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

Page 3: The Basics of Arrays ·  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

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

http://cs.mst.edu

New Solution: Arrays

const short HERD_SIZE=60;

string my_herd[HERD_SIZE];

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

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];

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

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]

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

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]

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

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]

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

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]

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

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]

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

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]

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

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]

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

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]

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

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]

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

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

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

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]

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

http://cs.mst.edu

Initializing Arrays

const int SIZE=5;

int array[SIZE]={0};

0 0 0 0 0

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

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

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]

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

http://cs.mst.edu

Initializing Arrays

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

5 2 4

[0] [1] [2]

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

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;

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

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;

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

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;

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

http://cs.mst.edu

End of Session