Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an.

Similar presentations


Presentation on theme: "Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an."— Presentation transcript:

1 Data Structures

2 Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an organization it can be – Employee’s name, sex, age, department, address so on In Railway reservation office – Passenger name, traveling date, traveling time, seat number so on In Computers – Set of integers, Structure(s) or any other user defined data type

3 Abstract Data Types (ADT) This is a higher level, domain and operations independent specification of data – While working on an “integer” type we don’t worry about its low level implementation (can be 32 bit, or 64 bit number) – Distance = rate * time (rate and time can be of type integer or real numbers) – Employee record means same to the bosses of two different organizations, no matter how differently they are stored by the admin officers

4 Data Structures Collection of data in an ordered fashion – Ages of the students in a class are all numbers (data), but once are grouped in one line, one after the other becomes structured data (arrays) – Age, class, sex, height, weight of a student is all data, but if we place them in one line per student it would be structured data (structures/struct) – Arrays, list, queues, stack, doubly linked lists, structures etc are all data structures Different operations are defined for each type of data structure to add, delete or modify its content

5 Algorithms The operations defined on the data structures are generally knows as algorithms For each data structure there has to be defined algorithms Algorithms are normally used to add, delete, modify, sort items of a data structure All programming languages are generally equipped with conventional data structures and algorithms. New data structures and the algorithms for manipulation can be defined by the user

6 Why Study? Designed to develop students understanding the impact of structuring data to achieve efficiency of a solution to a problem After completion you will be familiar with important and most often used data structuring techniques. It will enable you to understand the manner in which data is organized and presented later.

7 Objectives of the course Present in a systematic fashion the most commonly used data structures, emphasizing their abstract properties. Discuss typical algorithms that operate each kind of data structure, and analyze their performance. Compare different Data Structures for solving the same problem, and choose the best

8 Abstract Data Type ADT A data type whose properties (domain and operations) are specified independently of any particular implementation ADT is a mathematical model which gives the a set of utilities available to the user but never states the details of its implementation. In OO-Programming a Class is an ADT

9 Data Structures: Definition A collection of data elements whose organization is characterized by accessing operations that are used to store and retrieve the individual data elements; The logical arrangement of data as used by a system for data management; a representation of a data model in computer form

10 Data Structures: More specifically – A data structure is a way of grouping fundamental types (like integers, floating point numbers, and arrays) into a bundle that represents some identifiable thing. For example, a matrix may be thought of as the bundle of the number of rows and columns, and the array of values of the elements of the matrix. This information must be known in order to manipulate the matrix. – C introduced the struct for declaring and manipulating data structures. C++ extended the struct to a class

11 Goals of Data structures Identify and develop useful mathematical entities and operations and to determine what classes of problems can be solved by using these entities and operations. Determine representation of those abstract entities and to implement the abstract operation on these concrete representations.

12 A Real Life Example Lisa Michele John 110 622-9823 112-4433 75 Bronson Paola Electronic Phone Book Contains different DATA: - names - phone number - addresses Need to perform certain OPERATIONS: - add - delete - look for a phone number - look for an address How to organize the data so to optimize the efficiency of the operations

13 Features of c++ you need to know Variables Parameter Passing Pointers Classes and Objects Inheritance Others

14 Variables You must be very comfortable with the notion of a variable as an abstraction for a region of a memory. A variable has attributes such as name, type, value, address size, lifetime and scope.

15 Parameter Passing There are two parameter passing mechanisms in C++: pass-by-value and pass-by-reference. It is essential that you understand the behavioral difference between the two methods as well as the performance implications of using each of them.

16 Pointers Mastering the use of pointers is essential when programming in C++. The key to understanding pointers is to : – recognize that a pointer variable has exactly the same set of attributes as any other C++ variable. – It is crucial that you keep straight the distinctions between the : value of a pointer, the address of a pointer the object to which a pointer points.

17 Classes and Objects A C++ class encapsulates a set of – Values The values are represented by the member variables of the class – operations. The operations by the member functions of the class. – In C++ a class definition introduces a new type. – The instances of a class type are called objects. – Special role of the constructor and the destructor member functions of a class and when the C++ compiler invokes each of them.

