Download presentation
Presentation is loading. Please wait.
Published byShannon Little Modified over 9 years ago
1
CHAPTER: 12
2
Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double
3
Problem: How to store numerous values of same data type. E.g. store 100 numbers and find max. of those. Can we store like: int a, b, c, d, e……. z, A, B, C ……. Z, aa, ab, ac…. az, Aa, Ab, Ac…. Av; Solution: int a[100]; // an array
4
1. Single Dimensional Arrays 2. Multi Dimensional Arrays
6
Form: datatype arrayname[arraySize]; Example: double myList[10]; Each element in the array is referred to by an index. myList[0] references the first element in the array. myList[9] references the last element in the array.
7
Once an array is created, its size is fixed. It cannot be changed. You can find its size using sizeof(arrayName); For example, sizeof(myList) returns 10
8
Using a loop: for (int i = 0; i < sizeof(myList); i++) myList[i] = i; // cin>>myList[i]; Declaring, creating, initializing in one step: double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.
9
double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
10
Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double myList[]; myList = {1.9, 2.9, 3.4, 3.5};
11
Example: double myList[10]; myList[0]myList[1]myList[2]myList[3]..... myList[9] 2000200420082012.....2018
12
Arrays are easily printed using for loops. However, formatting the output may require some thought. Determine the output in each case shown below. Example 1: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int j = 0; j < 12; j++) cout << Grades[ j ] << endl; Example 2: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int j = 0; j < 12; j+=3) cout << Grades[ j ] << Grades[j+1] << Grades[j+2] << endl;
13
Example 3: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int Row = 0; Row <= 2; Row++) for (int Col = 0; Col <=3; Col++) cout << Grades[ 4*Row+Col ] << endl; Example 4: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int Row = 0; Row <= 2; Row++) { for (int Col = 0; Col <=3; Col++) cout << Grades[ 4*Row+Col ]; cout << endl; }
14
Write a program to accept a list of numbers (e.g. 10 numbers) from user and search for a specific number given by user.
15
So far we have used one-dimensional (1D) arrays, but we can also use arrays with 2 or more dimensions. 2D arrays essentially correspond to matrices. Example: int A;// single variable int B[6];// 1D array int C[3][4];// 2D array (matrix) int D[3][4][5];// 3D array int E[3][4][5][3];// 4D array //etc How many variables are stored in each case? ArrayNumber of variables B C D E 15
16
Multi-dimensional Arrays One subscript for each dimension Use square brackets for each subscript Each subscript begins with 0 A 2D array is typically represented as a matrix Example: int A[3][4];// 2D array (matrix) with 3 rows and 4 columns Col 0 Col 1 Col 2 Col 3 Row 0 Row 1 Row 2 A[0][0] A[1][2] A[2][3] 16
17
Specifying values in arrays As with 1D arrays, values can be loaded into multidimensional arrays: explicitly (referring to each element by its indices) or using a list Example: Loading values into a matrix explicitly int A[2][3];// 2D array (matrix) with 2 rows and 3 columns A[0][0] = 1; A[0][1] = 2; A[0][2] = 3; A[1][0] = 4; A[1][1] = 5; A[1][2] = 6; 123 456 Results for matrix A: 17
18
Visualizing multi-dimensional arrays We often use sketches to help us visualize arrays, such as: 1D array - single row or column 2D array – matrix 3D array – cube of cells 4D array – row or column of cubes? Example: int A, B1[6], B2[6], C[3][4], D[3][4][5], E[3][4][5][3]; A B1 B2 CD E 18
19
Visualizing multi-dimensional arrays Although we might find the visualizations on the previous slide to be useful, note that C++ stores the variables linearly, with the last index varied first (then the second index from the last, etc). This is important when loading an array with a list. Example: int C[3][4], D[2][3][4]; C D C[0][0] C[0][1] C[0][2] C[0][3] C[1][0] C[1][1] C[1][2] C[1][3] C[2][0] C[2][1] C[2][2] C[2][3] D[0][0][0] D[0][0][1] D[0][0][2] D[0][0][3] D[0][1][0] D[0][1][1] D[0][1][2] D[0][1][3] D[0][2][0] D[0][2][1] D[0][2][2] D[0][2][3] D[1][0][0] D[1][0][1] D[1][0][2] D[1][0][3] D[1][1][0] D[1][1][1] D[1][1][2] D[1][1][3] D[1][2][0] D[1][2][1] D[1][2][2] D[1][2][3] Note that matrices are filled in by row when using a list. Note that the indices increase numerically for any array. In this case: (..,111,112,113,120,..) 19
20
Loading values into a matrix using a list Example: Fill out the matrices indicated below. int A[2][3] = {3,6,9,12,15,18}, B[2][3] = {7,8,9}, C[][2] = {2,4,6,8,10,12,14,16} ; // Note: If the leftmost index is omitted, the array is sized according // to the number of list elements. // Recall from 1D arrays the effect of listing fewer values than the // size of the array. Matrix A: Matrix B: Matrix C: 20
21
Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int matrix[10][10]; matrix[0][0] = 3; for (int i=0; i<10; i++) for (int j=0; j<10; j++) { matrix[i][j] = i+j; } double[][] x;
23
You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example, int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; This is equivalent to the following statements: int[][] array = new int[4][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
24
int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; array.length array[0].length array[1].length array[2].length
25
Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };
26
Objective: Use two-dimensional arrays to create two matrices, and then add and multiply the two matrices. TestMatrixOperationRun
27
c ij = a i1 b 1j +a i2 b 2j +a i3 b 3j +a i4 b 4j +a i5 b 5j
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.