Download presentation
Presentation is loading. Please wait.
Published byPercival Rich Modified over 6 years ago
1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements are accessed using the base name with a numerical (integer) subscript Subscript numbering starts at zero Ends at N-1 where N is the number of elements in the array Array elements are stored in sequential memory locations
2
Declaring an Array Syntax: type arrayName[size];
type can be any valid C++ type Ex: int, float, double, char, string, … arrayName can be any valid C++ variable name size can be any positive integer (i.e. greater than zero) Specifies the number of elements in the array
3
Example Declarations int scores[10]; float salaries[25];
An array of 10 integers float salaries[25]; An array of 25 floating point numbers string names[4]; An array of 4 string variables
4
Accessing array elements
Array elements are accessed using the subscript operator Square brackets [num] Num: an integer between 0 and N-1, where N is the number of elements Error if num is less than zero or greater than or equal to N // an array of 10 integers int scores[10]; scores[0] is the first element of array scores[4] is the fifth element of array scores[9] is the last element of array
5
Working with array elements
Individual array elements are variables They are used and treated the same way any other variable is! Examples: int scores[10]; string names[4]; scores[0] = 80; names[1] = “Bob”; int sum= scores[5] + scores [6]; cout << “hello “ << names[1] << endl; if(scores[3] > scores[4]) { cout << “yes!” << endl; }
6
Sequential vs. Random access
Access any element at any time using a valid subscript number Sequential access Access in numerical order Example: int square[8]; for (int i = 0; i < 8; i++) { square[i] = i * i; } (stored in sequential memory locations) [0] [1] [2] [3] [4] [5] [6] [7] 1 4 9 16 25 36 49
7
Array Initialization Case 1:
type arrayName[N] = { val0, val1, …, val(N-1) }; Specify N initial values If fewer than N are given, unspecified values are uninitialized (random values) Case 2: type arrayName[] = { val0, val1, …, val(N-1) }; Size not specified in declaration – array automatically becomes size of initialization list Example float cube[5]= { 0.0, 1.0, 8.0, 27.0, 64.0 };
8
Array Storage Array elements are stored in sequential memory locations
Example: float cube[5]= { 0.0, 1.0, 8.0, 27.0, 64.0 }; (note: float type uses 32bits/4 bytes) Cube[5] Memory address Array index Value 0x1000 [0] 0.0 0x1004 [1] 1.0 0x1008 [2] 8.0 0x1012 [3] 27.0 0x1016 [4] 64.0
9
Passing Arrays to Functions
Arrays are always passed by reference (actually by pointer) Which means the elements can be changed by the function Pass entire array to a function by writing just its name (no subscripts or brackets) in the argument list of the function call Ex: func(array, 3); In both the function definition and prototype, use empty brackets ([ ]) to indicate an array Ex: void func(int A[], int size);
10
Passing Arrays to Functions (example)
// function definition void func(int A[], int size) { if(size >= 3) A[2]= 10; } // prototype void func(int[], int); main() { int array[3]= { 2, 4, 6 }; // call the function func(array, 3); cout << array[2] << endl; } This will output “10”
11
More on passing arrays Use keyword const to indicate that array argument cannot be changed by function Compiler will not allow changes to arrays passed with a const prefix This will not compile // function definition void func(const int A[], int size) { if(size >= 3) A[2]= 10; }
12
Individual elements are passed by value
Individual elements are treated like any other variable They are passed by value // function definition void func(int a) { a=3; } Neither func(x) or func(array[2]) will change the value passed in i.e. both x and array[2] will retain their original values after the “func()” is finished its execution
13
Multidimensional Arrays
Arrays can have more than one dimension Example: 2D array Commonly used to represent a table char ticTacToe[3][3]; Access ticTacTie[1][2]
14
Accessing a Multidimensional Array
Indexing starts at zero (like a 1D array)
15
Initialization Each inner pair of braces contains initial values for one row of the array matrix const int NUM_ROWS = 2; const int NUM_COLS = 3; float matrix[NUM_ROWS][NUM_COLS] = { {5.0, 4.5, 3.0}, {-16.0, -5.9, 0.0} };
16
Implicit Declaration Row dimension can be anonymous, but column dimension must be specified float matrix[][NUM_COLS] = {{5.0, 4.5, 3.0}, {-16.0, -5.9, 0.0}}; -> C++ must know the number of elements in each row in order to access a particular array element This can be used in function prototyping and definition
17
Function sumMatrix float sumMatrix (float table[][NUM_COLS], int rows)
{ float sum = 0.0; // Add each array element value to sum. for (int r = 0; r < rows; r++) for (int c = 0; c < NUM_COLS; c++) sum += table[r][c]; return sum; } * Note: NUM_COLS is global
18
Arrays with Several Dimensions
const int PEOPLE = 10; const int YEARS = 5; money sales[PEOPLE][YEARS][12];
19
Strings and Arrays of Characters
string object uses an array of char Can reference individual character of a string object in different ways name[ i ] name.at( i ) A useful member function of string class name.length()
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.