Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Introduction to Programming Lecture 39. Copy Constructor.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
EEM 480 Algorithms and Complexity by Assist. Prof. Dr. Emin Germen.
Chapter 10.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
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.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
 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.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Summary of lectures (1 to 11)
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
GE 211 Programming in C Matrix Dr. Ahmed Telba. Example Write function to take coefficients of quadratic equation a, b and c as input parameter and return.
C++ fundamentals.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Review of C++ Programming Part II Sheng-Fang Huang.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Java and C++, The Difference An introduction Unit - 00.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
 2006 Pearson Education, Inc. All rights reserved Arrays.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Chapter -6 Polymorphism
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Object-Oriented Programming (OOP) and C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Test 2 Review Outline.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
7 Arrays.
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
7 Arrays.
Introduction to Classes and Objects
Arrays Arrays A few types Structures of related data items
CS410 – Software Engineering Lecture #5: C++ Basics III
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)

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

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

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

Pointers Mastering the use of pointers is essential when programming in C++. The key to understanding pointers is to : 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.

Classes and Objects A C++ class encapsulates a set of 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.

Inheritance In C++ one class may be derived from another. The derived class inherits In C++ one class may be derived from another. The derived class inherits  all the member variables and the member functions of the base class or classes.  In addition, inherited member functions can be overridden in the derived class and new member variables and functions can be defined.  You should understand how the compiler determines the code to execute when a particular member function is called.

Inheritance In C++ one class may be derived from another. In C++ one class may be derived from another.  The derived class inherits all the member variables and the member functions of the base class or classes.  In addition, inherited member functions can be overridden in the derived class and new member variables and functions can be defined.  You should understand how the compiler determines the code to execute when a particular member function is called.

Other Features features such as features such as  templates,  exceptions  run-time type information  and more as we learn further

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

In Today’s lecture How to Input arrays How to Input arrays How to process arrays How to process arrays How to insert an item in an array How to insert an item in an array How to pass an array How to pass an array Structures Structures Their basic implementation Their basic implementation

The first Data Structure An Array! An Array! The simplest form of an Array is a one dimensional array that may be defined as a finite ordered set of homogenous elements For Example For Example int a[100];

Basic Operations Extraction Extraction  A function that accepts an array “a” and an index “i”, and returns an element of the array.  Example a[i] Storing Storing  It accepts array “a” an index “i” and an element x.  Example  a[i]=x

One Dimensional Array range = upper - lower+1 range = upper - lower+1 Neither the upper bound nor the lower bound can be changed and as well as the range can be changed during the program execution. Neither the upper bound nor the lower bound can be changed and as well as the range can be changed during the program execution. Lower Bound [0] Upper Bound Range

Implementation of 1Dimenional Array int b[100]; int b[100]; Reserves 100 successive locations, each large enough to contain a single integer. Reserves 100 successive locations, each large enough to contain a single integer. The address of the first of these locations is called the base Address: base(b) The address of the first of these locations is called the base Address: base(b) Reference to element b[0] is to the element at location base(b) Reference to element b[0] is to the element at location base(b) Reference to b[1] is to the element at Reference to b[1] is to the element at base(b) + 1* esize Hence b gives you the starting memory address of the array b b gives you the starting memory address of the array b

Memory view of an array a[0] a[1] a[2] a[3] a[4] int a[5] //Help me give output of this program void main(void) { int a[5] = { 2,3,4,7,8 }; cout << a[3] << endl; cout << a <<endl; cout << *(a+1) <<endl; cout << *a+1 <<endl; } 0x4 0x8 0xC 0x10 0x14

*a +1 without brackets leads to adding 1 to contents (value) of a[0]

Array of Variable Length A[0] A[1] A[2] A[3] 5HELLO 6BICSE4

Array of Variable length cont. A[0] A[1] A[2] A[3] HELLO\0 BICSE2\0 COTTON\0 pen\0 char *A[4]

Character arrays (Strings) Review ALI\0 A L I \0 Garbage A[0] A[1] A[2] A[3] A[4]

How to determine length of the string #include #include int len_str(char str[25]); int len_str_while(char s[25]); void main(void) { char l[25]; char l[25]; cin >> l; cout << len_str(l) << endl << len_str_while(l); } //function with for loop int len_str(char s[25]) { for (int i = 0; s[i] != '\0'; i++); return i; }

Cont… //another method using while loop int len_str_while(char s[25]) { int i=0; while (s[i] != '\0') {i++;} return i; }

Two dimensional Arrays

#include #include void mult_matrices(int a[][2], int b[][2], int result[][2]); void print_matrix(int a[][2]); void main(void) { int p[2][2] = { {10, 20}, {30,40 } }; int p[2][2] = { {10, 20}, {30,40 } }; int q[2][2] = { {50, 60}, {70, 80} }; int q[2][2] = { {50, 60}, {70, 80} }; int r[2][2]; int r[2][2]; print_matrix(p); print_matrix(p); print_matrix(q); print_matrix(q); mult_matrices(p, q, r); mult_matrices(p, q, r); print_matrix(r); print_matrix(r);} Main Program Why New Array?

Print a Matrix void print_matrix(int a[][2]) { int i, j; int i, j; for (i=0; i<2; i++) for (i=0; i<2; i++) { for (j=0; j<2; j++) for (j=0; j<2; j++) { cout << "\t" << a[i][j]; cout << "\t" << a[i][j]; } cout << endl; cout << endl; } } Why mention 2?

Multiply a Matrix void mult_matrices(int a[][2], int b[][2], int result[][2]) { int i, j, k; int i, j, k; for(i=0; i<2; i++) for(i=0; i<2; i++) { for(j=0; j<2; j++) for(j=0; j<2; j++) { result[i][j] = 0; result[i][j] = 0; for(k=0; k<2; k++) for(k=0; k<2; k++) { result[i][j] = result[i][j] + (a[i][k] * b[k][j]); result[i][j] = result[i][j] + (a[i][k] * b[k][j]); } } }} Why passed so many variables? Are these Variables Called by reference or By value?

Submission Date 13th March, th March, 2008 Before 1500 Hrs Before 1500 Hrs Late submission rules apply Late submission rules apply