Lecture Six Arrays Arrays An array is a
- Slides: 33
Lecture Six Arrays
Arrays An array, is a list of individual items that all have the same fundamental type. An array can hold multiple variables with the same type in adjacent memory locations. The variables in the array all use the array name and are distinguished from one another by their subscripts. A subscript is a number that indicates the position of the particular array element being used. An element is a single object in an array. A subscript is used to directly access a particular array element. Friday, March 5, 2021 Data Structure 2
Array Declaration In C++, you declare an array by using the following syntax: type array. Name [size]; Where type is any simple type, (int, float, char, …) array. Name is any legal identifier, and size (in the square brackets) represents the number of elements the array contains. Friday, March 5, 2021 Data Structure 3
Array Declaration For example, the following statement declares an array of five elements, each of which is type double: double money. Collected[5]; The elements in an array are numbered from zero to one less than the size of the array. Friday, March 5, 2021 C++ Programming Language 4
Arrays The array declaration int some. Numbers[7]; declares an array that holds seven integers. The name of the array represents the beginning address of the array. cout << some. Numbers; The following statement produces identical output because the name of the array is the address of the array’s first element: cout << &some. Numbers[0]; Similarly, the following two statements produce the same results: cout << (some. Numbers + 1); cout << &some. Numbers[1]; The first statement display the address of some. Numbers plus one more integer, so if the array is stored at memory address 3000, for example, and integers are four bytes, then the statement displays 3004. The second statement display the address of the second element of the some. Numbers array. Friday, March 5, 2021 C++ Programming Language 5
Arrays The subscript that is used to access an element of an array indicates how much is added to the starting address to locate a value. For example, if you declare an array as Int some. Numbers[7]; and if an int requires four bytes of storage in your system, when you access the value of some. Numbers[2], you access the value of exactly two integers, or eight bytes away from the beginning address of the array. Friday, March 5, 2021 C++ Programming Language 6
Initialization Syntax: int X[4] = {2, 4, 7, 9}; Behavior: initialize elements starting with leftmost, i. e. element 0. Remaining elements are initialized to zero. X 2 4 7 9 0 1 2 3 Initialize all to 0: int X[4]={0}; Friday, March 5, 2021 Data Structure 7
STORING VALUES IN AN ARRAY This program segment shows the declaration of a four-element integer array named rent, and also shows how you can assign a rent value to each array element. int rent[4]; rent [0]= 250; rent [1]= 375; rent [2]= 460; rent [3]= 600; Friday, March 5, 2021 Data Structure 8
int STORING VALUES IN AN ARRAY rent[4] = {250, 375, 460, 600}; The four values listed within the curly braces are assigned, in sequence, to rent[0], rent[1], rent[2], and rent[3]. If you declare an array without a size, but provide initialization values, C++ creates an array with the exact size you need. For example, the following two array declarations create identical arrays: int rent[4] = {250, 375, 460, 600}; int rent[] = {250, 375, 460, 600}; Friday, March 5, 2021 C++ Programming Language 9
Operations with Arrays • Assignment – x[0] = 6; /* Assign 6 to element x[0] */ – y[2] = 3. 1; /* Assign 3. 1 to element y[2] */ • Access – m = x[2]; – p = y[0]; • Input/Output: – the elements are handled as their types, e. g. Cin<<&x[2]<< &y[3]; Cout<<x[0]<< y[2]; /* output 6 and 3. 1 */ Friday, March 5, 2021 Data Structure 10
Arithmetic Operations int main() { double x[5]; } Variable Declaration for the array x[0] = 1; x[1] = 2; x[2] = x[0] + x[1]; x[3] = x[2] / 3; x[4] = x[3] * x[2]; Friday, March 5, 2021 /* X[2] = 3 */ /* X[3] = 1 */ /* X[4] = 3 */ Data Structure 11
for loops “for” loops are ideal for processing elements in the array. int main() { int i; double values[4] = {3. 14, 1. 0, 2. 61, 5. 3}; double sum. Values = 0. 0; for (i=0; i<4; i++) { sum. Values = sum. Values + values[i]; } cout<<“Sum = %lfn”<<sum. Values; } Friday, March 5, 2021 Data Structure 12
for loops “for” loops are ideal for processing elements in the array. int main() { int i; double values[4] = {3. 14, 1. 0, 2. 61, 5. 3}; double sum. Values = 0. 0; ERROR! Out of bound for (i=0; i<=4; i++) { sum. Values = sum. Values + values[i]; } cout<<“Sum = %lfn”<< sum. Values; } Friday, March 5, 2021 Data Structure 13
int main() { double grades[5] = {90, 87, 65, 92, 100}; double sum; int i; Example cout<<"The first grade is: %. 1 fn", grades[0]; sum = 0; for(i=0; i<5; i++) { sum += grades[i]; } cout<<"The average grade is: %. 1 fn", sum / 5; } grades[2] = 70; /* Replaces 65 */ grades[3] = grades[4]; /* Replaces 92 with 100 */ Friday, March 5, 2021 Data Structure 14
ACCESSING AND USING ARRAY VALUES #include<iostream> int main(){ const int SIZE = 5; int single. Int = 52; int array. Int[SIZE] = {12, 36}; cout << "Single. Int is " << single. Int << endl; cout << "First array element is " << array. Int[0] << endl; ++single. Int; ++array. Int[0]; cout << "After incrementing, " << "single. Int is " << single. Int << endl; cout << "After incrementing, " << "first array element is " << array. Int[0] << endl; single. Int = single. Int * 2; array. Int[0] = array. Int[0] * 2; cout << "After doubling, single. Int is " << single. Int << endl; cout << "After doubling, first array element is " << array. Int[0] << endl; return 0; } Friday, March 5, 2021 C++ Programming Language 15
ACCESSING AND USING ARRAY VALUES const int SZ_OF_ARRAY = 5; int array. Int[SZ_OF_ARRAY] = {34, 56, 12, 3, 99}; for(int x = 0; x < SZ_OF_ARRAY; ++x) cout << array. Int[x] << endl; Friday, March 5, 2021 C++ Programming Language 16
ACCESSING AND USING ARRAY VALUES #include<iostream> int main() { const int NUM_PRICES = 10; double price[NUM_PRICES]; int sub; for(sub = 0; sub < NUM_PRICES; ++sub) { cout << "Enter a price "; cin >> price[sub]; } cout << endl << "The entered prices, in reverse order: " << endl; for(sub = NUM_PRICES - 1; sub >= 0; --sub) cout << price[sub] << " "; cout << endl; return 0; } Friday, March 5, 2021 C++ Programming Language 17
Constants for capacity Good programming practice: use #define for constants in your program For example: #define Max. Limit 25 int grades[Max. Limit]; for(int i; i<Max. Limit; i++){ }; If size needs to be changed, only the capacity “Max. Limit” needs to be changed. Friday, March 5, 2021 Data Structure 18
2 -D Arrays int cave[Array. Size]; Column 0 Row 1 2 3 Friday, March 5, 2021 0 1 2 3 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 Data Structure 19
2 D Arrays Column 0 Row 1 2 0 1 2 3 1 5 9 2 6 10 3 7 11 4 8 12 my. Matrix[0][1] 2 my. Matrix[2][3] 12 my. Matrix[row][col] int my. Matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; Friday, March 5, 2021 Data Structure 20
Physically, in one block of memory int my. Matrix[2][4] = { {1, 2, 3, 4}, {5, 6, 7, 8} }; ffe 2 de 0 c ffe 2 de 10 ffe 2 de 14 ffe 2 de 18 ffe 2 de 1 c ffe 2 de 20 ffe 2 de 24 ffe 2 de 28 1 2 3 4 5 row 1 6 7 8 row 2 Array elements are stored in row major order. Row 1 first, followed by row 2, row 3, and so on Friday, March 5, 2021 Data Structure 21
2 D Array Name and Addresses int my. Matrix[2][4] = { {1, 2, 3, 4}, {5, 6, 7, 8} }; ffe 2 de 0 c ffe 2 de 10 ffe 2 de 14 ffe 2 de 18 ffe 2 de 1 c ffe 2 de 20 ffe 2 de 24 ffe 2 de 28 1 2 3 4 5 6 7 8 my. Matrix: pointer to the first element of the 2 D array my. Matrix[0]: pointer to the first row of the 2 D array my. Matrix[1]: pointer to the second row of the 2 D array Friday, March 5, 2021 Data Structure 22
Accessing 2 D Array Elements int my. Matrix[2][4] = { {1, 2, 3, 4} , {5, 6, 7, 8} }; 1 2 3 4 5 6 7 8 Indexing: my. Matrix[i][j] is same as (my. Matrix[i] + j) ( (my. Matrix + i))[j] (( (my. Matrix + i)) + j) (&my. Matrix[0][0] + 4*i + j) Friday, March 5, 2021 Data Structure 23
Declaration #define ROWS 3 #define COLS 5 int table[ROWS][COLS]; Friday, March 5, 2021 Data Structure 24
for (int i=0; i < ROWS; i++) { for (int j=0; j < COLS; j++ ) { cout<<" x[%d]: %d", i, j, x[i][j]; } cout<<"n”; } 2 D Arrays often require nested loops – two variables Friday, March 5, 2021 Data Structure 25
Table A = { {13, 22, 9, 23}, {17, 5, 24, 31, 55}, {4, 19, 29, 41, 61} }; 13 17 4 22 5 19 9 24 29 23 31 41 ? 55 61 1 6 11 Table B = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; Friday, March 5, 2021 Data Structure 2 7 12 3 8 13 4 9 14 5 10 ? 26
USING CHARACTER ARRAY STRINGS In C++ a character variable can hold a single character value, such as ‘A’ or ‘$’. Single character values are always expressed within single quotation marks. In C++, if you want to express multiple character values as a constant, such as a first name or a book title, you use double quotation marks. A C++ value expressed within double quotation marks is commonly called a string, or a literal string. Friday, March 5, 2021 Data Structure 27
String When you want to store related characters that are used together, such as someone’s name, then the characters constitute a string. You also can store the individual characters of a string in an array. However, C++ programmers do not refer to an array of characters as a string unless the last usable character in the string is the null character. The null character is represented by the combination ‘ ’ (backslash and zero), or by using the constant NULL. Friday, March 5, 2021 Data Structure 28
String Declaration If you want to declare a string named first. Name and initialize it to “Mary”, each of the following statements provides the same result. char first. Name[] = "Mary"; char first. Name[] = {"Mary"}; char first. Name[5] = "Mary"; char first. Name[5] = {"Mary"}; char first. Name[5] = {'M', 'a', 'r', 'y', '