Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA STRUCTURES LAB 1 TA: Nouf Al-harbi

Similar presentations


Presentation on theme: "DATA STRUCTURES LAB 1 TA: Nouf Al-harbi"— Presentation transcript:

1 DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

2 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 2 Introductiom to C++

3  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 Data Structures LAB Objectives 3 Introductiom to C++

4 LAB Marks 4 Introductiom to C++

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

6 DATA STRUCTURES LAB 1 Introduction to C++

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

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

9 Arrays Introductiom to C++ 9  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 cout s and 20 cins ?  How about sorting a large number of ints?  This is where ARRAYS come in!

10 Introduction to Arrays Introductiom to C++ 10  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

11 Array :D eclaration Introductiom to C++ 11 typeName arrayName[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

12 Array : Declaration Introductiom to C++ 12  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. int score[5];

13 Array : Initialization Introductiom to C++ 13  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 score[4] = 10; for (int i=0;i<5;i++) score[i]=i*i+1;

14 Arrays : Initialization Introductiom to C++ 14  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 int score[]={2, 1, 5, 4, 6}; int score[5]={2, 1, 5, 4, 6}; int marks[10]={1,2};

15 Introductiom to C++ 15 Referencing and using arrays  Array references may be used anywhere an ordinary variable is used  The for statement is a good way to go through the elements of an array cin >> score[4] >> score[2]; cout << score[2] << “, “ << score[4]; score[0] = 32; score[3] = score[0]++; 12341234

16 Array Example Introductiom to C++ 16  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 #include int main() { int StdMarksAvg[5],i=0; double sum=0,avg=0; cout<<"Enter the Students Marks"<<endl; for (i=0;i<5;i++) { cout<<"the mark of sudent "<<i+1<<": "; cin>>StdMarksAvg[i]; sum+=StdMarksAvg[i]; } avg=sum/5; cout<<"The Average of Marks="<<avg<<endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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 Rows Columns 17 Introductiom to C++

18 Matrix Example Introductiom to C++ 18 int maxRows = 2; int maxCols = 3 ; int matrix [ 2] [ 3 ]; int row, col ; for ( row = 0 ; row < maxRows ; row ++ ) { for ( col = 0 ; col < maxCols ; col ++ ) { cout << “Please enter value of ”<< row << “, “ << col; cin >> matrix [ row ] [ col ] ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14

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

20 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.

21 Introductiom to C++ 21 Example - Student Record  Student Record:  Name a string  HW Gradesan array of 3 doubles  Test Gradesan array of 2 doubles  Final Average a double

22 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.

23 Example Structure Definition Introductiom to C++ 23 struct StudentRecord { string name;// student name double hw[3];// homework grades double test[2];// test grades double ave;// final average }; 12345671234567

24 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. StudentRecord stu;

25 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 '.' : cout << stu.name << endl; stu.hw[2] = 82.3; stu.ave = total/100; 123123

26 Introductiom to C++ 26 Structure Assignment  You can use structures just like variables: Copies the entire structure StudentRecord s1,s2; s1.name = “Lama"; … s2 = s1; 12341234

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

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

29 Declaring a Class 29 Introductiom to C++ class ClassName { memberList }; 12341234

30 Classes: Data and Method declaration 30  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 31  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 Introductiom to C++ 32 class Part { int modelnumber; double cost; void SetPart(int mn, double c); void ShowPart(); }; 1234567812345678 Modelnumber : int Cost : double SetPart(int mn, double c) void ShowPart() part Data members methods

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. 33 Introductiom to C++ Part wheel;

34 Classes : Calling Data Member and Member Functions 34  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++ wheel.modelnumber = 50; wheel.ShowPart();

35 Classes: Assign Values to Objects, Not to Classes 35  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". Introductiom to C++ int x; //define x to be an int x = 50; //set x's value to 50 int = 50;WRONG

36 Classes: Assign Values to Objects, Not to Classes 36  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++ Part.modelnumber = 50; //wrong ??? Part wheel; //just like int x; wheel.modelnumber = 50; //just like x = 50;

37 Classes: Using Access Specifiers 37  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 38 Introductiom to C++ class ClassName { int x; public: int y; int z; private: int a; }; 123456789123456789

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

40 private & public Example 40 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

41 private & public Example 41 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11

42 Special Member Functions Introductiom to C++ 42  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

43 Constructor Example 1 43 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

44 Constructor Example 1 44 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

45 OUTPUT.. 45 Introductiom to C++

46 Constructor Example 2 46 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

47 Constructor Example 2 47 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

48 OUTPUT.. 48 Introductiom to C++

49 Constructor Example 3 49 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

50 Constructor Example 3 50 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

51 OUTPUT.. 51 Introductiom to C++

52 Separating Classes into Files 52  Classes often get pretty big  having all your code in one file can quickly become unmanageable.  if you want to reuse your classes in other programs, you have to copy and paste them into the new program.  there is a convention for separating classes into files.  the class declaration is placed in one file (header file)  ClassName.h  the implementation of all the methods is put in another file  ClassName.cpp  include the header file in your program with an #include directive Introductiom to C++

53 example 53 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12

54 example 54 Introductiom to C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

55 1 2 3 4 5 6 7 8 9 10 11 12 example 55 Introductiom to C++

56 OUTPUT.. 56 Introductiom to C++


Download ppt "DATA STRUCTURES LAB 1 TA: Nouf Al-harbi"

Similar presentations


Ads by Google