SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Data Structures and Abstract Data Types Chapter 2
Chapter Contents ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Data Structures, Abstract Data Types, and Implementations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Data Structures, Abstract Data Types, and Implementations ,[object Object],[object Object],[object Object],[object Object],[object Object]
C++  Data Types structured array   struct   union  class simple integral  enum char   short  int  long  bool address pointer  reference floating float  double  long double
Structured Data Type  ,[object Object],[object Object],[object Object]
Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Single Dimension Arrays ,[object Object],[object Object],[object Object],[object Object]
Character Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object]
Subscript Operation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declare variables to  store and total 3 blood pressures   ,[object Object],[object Object],4002 4000 4004 bp2 bp1 bp3 cin >> bp1 >>  bp2 >>  bp3; total =  bp1 +  bp2 +  bp3;
What if you wanted to store and total 1000 blood pressures? ,[object Object],[object Object],bp[0]  bp[1]  bp[2]  . . . .  bp[999] 5000  5002  5004  5006     .  .  .  .
One-Dimensional Array Definition ,[object Object],[object Object],[object Object]
Another Example ,[object Object],[object Object],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
Declaration of an Array ,[object Object],[object Object],[object Object],[object Object],[object Object]
Yet Another Example ,[object Object],[object Object],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
Assigning Values to  Individual Array Elements ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],temps[0]  temps[1]  temps[2]  temps[3]  temps[4] 7000  7004  7008  7012  7016   99.4  ?  98.6  101.2  50.6
What values are assigned? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],temps[0]  temps[1]  temps[2]  temps[3]  temps[4] 7000  7004  7008  7012  7016   ?  ?  ?  ?  ?
Now what values are printed? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],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
Variable Subscripts ,[object Object],[object Object],[object Object],[object Object],[object Object],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
A Closer Look at the Compiler ,[object Object],[object Object],[object Object],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
Initializing in a Declaration ,[object Object],[object Object],[object Object],[object Object],[object Object],ages[0]  ages[1]  ages[2]  ages[3]  ages[4] 6000  6002  6004  6006  6008 40  13  20  19  36
Passing Arrays as Arguments ,[object Object],[object Object]
In C++,  No Aggregate Array Operations ,[object Object],[object Object]
Using Arrays as  Arguments to Functions  ,[object Object],[object Object],[object Object]
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;
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; }
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  . . . . . .
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]; } }
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]; } }
Using Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Figure1:Array Output Function  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
  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] ;  }
#include <iostream> using namespace std; void main() { int Nums[4]; int Sum=0; cout<<&quot;Enter 4 Numbers :&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; }
Multidimensional Arrays ,[object Object],. . .
Example on Multidimensional Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Array of Array Declarations ,[object Object],[object Object]
Array of Array Declarations ,[object Object],scoresTable[2] is the whole row numbered 2 scoresTable [1][3]
Memory Allocation in  2-Dimensional Arrays ,[object Object],[object Object],location [0][4] is followed in memory by location [1][0]

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (18)

Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Unit 2
Unit 2Unit 2
Unit 2
 
Computer Programming- Lecture 4
Computer Programming- Lecture 4Computer Programming- Lecture 4
Computer Programming- Lecture 4
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
Arrays
ArraysArrays
Arrays
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
Python programing
Python programingPython programing
Python programing
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
Python
PythonPython
Python
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
See through C
See through CSee through C
See through C
 

Ähnlich wie Lec2&3_DataStructure

SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.pptFareedIhsas
 
Data structure array
Data structure  arrayData structure  array
Data structure arrayMajidHamidAli
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it outrajatryadav22
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptxNelyJay
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyworldchannel
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxkrishna50blogging
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionSvetlin Nakov
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfRahul04August
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersANUSUYA S
 
Array and Pointers
Array and PointersArray and Pointers
Array and PointersProf Ansari
 

Ähnlich wie Lec2&3_DataStructure (20)

Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Unit 3
Unit 3 Unit 3
Unit 3
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptx
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Java part 2
Java part  2Java part  2
Java part 2
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
 

Mehr von Ibrahim El-Torbany (14)

Idea2
Idea2Idea2
Idea2
 
Cpp lernaufgabe linked_list
Cpp lernaufgabe linked_listCpp lernaufgabe linked_list
Cpp lernaufgabe linked_list
 
C++ examples &revisions
C++ examples &revisionsC++ examples &revisions
C++ examples &revisions
 
Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
Lec5
Lec5Lec5
Lec5
 
Lec3
Lec3Lec3
Lec3
 
Lec1
Lec1Lec1
Lec1
 
Lec4
Lec4Lec4
Lec4
 
Ass logic
Ass logicAss logic
Ass logic
 
Math lecture 4 Part 1
Math lecture 4 Part 1Math lecture 4 Part 1
Math lecture 4 Part 1
 
Tutorial 1
Tutorial 1Tutorial 1
Tutorial 1
 
Lecture 2 math 2
Lecture 2 math 2Lecture 2 math 2
Lecture 2 math 2
 
Lec1
Lec1Lec1
Lec1
 
Chapter 1 what is statistics
Chapter 1 what is statisticsChapter 1 what is statistics
Chapter 1 what is statistics
 

Lec2&3_DataStructure

  • 1. Data Structures and Abstract Data Types Chapter 2
  • 2.
  • 3.
  • 4.
  • 5.
  • 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.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 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. 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. 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. 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. 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.
  • 33.
  • 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. #include <iostream> using namespace std; void main() { int Nums[4]; int Sum=0; cout<<&quot;Enter 4 Numbers :&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.
  • 37.
  • 38.
  • 39.
  • 40.