600 likes | 902 Views
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.
E N D
Data StructuresLAB 1 TA: Nouf Al-harbinouf200@hotmail.com
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++
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++
LAB Marks Introductiom to C++
Lab Materials .. noufcstu.weebly.com Introductiom to C++
Data structures lab 1 Introduction to C++
objectives • Quick Review of some topics in C++ : • Arrays • Structures • Classes Introductiom to C++
Data types in C++ • Note : Not all the data types are included • just some examples Introductiom to C++
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++
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++
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++
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++
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++
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++
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++
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++
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++
Matrix Example Introductiom to C++
Matrix Example Input After first outer loop [0] Input After second outer loop [0] [1] Introductiom to C++
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++
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++
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++
Example Structure Definition Introductiom to C++
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++
Accessing Members • You can treat the members of a struct just like variables. • You need to use the member access operator '.' : Introductiom to C++
Structure Assignment • You can use structures just like variables: Copies the entire structure Introductiom to C++
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++
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++
Declaring a Class Introductiom to C++
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++
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++
Classes: Data and Method declaration part Data members Modelnumber : int Cost : double methods SetPart(int mn, double c) void ShowPart() Introductiom to C++
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++
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++
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++
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++
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++
Classes: Using Access Specifiers example Introductiom to C++
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++
private & public Example Introductiom to C++
private & public Example Introductiom to C++
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++
Constructor Example 1 Introductiom to C++
Constructor Example 1 Introductiom to C++
OUTPUT .. Introductiom to C++
Constructor Example 2 Introductiom to C++
Constructor Example 2 Introductiom to C++
OUTPUT .. Introductiom to C++
Constructor Example 3 Introductiom to C++
Constructor Example 3 Introductiom to C++