Lec2

1,004 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,004
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lec2

  1. 1. Data Structures and Abstract Data Types Chapter 2
  2. 2. Chapter Contents <ul><li>1 Data Structures, Abstract Data Types and Implementations </li></ul><ul><li>2 Static Arrays </li></ul><ul><li>3 Multidimensional Arrays (optional) </li></ul><ul><li>4 Dynamic Arrays </li></ul><ul><li>5 C-Style Structs (optional) </li></ul><ul><li>6 Procedural Programming </li></ul>
  3. 3. Chapter Objectives <ul><li>Look at ADTs, implementations in detail </li></ul><ul><li>Introduce arrays as ADTs </li></ul><ul><li>See arrays implemented as C++ static arrays </li></ul><ul><li>(Optional) Describe multidimensional arrays </li></ul><ul><li>Extend pointers to use in dynamic arrays </li></ul><ul><li>(Optional) Show use of C++ structs to model objects with multiple attributes </li></ul><ul><li>Show example of procedural programming paradigm </li></ul>
  4. 4. Data Structures, Abstract Data Types, and Implementations <ul><li>Consider example of an airplane flight with 10 seats to be assigned </li></ul><ul><li>Tasks </li></ul><ul><ul><li>List available seats </li></ul></ul><ul><ul><li>Reserve a seat </li></ul></ul><ul><li>How to store, access data? </li></ul><ul><ul><li>10 individual variables </li></ul></ul><ul><ul><li>An array of variables </li></ul></ul>
  5. 5. Data Structures, Abstract Data Types, and Implementations <ul><li>Implementation consists of </li></ul><ul><ul><li>Storage (data) structures </li></ul></ul><ul><ul><li>Algorithms for basic operations </li></ul></ul><ul><li>Note following figure </li></ul><ul><ul><li>C++ provides large collection of data types and structures </li></ul></ul>
  6. 6. C++ Data Types structured array struct union class simple integral enum char short int long bool address pointer reference floating float double long double
  7. 7. Structured Data Type <ul><li>A structured data type is a type that </li></ul><ul><ul><li>Stores a collection of individual components with one variable name </li></ul></ul><ul><ul><li>And allows individual components to be stored and retrieved by their position within the collection </li></ul></ul>
  8. 8. Arrays <ul><li>Collection of data elements </li></ul><ul><ul><li>All of same type </li></ul></ul><ul><ul><li>Each accessed by specifying position </li></ul></ul><ul><li>Static array </li></ul><ul><ul><li>Compiler determines how memory allocated </li></ul></ul><ul><li>Dynamic array </li></ul><ul><ul><li>Allocation takes place at run time </li></ul></ul>
  9. 9. Single Dimension Arrays <ul><li>Syntax: ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list }; </li></ul><ul><li>Example: int b [10]; </li></ul><ul><li>Elements accessed by </li></ul><ul><ul><li>name and [ ] operation b[5] </li></ul></ul>
  10. 10. Character Arrays <ul><li>Elements of an array may be of any type </li></ul><ul><ul><li>Including characters </li></ul></ul><ul><li>Example: char name [NAME_CAPACITY] = &quot;John Doe&quot;; </li></ul><ul><li>If array initialized shorter than specs </li></ul><ul><ul><li>extra locations filled with null character </li></ul></ul>
  11. 11. Subscript Operation <ul><li>We have said elements accessed by name and [ ] numList[5] </li></ul><ul><li>Consider the [ ] to be an operator </li></ul><ul><ul><li>The subscript operator </li></ul></ul><ul><ul><li>Performs address translation </li></ul></ul><ul><li>Name of the array is a pointer constant </li></ul><ul><ul><li>The base address </li></ul></ul>
  12. 12. Declare variables to store and total 3 blood pressures <ul><li>int bp1, bp2, bp3; </li></ul><ul><li>int total; </li></ul>4002 4000 4004 bp2 bp1 bp3 cin >> bp1 >> bp2 >> bp3; total = bp1 + bp2 + bp3;
  13. 13. What if you wanted to store and total 1000 blood pressures? <ul><li>int bp[1000]; </li></ul><ul><li>// Declares an array of 1000 int values </li></ul>bp[0] bp[1] bp[2] . . . . bp[999] 5000 5002 5004 5006 . . . .
  14. 14. One-Dimensional Array Definition <ul><li>An array is a structured collection of components (called array elements), all of the same data type, given a single name, and stored in adjacent memory locations </li></ul><ul><li>The individual components are accessed by using the array name together with an integral valued index in square brackets </li></ul><ul><li>The index indicates the position of the component within the collection </li></ul>
  15. 15. Another Example <ul><li>Declare an array called temps which will hold up to 5 individual float values </li></ul><ul><li>float temps[5]; // Declaration allocates memory </li></ul>temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 number of elements in the array indexes or subscripts Base Address
  16. 16. Declaration of an Array <ul><li>The index is also called the subscript </li></ul><ul><li>In C++, the first array element always has subscript 0, the second array element has subscript 1, etc. </li></ul><ul><li>The base address of an array is its beginning address in memory </li></ul><ul><li>SYNTAX </li></ul><ul><li>DataType ArrayName[ConstIntExpression]; </li></ul>
  17. 17. Yet Another Example <ul><li>Declare an array called name which will hold up to 10 individual char values </li></ul><ul><li>char name[10]; // Declaration allocates memory </li></ul>number of elements in the array name[0] name[1] name[2] name[3] name[4] . . . . . name[9] 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 Base Address
  18. 18. Assigning Values to Individual Array Elements <ul><li>float temps[5]; int m = 4; // Allocates memory </li></ul><ul><li>temps[2] = 98.6; </li></ul><ul><li>temps[3] = 101.2; </li></ul><ul><li>temps[0] = 99.4; </li></ul><ul><li>temps[m] = temps[3] / 2.0; </li></ul><ul><li>temps[1] = temps[3] - 1.2; </li></ul><ul><li>// What value is assigned? </li></ul>temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 99.4 ? 98.6 101.2 50.6
  19. 19. What values are assigned? <ul><li>float temps[5]; // Allocates memory </li></ul><ul><li>int m; </li></ul><ul><li>for (m = 0; m < 5; m++) </li></ul><ul><li>{ </li></ul><ul><li>temps[m] = 100.0 + m * 0.2 ; </li></ul><ul><li>} </li></ul>temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 ? ? ? ? ?
  20. 20. Now what values are printed? <ul><li>float temps[5]; // Allocates memory </li></ul><ul><li>Int m; </li></ul><ul><li>. . . . . </li></ul><ul><li>for (m = 4; m >= 0; m--) </li></ul><ul><li>{ </li></ul><ul><li>cout << temps[m] << endl; </li></ul><ul><li>} </li></ul>temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8
  21. 21. Variable Subscripts <ul><li>float temps[5]; // Allocates memory </li></ul><ul><li>int m = 3; </li></ul><ul><li>. . . . . . </li></ul><ul><li>What is temps[m + 1] ? </li></ul><ul><li>What is temps[m] + 1 ? </li></ul>temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8
  22. 22. A Closer Look at the Compiler <ul><li>float temps[5]; // Allocates memory </li></ul><ul><li>To the compiler, the value of the identifier temps is the base address of the array </li></ul><ul><li>We say temps is a pointer (because its value is an address); it “points” to a memory location </li></ul>temps[0] temps[1] temps[2] temps[3] temps[4] 7000 7004 7008 7012 7016 100.0 100.2 100.4 100.6 100.8
  23. 23. Initializing in a Declaration <ul><li>int ages[5] ={ 40, 13, 20, 19, 36 }; </li></ul><ul><li>for (int m = 0; m < 5; m++) </li></ul><ul><li>{ </li></ul><ul><li>cout << ages[m]; </li></ul><ul><li>} </li></ul>ages[0] ages[1] ages[2] ages[3] ages[4] 6000 6002 6004 6006 6008 40 13 20 19 36
  24. 24. Passing Arrays as Arguments <ul><li>In C++, arrays are always passed by reference </li></ul><ul><li>Whenever an array is passed as an argument, its base address is sent to the called function </li></ul>
  25. 25. In C++, No Aggregate Array Operations <ul><li>The only thing you can do with an entire array as a whole (aggregate) is to pass it as an argument to a function </li></ul><ul><li>Exception: aggregate I/O is permitted for C strings (special kinds of char arrays) </li></ul>
  26. 26. Using Arrays as Arguments to Functions <ul><li>Generally, functions that work with arrays require 2 items of information </li></ul><ul><ul><li>The beginning memory address of the array (base address) </li></ul></ul><ul><ul><li>The number of elements to process in the array </li></ul></ul>
  27. 27. Example with Array Parameters #include <iomanip> #include <iostream> void Obtain (int[], int); // Prototypes here void FindWarmest ( const int[], int , int&); void FindAverage ( const int[], int , int&); void Print ( const int[], int); using namespace std; int main ( ) { // Array to hold up to 31 temperatures int temp[31] int numDays; int average; int hottest; int m;
  28. 28. Example continued cout << “How many daily temperatures? ”; cin >> numDays; Obtain(temp, numDays); // Call passes value of numDays and address temp cout << numDays << “ temperatures“ << endl; Print (temp, numDays); FindAverage (temp, numDays, average); FindWarmest (temp, numDays, hottest); cout << endl << “Average was: “ << average << endl; cout << “Highest was: “ << hottest << endl; return 0; }
  29. 29. Memory Allocated for Array int temp[31]; // Array to hold up to 31 temperatures temp[0] temp[1] temp[2] temp[3] temp[4] . . . . . temp[30] 6000 Base Address 50 65 70 62 68 . . . . . .
  30. 30. void Obtain ( /* out */ int temp[] , /* in */ int number ) // User enters number temperatures at keyboard // Precondition: // number is assigned && number > 0 // Postcondition: // temp[0 . . number -1] are assigned { int m; for (m = 0; m < number; m++) { cout << “Enter a temperature : “; cin >> temp[m]; } }
  31. 31. void Print ( /* in */ const int temp[], /* in */ int number ) // Prints number temperature values to screen // Precondition: // number is assigned && number > 0 // temp[0 . . number -1] are assigned // Postcondition: // temp[0 . . number -1] printed 5 per line { int m; cout << “You entered: “; for (m = 0; m < number; m++) { if (m % 5 == 0) cout << endl; cout << setw(7) << temp[m]; } }
  32. 32. Using Arrays <ul><li>Accessing array for output </li></ul><ul><ul><li>See Fig. 1 </li></ul></ul><ul><li>Accessing array for input from keyboard </li></ul><ul><ul><li>See Fig. 2 </li></ul></ul><ul><li>Note use of arrays as parameters </li></ul><ul><ul><li>Must specify number of elements of array being used </li></ul></ul>
  33. 33. Figure1:Array Output Function <ul><li>void display(int theArray[], int numValues) </li></ul><ul><li>/*----------------------------------------------------------------- </li></ul><ul><li>Display values in an array of integers. Precondition: 0 <= numValues < capacity of theArray. Postcondition: The first numValues integers stored in theArray have been output to cout. </li></ul><ul><li>-------------------------------------------------------------------------*/ </li></ul><ul><li>{ </li></ul><ul><li>for (int i = 0; i < numValues; i++) </li></ul><ul><li>cout « theArray[i] « 00 &quot; . </li></ul><ul><li>cout « endl; </li></ul><ul><li>} </li></ul>
  34. 34. Figure 2: Array Input Function #include <cassert> void read(IntArray theArray, int capacity, int numValues) /*------------------------------------------------------------------------- Input values into an array of integers from the keyboard. Preconditions: 0 <= numValues < capacity, which is the capacity of theArray. Postcondition: numValues integers entered from the keyboard have been stored in the first NumValues positions of theArray -------------------------------------------------------------------------/* { assert (numValues >= 0 && numValues <= capacity); for (int i = 0; i < numValues; i++) cin » theArray[i] ; }
  35. 35. #include <iostream> using namespace std; void main() { int Nums[4]; int Sum=0; cout<<&quot;Enter 4 Numbers :n&quot;; for(int i=0;i<4;i++) cin>>Nums[i]; for(int j=0;j<4;j++) Sum+=Nums[j]; cout<<&quot;Sum = &quot;<<Sum<<endl; }
  36. 36. Multidimensional Arrays <ul><li>Consider multiple pages of the student grade book const int NUM_ROWS = 10, NUM_COLS = 5, NUM_RANKS = 10; typedef double ThreeDimArray[NUM_ROWS][NUM_COLS][NUM_RANKS]; </li></ul>. . .
  37. 37. Example on Multidimensional Arrays <ul><li>#include <iostream> using namespace std; </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>// A 2-Dimensional array </li></ul><ul><li>double distance[2][4] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55, 6234.12}; </li></ul><ul><li>// Scan the array from the 3rd to the 7th member </li></ul><ul><li>cout << &quot;Members of the array&quot;; </li></ul><ul><li>cout << &quot;nDistance [0][0]&quot; << &quot;: &quot; << distance[0][0]; </li></ul><ul><li>cout << &quot;nDistance [0][1]&quot; << &quot;: &quot; << distance[0][1]; </li></ul><ul><li>cout << &quot;nDistance [0][2]&quot; << &quot;: &quot; << distance[0][2]; </li></ul><ul><li>cout << &quot;nDistance [0][3]&quot; << &quot;: &quot; << distance[0][3]; </li></ul><ul><li>cout << &quot;nDistance [1][0]&quot; << &quot;: &quot; << distance[1][0]; </li></ul><ul><li>cout << &quot;nDistance [1][1]&quot; << &quot;: &quot; << distance[1][1]; </li></ul><ul><li>cout << &quot;nDistance [1][2]&quot; << &quot;: &quot; << distance[1][2]; </li></ul><ul><li>cout << &quot;nDistance [1][3]&quot; << &quot;: &quot; << distance[1][3]; </li></ul><ul><li>cout << endl; return 0; </li></ul><ul><li>} </li></ul><ul><li>This would produce: Members of the array </li></ul><ul><li>Distance [0][0]: 44.14 Distance [0][1]: 720.52 </li></ul><ul><li>Distance [0][2]: 96.08 Distance [0][3]: 468.78 </li></ul><ul><li>Distance [1][0]: 6.28 Distance [1][1]: 68.04 </li></ul><ul><li>Distance [1][2]: 364.55 Distance [1][3]: 6234.12 </li></ul>
  38. 38. Array of Array Declarations <ul><li>An array of arrays </li></ul><ul><ul><li>An array whose elements are other arrays </li></ul></ul>
  39. 39. Array of Array Declarations <ul><li>Each of the rows is itself a one dimensional array of values </li></ul>scoresTable[2] is the whole row numbered 2 scoresTable [1][3]
  40. 40. Memory Allocation in 2-Dimensional Arrays <ul><li>Elements stored in rowwise order </li></ul><ul><li>Also called column major order </li></ul>location [0][4] is followed in memory by location [1][0]

×