Download presentation
Presentation is loading. Please wait.
Published byKaren Holmes Modified over 9 years ago
2
http://cs.mst.edu The Basics of Arrays
3
http://cs.mst.edu Problem: How can the rancher easily catalog all of his cattle?
4
http://cs.mst.edu Possible Solution string bessie; string mr_tongue; string miss_fancy; string jumper; … string blondie; Mr. Tongue
5
http://cs.mst.edu New Solution: Arrays const short HERD_SIZE=60; string my_herd[HERD_SIZE];
6
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];
7
http://cs.mst.edu Arrays Visualized const int SAMPLE_SIZE=340; float deflection_readings[SAMPLE_SIZE]; 3.563.444.033.963.773.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
8
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 3.563.444.033.963.773.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
9
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 3.563.444.033.963.773.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
10
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 45.13.444.033.963.773.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
11
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 45.13.444.033.963.773.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
12
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 45.13.444.033.9612.13.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
13
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 45.13.444.033.9612.13.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
14
http://cs.mst.edu Accessing Elements deflection_readings[0]=45.1; deflection_readings[4]=12.1; cout << deflection_readings[6] << endl; 45.13.444.033.9612.13.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
15
http://cs.mst.edu Access Mistakes const int SAMPLE_SIZE=340; float deflection_readings[SAMPLE_SIZE];... cout << deflection_readings[SAMPLE_SIZE]; 45.13.444.033.9612.13.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339]
16
http://cs.mst.edu Access Mistakes const int SAMPLE_SIZE=340; float deflection_readings[SAMPLE_SIZE];... cout << deflection_readings[SAMPLE_SIZE]; 45.13.444.033.9612.13.493.92 … 4.013.833.21 [0][1][2][3][4][5][6]…[337][338][339] ? [340] “Walking off the array” !! - This is not your program’s memory
17
http://cs.mst.edu Initializing Arrays const int SIZE=5; int array[SIZE]={7,2,4,5,1}; 72451 [0][1][2][3][4]
18
http://cs.mst.edu Initializing Arrays const int SIZE=5; int array[SIZE]={0}; 00000 [0][1][2][3][4]
19
http://cs.mst.edu Initializing Arrays const int SIZE=5; int array[SIZE]={9,1}; 91000 [0][1][2][3][4]
20
http://cs.mst.edu Initializing Arrays int array[]={5,2,4}; 524 [0][1][2]
21
http://cs.mst.edu Things You Can NOT Do Return an array from a function Output an entire array like int, float, or strings int array[10]; cout << array << endl; Read in to an entire array int array[10]; cin >> array;
22
http://cs.mst.edu Things You Can NOT Do Return an array from a function Output an entire array like int, float, or strings int array[10]; cout << array << endl; Read in to an entire array int array[10]; cin >> array;
23
http://cs.mst.edu Things You Can NOT Do Return an array from a function Output an entire array like int, float, or strings int array[10]; cout << array << endl; Read in to an entire array int array[10]; cin >> array;
24
http://cs.mst.edu End of Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.