1 / 56

Data Structures LAB 1

Data Structures LAB 1. TA: Nouf Al- harbi nouf200@hotmail.com. What’s Data Structures ..?. a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently Different kinds of data structures are suited to different kinds of applications.

lam
Download Presentation

Data Structures LAB 1

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. Data StructuresLAB 1 TA: Nouf Al-harbinouf200@hotmail.com

  2. What’s Data Structures ..? • a data structureis a particular way of storing and organizing data in a computer so that it can be used efficiently • Different kinds of data structures are suited to different kinds of applications Introductiom to C++

  3. Data Structures LAB Objectives Understand the purposes and methods of the most commonly occurring data structures Analyze the data structure needs of particular problems Write C++ applications using data structures discussed in the course Introductiom to C++

  4. LAB Marks Introductiom to C++

  5. Lab Materials .. noufcstu.weebly.com Introductiom to C++

  6. Data structures lab 1 Introduction to C++

  7. objectives • Quick Review of some topics in C++ : • Arrays • Structures • Classes Introductiom to C++

  8. Data types in C++ • Note : Not all the data types are included • just some examples Introductiom to C++

  9. Arrays • Suppose we want to compute the average of 20 marks. • Do we need to declare 20 variables • mark1, mark2, …, mark20? • Do we need to write 20 couts and 20 cins? • How about sorting a large number of ints? • This is where ARRAYS come in! Introductiom to C++

  10. Introduction to Arrays • An array is a fundamental data structure • used to store objects of a particular type in contiguous memory locations • Arrays allow us to randomly access this contiguous memory via indexing Introductiom to C++

  11. Array :Declaration typeNamearrayName[sizeOfTheArray] • typeName the type of the elements of the array • Can be any type (primitive or user-defined) • arrayName the name of the array. • Any valid C++ identifier • sizeOfTheArray the size or the capacity of the array Introductiom to C++

  12. Array : Declaration • An array consisting of 5 variables of type int is declared: • It creates 5 int type variables which are accessed as • score[0], score[1] , score[2], score[3], score[4] • These are called indexed variables or subscripted variables • The number in the brackets index or subscript • Array subscripts (0 size of the array-1), which is 4. Introductiom to C++

  13. Array : Initialization • You may initialize specific index variables • this can also be read from cin • You can use for statement to initialized the values of an array Introductiom to C++

  14. Arrays : Initialization • The size of an array can be omitted • The following definition is also possible • Initialization may cover only the first few elements • the first two elements are initialized to 1 and 2 • The others to the default value Introductiom to C++

  15. Referencing and using arrays • Array references may be used anywhere an ordinary variable is used • The forstatement is a good way to go through the elements of an array Introductiom to C++

  16. Array Example • Write a program that • defines an array called StdMarksAvg have 5 elements • Allow the user to enter the 5 Marks • Calculate the Average of marks .. • Then print it out Introductiom to C++

  17. Multidimensional Arrays • Multidimensional arrays can be described as “arrays of arrays” • For example • a bidimensional array can be imagined as a bidimensional table made of elements • all of elements of a same uniform data type Columns Rows Introductiom to C++

  18. Matrix Example Introductiom to C++

  19. Matrix Example Input After first outer loop [0] Input After second outer loop [0] [1] Introductiom to C++

  20. Structure • A Structure is a container, it can hold many things. • These things can be of any type. • Structures are used to organize related data (variables) in to a nice package. Introductiom to C++

  21. Example - Student Record • Student Record: • Name a string • HW Grades an array of 3 doubles • Test Grades an array of 2 doubles • Final Average a double Introductiom to C++

  22. Structure Members • Each thing in a structure is called member. • Each member has a name, a type and a value. • Names follow the rules for variable names. • Types can be any defined type. Introductiom to C++

  23. Example Structure Definition Introductiom to C++

  24. Using a struct • By defining a structure you create a new data type. • Once a struct is defined, you can create variables of the new type. Introductiom to C++

  25. Accessing Members • You can treat the members of a struct just like variables. • You need to use the member access operator '.' : Introductiom to C++

  26. Structure Assignment • You can use structures just like variables: Copies the entire structure Introductiom to C++

  27. Classes : Introduction • Structures provide a way to group data elements • Functions organize program actions into named entities • we’ll put these ideas together to create Classes Introductiom to C++

  28. Classes : What Are Classes? • The functions in the class manipulate the member variables. • A class enables you to encapsulate these variables and functions into one collection (object). class Introductiom to C++

  29. Declaring a Class Introductiom to C++

  30. Classes: Data and Method declaration • Data member declarations are normal variable declarations • int x; is a data member declaration • you cannot initialize data members where you declare them • They must be initialized either in a method or outside the class. • For example, int x = 50; as a data member declaration causes an error. Introductiom to C++

  31. Classes: Data and Method declaration • Method declarations are function declarations placed inside a class • a function declaration can also include the implementation (definition), or you can implement it separately Introductiom to C++

  32. Classes: Data and Method declaration part Data members Modelnumber : int Cost : double methods SetPart(int mn, double c) void ShowPart() Introductiom to C++

  33. Classes : defining object • An object is an individual instance of a class • You define an object of your new type just as you define an integer variable: • This code defines wheel, which is an object whose class (or type) is Part. Introductiom to C++

  34. Classes : Calling Data Member and Member Functions • Once you define an actual Part object -for example, wheel • You use the dot operator (.) to access the members of that object. • to assign 50 to wheel’s modelnumber member variable • to call the ShowPart() function Introductiom to C++

  35. Classes: Assign Values to Objects, Not to Classes • In C++ you don't assign values to types • you assign values to variables • This is a shorthand way of saying, "Assign 50 to the variable x, which is of type int". WRONG Introductiom to C++

  36. Classes: Assign Values to Objects, Not to Classes • In the same way, you wouldn't write • you must define a Part object and assign 50 to the modelnumber of that object. Introductiom to C++

  37. Classes: Using Access Specifiers • C++ allows you to control where the data members of your class can be accessed. • A class has 2 kinds of access specifiers: public and private. • Public data members and methods can be accessed outside the class directly. • The public stuff is the interface. • Private members and methods are for internal use only. • If no access specifier is provided in the class, all members default to private. Introductiom to C++

  38. Classes: Using Access Specifiers example Introductiom to C++

  39. Classes: Using Access Specifiers part part1 Part 2 Part 3 Data members • Modelnumber: int, private • Cost : double, private • Modelnumber=6245 • Cost=220 • Modelnumber=6246 • Cost=185 • Modelnumber=6247 • Cost=150 objects methods • SetPart(int mn, • double c), public • void ShowPart(), public Introductiom to C++

  40. private & public Example Introductiom to C++

  41. private & public Example Introductiom to C++

  42. Special Member Functions • Constructors: called when a new object is created (instantiated) • can be many constructors, each can take different arguments • It is normally used to set initial values for the data members • It has the same name as the class • cannot have a return value (not even void) • Destructor: called when an object is eliminated • only one, has no arguments • It is always named the same name as the class, but with a tilde (~) at the beginning Introductiom to C++

  43. Constructor Example 1 Introductiom to C++

  44. Constructor Example 1 Introductiom to C++

  45. OUTPUT .. Introductiom to C++

  46. Constructor Example 2 Introductiom to C++

  47. Constructor Example 2 Introductiom to C++

  48. OUTPUT .. Introductiom to C++

  49. Constructor Example 3 Introductiom to C++

  50. Constructor Example 3 Introductiom to C++

More Related