Data Structures - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Data Structures

Description:

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

Number of Views:148
Avg rating:3.0/5.0
Slides: 28
Provided by: Sana91
Category:

less

Transcript and Presenter's Notes

Title: Data Structures


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

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

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

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

5
Pointers
  • 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.

6
Classes and Objects
  • 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.

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

8
Inheritance
  • 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.

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

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

11
In Todays lecture
  • How to Input arrays
  • How to process arrays
  • How to insert an item in an array
  • How to pass an array
  • Structures
  • Their basic implementation

12
The first Data Structure
  • 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
  • int a100

13
Basic Operations
  • Extraction
  • A function that accepts an array a and an index
    i, and returns an element of the array.
  • Example
  • ai
  • Storing
  • It accepts array a an index i and an element
    x.
  • Example
  • aix

14
One Dimensional Array
  • range upper - lower1
  • 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
Range
Upper Bound
15
Implementation of 1Dimenional Array
  • int b100
  • 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)
  • Reference to element b0 is to the element at
    location base(b)
  • Reference to b1 is to the element at
  • base(b) 1 esize
  • Hence
  • b gives you the starting memory address of the
    array b

16
Memory view of an array
2 3 4 7 8
int a5
//Help me give output of this program void
main(void) int a5 2,3,4,7,8 cout
ltlt a3 ltlt endl cout ltlt a ltltendl cout ltlt
(a1) ltltendl cout ltlt a1 ltltendl

2
3
4
7
8



a0
a1
a2
a3
a4



0x4
0x8
0xC
0x10
0x14


17
  • a 1 without brackets leads to adding 1 to
    contents (value) of a0

18
Array of Variable Length


A0
A1
A2
A3



5 H E L L O
6 B I C S E 4
19
Array of Variable length cont.
char A4

A0
A1
A2
A3



H E L L O \0
B I C S E 2 \0
C O T T O N \0
p e n \0
20
Character arrays (Strings)Review

A
L
I
\0
Garbage



A0
A1
A2
A3
A4


A L I \0
21
How to determine length of the string
  • include ltiostream.hgt
  • include ltconio.hgt
  • include ltstdio.hgt
  • int len_str(char str25)
  • int len_str_while(char s25)
  • void main(void)
  • char l25
  • cin gtgt l
  • cout ltlt len_str(l) ltlt endl ltlt len_str_while(l)
  • //function with for loop
  • int len_str(char s25)
  • for (int i 0 si ! '\0' i)
  • return i

22
Cont
  • //another method using while loop
  • int len_str_while(char s25)
  • int i0
  • while (si ! '\0')
  • i
  • return i

23
Two dimensional Arrays
Do it yourself
24
Main Program
  • include ltiostream.hgt
  • include ltconio.hgt
  • void mult_matrices(int a2, int b2, int
    result2)
  • void print_matrix(int a2)
  • void main(void)
  • int p22 10, 20, 30,40
  • int q22 50, 60, 70, 80
  • int r22
  • print_matrix(p)
  • print_matrix(q)
  • mult_matrices(p, q, r)
  • print_matrix(r)

Why New Array?
25
Print a Matrix
  • void print_matrix(int a2)
  • int i, j
  • for (i0 ilt2 i)
  • for (j0 jlt2 j)
  • cout ltlt "\t" ltlt aij
  • cout ltlt endl
  • cout ltlt endl

Why mention 2?
26
Multiply a Matrix
  • void mult_matrices(int a2, int b2, int
    result2)
  • int i, j, k
  • for(i0 ilt2 i)
  • for(j0 jlt2 j)
  • resultij 0
  • for(k0 klt2 k)
  • resultij resultij (aik
    bkj)

Why passed so many variables? Are
these Variables Called by reference or By value?
27
Submission Date
  • 13th March, 2008
  • Before 1500 Hrs
  • Late submission rules apply
Write a Comment
User Comments (0)
About PowerShow.com