DATA STRUCTURES LAB 1 TA: Nouf Al-harbi

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Chapter 14: Overloading and Templates
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
C++ for Engineers and Scientists Third Edition
Arrays.
1 C++ Structures Starting to think about objects...
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Arrays.
1 CS161 Introduction to Computer Science Topic #15.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays Chapter 7.
Procedural and Object-Oriented Programming
Structures in C++.
7 Arrays.
Starting to think about objects...
7 Arrays.
Starting to think about objects...
Arrays Arrays A few types Structures of related data items
Presentation transcript:

DATA STRUCTURES LAB 1 TA: Nouf Al-harbi

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++

 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++

LAB Marks 4 Introductiom to C++

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

DATA STRUCTURES LAB 1 Introduction to C++

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

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

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!

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

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

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];

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;

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};

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]++;

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; }

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++

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 ] ; }

Matrix Example After first outer loop After second outer loop Input [0] [1] [0] 19 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 Gradesan array of 3 doubles  Test Gradesan 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.

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 };

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;

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;

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;

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

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++

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

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++

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++

Classes: Data and Method declaration Introductiom to C++ 32 class Part { int modelnumber; double cost; void SetPart(int mn, double c); void ShowPart(); }; Modelnumber : int Cost : double SetPart(int mn, double c) void ShowPart() part Data members methods

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;

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();

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

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;

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++

Classes: Using Access Specifiers example 38 Introductiom to C++ class ClassName { int x; public: int y; int z; private: int a; };

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

private & public Example 40 Introductiom to C

private & public Example 41 Introductiom to C

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

Constructor Example 1 43 Introductiom to C

Constructor Example 1 44 Introductiom to C

OUTPUT.. 45 Introductiom to C++

Constructor Example 2 46 Introductiom to C

Constructor Example 2 47 Introductiom to C

OUTPUT.. 48 Introductiom to C++

Constructor Example 3 49 Introductiom to C

Constructor Example 3 50 Introductiom to C

OUTPUT.. 51 Introductiom to C++

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++

example 53 Introductiom to C

example 54 Introductiom to C

example 55 Introductiom to C++

OUTPUT.. 56 Introductiom to C++