18 © 2000 Scott S Albert Variables Used to store data during program execution – Results of calculations – Temporary values – Control information Can hold many different types of data – Numbers, characters, strings of characters and objects ( collections of data and programs )

19 © 2000 Scott S Albert What is a program variable? A named location to store data – a container for data It can hold only one type of data – for example only integers, only floating point (real) numbers, or only characters

20 © 2000 Scott S Albert Creating variables All program variables must be declared before using them A variable declaration associates a name with a storage location in memory and specifies the type of data it will store: Type Variable_1, Variable_2, …; For example, to create three integer variables to store the number of baskets, number of eggs per basket, and total number of eggs: int numberOfBaskets, eggsPerBasket, totalEggs;

21 © 2000 Scott S Albert Variables Variables store data in a program Variables must be declared (or defined) before they are used – Variable declarations Variable type Variable name (identifier) – Example int nPosition, nHeight, nWidth; char cAnswer;

22 © 2000 Scott S Albert Variable Names Naming rules – Identifiers must start with a letter, may contain letters or numbers or _ – There is essentially no limit to the length of an identifier Variable name should match its use – Use dName rather than a – Use nHeight rather than x

23 © 2000 Scott S Albert Variable Types Primitive types – Basic building blocks of the C++ language. Match intrinsic hardware capabilities. Class types – Contain: One or more primitive types Plus associated C++ statements to manage the primitive types Possibly even other class types

24 © 2000 Scott S Albert Primitive Types shortinteger1 byte intinteger2 bytes unsignedinteger4 bytes longinteger8 bytes floatreal4 bytes doublereal8 bytes charcharacter2 bytes boollogical1 bit/1 byte Use the sizeof() operator for a particular system

25 © 2000 Scott S Albert Primitive Data Types

26 © 2000 Scott S Albert Primitive Types Integer – Signed whole numbers only (no decimal) Real (Scientific Notation) – Signed numbers with two portions An exponent A mantissa with a fixed precision Char – ASCII character

27 © 2000 Scott S Albert Integer vs Real Numbers Why two different systems? – Clearly the range of integers fit into the range of real numbers Integers are computationally very efficient – If you can use integers, do so your program will run much faster Integers are precise – By their nature, real numbers often have very small errors that creep into their representation. Integers do not have this issue.

28 © 2000 Scott S Albert Variable names: Identifiers Rules - these must be obeyed all C++ identifiers must follow the same rules must not start with a digit must contain only numbers, letters, underscore (_) and $ (but avoid using $, it is reserved for special purposes) names are case-sensitive (ThisName and thisName are two different variable names) Good Programming Practice - these should be obeyed always use meaningful names from the problem domain (for example, eggsPerBasket instead of n, which is meaningless, or count, which is not meaningful enough) start variable names with lower case capitalize interior words (use eggsPerBasket instead of eggsperbasket ) avoid using $ since it is reserved for special purposes

29 © 2000 Scott S Albert Two main kinds of types in C++ primitive data types the simplest types cannot decompose into other types values only, no methods Examples: int - integer double - floating point (real) char - character class types more complex composed of other types (primitive or class types) both data and methods

30 © 2000 Scott S Albert Syntax Templates Efficient method to communicate the grammar of a computer language – Similar to a sentence diagram – Indicates the required and optional portions of a statement.

31 © 2000 Scott S Albert Assignment Statements Syntax template Variable = Expression ; Operation – The expression on the right hand side is evaluated and assigned to the memory location named by the variable on the left hand side.

32 © 2000 Scott S Albert Expressions Simple expression – Variable Arithmetic expressions – Binary operators (require two values) + - * / % – Unary operators (only one value needed) + - ++ --

33 © 2000 Scott S Albert Expression Examples int nPosition, nHeight, nWidth; nPosition = 0; nHeight = 5; nWidth = nHeight; nHeight = nHeight * 2; nPosition = nHeight + nWidth * 3 + 5; float nXPos, nYPos; nXPos = 2.3; nYPos = nXPos / 0.3 ; nHeight = -nWidth; nHeight = nWidth * -nPosition;

