with c
play

WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. - PowerPoint PPT Presentation

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1 Arrays Prof. Amr Goneid, AUC 2 1-D Arrays Data Structures The Array Data Type How to Declare an Array


  1. CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1

  2. Arrays Prof. Amr Goneid, AUC 2

  3. 1-D Arrays  Data Structures  The Array Data Type  How to Declare an Array  Operations on Arrays  Passing to and from Functions  Examples  Example Functions Prof. Amr Goneid, AUC 3

  4. 1. Data Structures Data Set Linear Tree Graph Prof. Amr Goneid, AUC 4

  5. Data Structures  Sets: No structure, just membership.  Linear: Sequential, one-to-one. e.g Arrays, Strings and Streams  Tree: Non-Linear, one-to-many.  Graph: Non-Linear, many-to-many. Arrays, Structures and Classes are used to model different data structures. Prof. Amr Goneid, AUC 5

  6. 2. The Array Data Type  Array elements have a common name  The array as a whole is referenced through the common name  Array elements are of the same type — the base type  Individual elements of the array are referenced by sub_scripting the group name by an index Prof. Amr Goneid, AUC 6

  7. 1-D Arrays Prof. Amr Goneid, AUC 7

  8. 1-D Arrays  Linear Data Structure (One-To-One)  Fixed Size n (Static)  All elements are of the same type  An element is accessed by an ordinal index with values between a lower bound (0) and an upper bound (n-1) index 0 n -1 Prof. Amr Goneid, AUC 8

  9. Array Size  Size = No. of Elements = n  UB = n - 1  Number of Bytes = size * Element size. All elements are of the same size.  Maximum size of 64 Kbytes . 250 elements = 500 bytes 0 249 int 26 elements = 104 bytes 0 25 float Prof. Amr Goneid, AUC 9

  10. 3. How to Declare an Array  Syntax: <basetype> <name> [size] ; e.g. int a [20] ; float x [101] ; const Maxelem = 200; typedef int itemtype; itemtype y [Maxelem+1]; string name [51 ]; enum color {red , green , blue}; color pixel [201]; int npixels[3]; Prof. Amr Goneid, AUC 10

  11. Sample Declarations  Suppose const int N = 20; const int M = 40; const int MaxStringSize = 80; const int MaxListSize = 1000 ; Prof. Amr Goneid, AUC 11

  12. Sample Declarations  Then the following are all correct array declarations. int A[10]; char B[MaxStringSize]; float C[M*N]; int Values[MaxListSize]; Rational D[N-15]; Prof. Amr Goneid, AUC 12

  13. 4. Operations on Arrays  Declaration with Initialization: e.g. int x [6] = {12 , 23 , 56 , 34 , 18 , 20}; char g[ ] = { ‘A’ , ‘B’ , ‘C’ ,’D’ , ‘F’ }; //This sets the size of the array g to 5 elements Prof. Amr Goneid, AUC 13

  14. Operations on Arrays  Accessing an Element: <array name>[index] 0 =< index =< UB index (location of element in the array) can be a const, variable or any integral / ordinal expression. e.g. x[2] or y[i] or x[2*n+1] Prof. Amr Goneid, AUC 14

  15. Operations on Arrays  Index manipulation: int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; 12 Prof. Amr Goneid, AUC 15

  16. Operations on Arrays Warning  C++ will not flag an error if the array subscript goes out of bounds  Ex: int B[10]; // declare B with 10 elements B[20] = 2; // subscript out of range This is allowed by the compiler, but will lead to unpredictable behavior  Beware! 12 Prof. Amr Goneid, AUC 16

  17. Operations on Arrays  Retrieve an element: e.g. z = x[ i ] ;  Update an element: e.g. name[ i ] = “Ann” ; pixel[100] = red; npixels[green] ++ ;  Input an element of an array: cin >> x[i];  Output an element of an array: cout << x[i]; Prof. Amr Goneid, AUC 17

  18. 5. Passing to and from Functions  Passing an array element as a parameter : e.g. void swap ( int &a , int &b); invoke as swap ( x[i] , x[j] ) ;  Passing an entire array by reference: e.g. int findmax ( int x [ ] , int size ); invoke as m = findmax ( x , n); Prof. Amr Goneid, AUC 18

  19. Remember  Arrays are always passed by reference  Can use const if array elements are not to be modified, e.g. int findmax ( const int x [ ] , int size );  You do not need to include the array size within the brackets when defining an array parameter.  This is because the array name is the base address of the array, and the size is already known. Prof. Amr Goneid, AUC 19

  20. 6. Examples  A function to input n elements of an integer array and return the array and n as parameters: const MAX_SIZE = 200; int a [MAX_SIZE]; void input_array ( int a[ ] , int &n ) { int n = 0; int v; while((n < MAX_SIZE) && (cin >> v)) { a[n] = v; n++; } } Prof. Amr Goneid, AUC 20

  21. Examples  A function that receives an integer array and lists the first n elements of the array : void output_array ( const int a[ ] , int n ) { for (int i = 0 ; i < n ; i++) cout << a[i] << “ “ ; cout << endl; } Prof. Amr Goneid, AUC 21

  22. Examples  A function to receive an integer array and return the index of the minimum element in the sub-array starting at index (s) and ending at index (e): int index_of_min ( int a[ ] , int s , int e ) { int imin = s; for (int i = s+1; i <= e ; i++) if (a[i] < a[imin]) imin = i ; return imin ; } Prof. Amr Goneid, AUC 22

  23. Examples  A function to receive a real array and return the average value of the elements. float average( float x[ ] , int n ) { float sum = 0; for (int i = 0; i < n ; i++) sum += x[i]; return (sum / float (n)) ; } Prof. Amr Goneid, AUC 23

  24. 7. Example Functions  Function1: Computing the Average and Standard Deviation of a list of numbers.  Function2: Linear (Sequential) Search in an array.  Function3: Sorting an array using Selection Sort.  Function4: Sorting an array using Bubble Sort  Function5: Searching an array using Binary Search Prof. Amr Goneid, AUC 24

  25. Function 1: Average & Standard Deviation The average (av) of array x[0..n-1] is computed as the sum of all elements divided by n. The variance is defined as the average of the squared deviations from the average value. The Standard Deviation is the square root of the variance. − − 2 n 1 n 1 ( ) 1 ∑ 1 ∑ 〈 〉 = = − 〈 〉 σ = x x , v x x , v i i n n = = i 0 i 0 Prof. Amr Goneid, AUC 25

  26. Function stat void stat ( const float x[ ], int n, float &av, float &sd) { float d; float var = 0; av = average(x,n); for (int i = 0; i < n ; i++) { d = x[i] – av ; var += d*d; } sd = sqrt(var / float(n)); } Prof. Amr Goneid, AUC 26

  27. Function 2: Linear Search  The idea of a linear search is to walk through the entire array until a target value is located. If found, its location is returned.  If the target is not located some type of indicator needs to be returned Prof. Amr Goneid, AUC 27

  28. Linear Search Function // Searches an integer array of size (n) // for a given element (the target) // Array elements ranging from 0 to // n - 1 are searched for an element // equal to target. // Returns the subscript of target if // found; otherwise, returns -1. Prof. Amr Goneid, AUC 28

  29. linSearch Function (Cont.) int linSearch (const int a[ ], int target, int n) { for (int i = 0; i < n; i++) if (a[i] == target) return i; // All elements were tested without success. return -1; } // end linSearch Prof. Amr Goneid, AUC 29

  30. Function 3: Selection Sort  Assume elements to be in locations 0..n-1  Let (i) be the start of a sub-array of at least 2 elements, i.e. i = 0 .. n-2  for each i = 0 .. n-2 find smallest element in sub-array a[i] to a[n-1]. swap that element with that at the start of the sub-array. Prof. Amr Goneid, AUC 30

  31. How it works 4 3 1 6 2 5 1 3 4 6 2 5 1 2 4 6 3 5 1 2 3 6 4 5 1 2 3 4 6 5 1 2 3 4 5 6 Prof. Amr Goneid, AUC 31

  32. Selection Sort Algorithm void selectsort (itemType a[ ], int n) { int i , j , m; for (i = 0; i < n-1; i++) { m = i ; for ( j = i+1; j < n; j++) if (a[j] < a[m]) m = j ; swap (a[i] , a[m]); } } Prof. Amr Goneid, AUC 32

  33. Function 4: Bubble Sort  The general idea is to compare adjacent elements and swap if necessary  Assume elements to be in locations 0..n-1  Let (i) be the index of the last element in a sub- array of at least 2 elements, i.e. i = n-1 .. 1 Prof. Amr Goneid, AUC 33

  34. Bubble Sort Algorithm  for each i = n-1…1 Compare adjacent elements a j and a j+1 , j = 0..i-1 and swap them if a j > a j+1 This will bubble the largest element in the sub-array a[0..i] to location (i). Prof. Amr Goneid, AUC 34

  35. How it works 4 3 1 2 3 4 1 2 3 1 4 2 3 1 2 4 1 3 2 4 1 2 3 4 Prof. Amr Goneid, AUC 35

  36. Bubble Sort Algorithm void bubbleSort (itemType a[ ], int n) { int i , j; bool swapped; for (i = n; --i >= 0; ) { swapped = false; for (j = 0; j < i; j++) { if (a[j] > a[j+1] ) { swap(a[j] , a[j+1]); swapped = true; } } if (!swapped) return; } } Prof. Amr Goneid, AUC 36

  37. Function 5: Binary Search Prof. Amr Goneid, AUC 37

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend