1 / 27

CS112 – 2017 / 2018 2 nd Term Programming I Lecture 15 & 16: C++ Valarrays and 2D Arrays

Cairo University, Faculty of Computers and Information. CS112 – 2017 / 2018 2 nd Term Programming I Lecture 15 & 16: C++ Valarrays and 2D Arrays. By Dr. Mohamed El-Ramly. Lecture Objectives C++ Other Types Of Arrays. 1- Why valarray s 2- Declaring valarray s 3- Using valarray s

landrea
Download Presentation

CS112 – 2017 / 2018 2 nd Term Programming I Lecture 15 & 16: C++ Valarrays and 2D Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cairo University, Faculty of Computers and Information CS112 – 2017 / 2018 2nd TermProgramming ILecture 15 & 16: C++ Valarrays and 2D Arrays By Dr. Mohamed El-Ramly

  2. Lecture ObjectivesC++ Other Types Of Arrays • 1- Why valarrays • 2- Declaring valarrays • 3- Using valarrays • 4- valarraysize • 5- Uniary operators valarray • 6- Assignment operators valarray • 7- Binary operators valarray • 8- Comparing valarrays • 9- Functions on valarrays

  3. Lecture ObjectivesC++ Other Types Of Arrays 10- 2D arrays.

  4. 1. valarray What are the limitations of arrays ? 1- Fixed size 2- No predefined operations 3- Does not know how many elements are there

  5. 1. Vectors and valarray • Valarrays (for mathematical vectors) • Have lots of predefined operations and operators • But still fixed capacity • Only numeric type • Highly optimized for math vector operations • Vectors • Have predefined operations • Can grow when necessary (but not shrink) • Elements can be of any type

  6. 2. Declare valarray • valarray<T> V; • valarray<T> V(n); • valarray<T> V(value, n); • valarray<T> V(array); • valarray<T> V(anotherValarray);

  7. vector<int> v(10);

  8. 2. Declare valarray • valarray<double> v1; • valarray<float> v2(10); • valarray<int> v3(999, 10); • const double a[] = {1.1, 2.2, 3.3, 4.4, 5.5}; • valarray<double> v4(a, 4); • valarray<double> v5(v4);

  9. 3. Using / Accessing valarray • valArr [5] // item 5 • for (int i= 0; i < valArr.size();i++) cout << valArr [i] << " " ; • valArr = value (goes to all elements) • valArr = anotherValArr(same size) • This is not possible with arrays

  10. 4. Size / Resize valarray • valArr.size(); • valArr.resize (12); // resize to 12 items, each = 0 • valArr.resize (12, 101); // resize to 12 items, each= 101

  11. 5. Unary Operators valarray • + - ! ~ • valarray<int> v11({1, -2, 3,-4}); • valarray<int> v12(-v11); • valarray<bool> v13(!v11); // Not • valarray<int> v14(~v11); // Invert

  12. 6. Assignment Operators valarray • += -= *= /= %= • &= |= ^= <<= >>= • valarray<int> v21({1, 21, 13,4}); • v21 *= v22; • v21 %= 11; -> valarray {1, 10, 2, 4} • v21 >>= 1;

  13. 7. Binary Operators valarray • + - * / % & | ^ << >> && || • valArr1 + valArr2SameLEngth • valArr1 + value • valarray<int> v31({1, -2, 3,-4}); • valarray<int> v32({5, -6, 7,-8}); • valarray<int> v33 = v31 + v32;

  14. 8. Comparing valarray • > < <= >= != == • valArr1 < valArr2SameLEngth • valArr1 >= value • valarray<int> v31({1, -2, 3,-4}); • valarray<int> v32({5, -6, 7,-8}); • V31 != v32 • V31 > -10

  15. 9. Functions on valarray • sum(), min(), max() • shift(int n) • cshift(int n) • apply (function) • swap() // Swaps two valarrays • begin(), end() • abs()

  16. Pb#19: Average, STDev Calculate the average and standard deviation for a set of numbers using • Arrays • valarray 19

  17. 10- 2D and Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of rows and columns: const int NUMROWS = 3; const int NUMCOLS = 7; int Array[NUMROWS][NUMCOLS]; Array[2][5] 3rd value in 6th column Array[0][4] 1st value in 5th column The declaration must specify the number of rows and the number of columns, and both must be constants.

  18. Processing a 2-D Array A one-dimensional array is usually processed via a for loop. Similarly, a two-dimensional array may be processed with a nested for loop: for (int Row = 0; Row < NUMROWS; Row++) { for (int Col = 0; Col < NUMCOLS; Col++) { Array[Row][Col] = 0; } } Each pass through the inner for loop will initialize all the elements of the current row to 0. The outer for loop drives the inner loop to process each of the array's rows.

  19. Initializing in Declarations int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} }; int Array2[2][3] = { 1, 2, 3, 4, 5 }; int Array3[2][3] = { {1, 2} , {4 } }; If we printed these arrays by rows, we would find the following initializations had taken place: Rows of Array1: 1 2 3 4 5 6 Rows of Array2: 1 2 3 4 5 0 Rows of Array3: 1 2 0 4 0 0 for (int row = 0; row < 2; row++) { for (int col = 0; col < 3; col++) { cout << setw(3) << Array1[row][col]; } cout << endl; }

  20. Higher-Dimensional Arrays An array can be declared with multiple dimensions. 2 Dimensional 3 Dimensional double Coord[100][100][100]; Multiple dimensions get difficult to visualize graphically. •

  21. 2-D Arrays as Parameters When passing a two-dimensional array as a parameter, the base address is passed, as is the case with one-dimensional arrays. But now the number of columns in the array parameter must be specified. This is because arrays are stored in row-major order, and the number of columns must be known in order to calculate the location at which each row begins in memory: address of element (r, c) = base address of array + r*(number of elements in a row)*(size of an element) + c*(size of an element) void Initialize(int TwoD[][NUMCOLS], const int NUMROWS) { for (int i = 0; i < NUMROWS; i++) { for (int j = 0; j < NUMCOLS; j++) TwoD[i][j] = -1; } }

  22. Readings • Read notes posted in acadox

More Related