34 © 2000 Scott S Albert Additional Arithmetic Operators Increment and Decrement – ++ will increment a variable by 1 – -- will decrement a variable by 1 – A unary operator that can be prefix or postfix Modulus operator % – Integer division yields an integer result – % Returns the remainder of an integer division

35 © 2000 Scott S Albert Samples int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = nSomeNumber / 3; nRemainder = nSomeNumber % 3; float nAnswer; nAnswer = nSomeNumber / 3.0; nSomeNumber++; nQuotient = 7; nSomeNumber = nQuotient ++; nSomeNumber = ++nQuotient ;

36 © 2000 Scott S Albert Samples void main() { cout<<"Hello World”<<endl; int nSomeNumber, nQuotient, nRemainder; nSomeNumber = 17; nQuotient = 7; nSomeNumber = nQuotient++; cout<<"nQuotient postfix is " << nQuotient<<endl; cout<<"nSomeNumber postfix is " << nSomeNumber<<endl; nSomeNumber = ++nQuotient ; cout<<"nQuotient prefix is " +<<nQuotient<<endl; cout<<"nSomeNumber prefix is " << nSomeNumber<<endl; }

37 © 2000 Scott S Albert Examples to run int nCount = 0; int nValue = 0; nValue = nCount++; cout<<"NCount is"+nCount+"nValue is ”<< nValue<<endl; nValue = ++nCount; cout<<"NCount is "+nCount+" nValue is ”<< nValue<<endl; nCount++; ++nCount; nValue = nValue + 1; nCount = nValue--; nCount = nValue - 1;

38 © 2000 Scott S Albert Prefix and Postfix A prefix ++ or -- is evaluated (executed) first and the new value is available in the current expression A postfix ++ or -- is evaluated (executed) after the expression has been evaluated therefore the new value IS NOT available in the current expression

39 © 2000 Scott S Albert Precedence Rules How is the following expression evaluated? – nPosition = nHeigth / nWidth + 5 * 3; Why? Precedence rules (introduction) – First unary operators +, -, ++, -- – Next binary operators *, /, % – Then binary operators +, - – Unless there are parenthesis ( )

40 © 2000 Scott S Albert Precedence Exercise int nCount = 0; int nValue = 0; nValue = 1 + 2 – 3 – 4; nValue = 16 * 4 / 2 * 3; nValue = 7 – 3 * 4 + 2; nValue = 6 * -nCount + 5; nValue = (6 * (-nCount)) + 5;

41 Conversion between types Implicit conversion – C++ will automatically convert some types. This conversion is often call promotion since the conversion always moves to a more expansive type. – short  int  long  float  double – Any type can be promoted to a type on its right byte can be promoted to long float can not be promoted to short int foo; double foobar; foobar = 2.5; foo = foobar; cout<<"foobar is " << foobar;

42 Conversion between types Explicit conversion C++ allows the programmer to explicitly specify a conversion though a cast – This allows a conversion that would not be done through promotion, but should be used with caution. Values are truncated (not rounded) without warning. Special issues when casting characters to numbers

43 Console I/O Input from the keyboard, output to a “console” window. – Not Windows programming per se – Rather a rudimentary input and output mechanism useful for simple programs where we don’t want to spend a great deal of time on the user interface No mouse, graphics, multiple windows etc.

44 Output Methods Cout<< – Outputs to the console window Use endl or \n for a new line Input Methods Cin>>

45 Comments Two types of comments – // -- everything after a // is ignored by the compiler to the end of the line – /* -- everything after a /* is ignored by the compiler until a */ is reached

46 Named Constants Named constants are of type – const Normally there names are all upper case Examples const int MAXNUMBER = 100; const float MINTEMP = -40.0;


Download ppt "Data Structures. Introduction to Data Structures What is Data ? – Any useful information – that can be stored or memorized for future reference In an."

Similar presentations


Ads by Google