0% found this document useful (0 votes)
1 views

CS-206 Data Structures and Algorithms Lecture 4

The document discusses data structures and algorithms, focusing on the organization and implementation of data, particularly in the context of managing airplane seat assignments. It covers static and dynamic arrays, their operations, and the differences between abstract data types and data structures. Additionally, it addresses memory allocation, potential issues with arrays, and the importance of dynamic memory management in programming.

Uploaded by

codingworld5879
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

CS-206 Data Structures and Algorithms Lecture 4

The document discusses data structures and algorithms, focusing on the organization and implementation of data, particularly in the context of managing airplane seat assignments. It covers static and dynamic arrays, their operations, and the differences between abstract data types and data structures. Additionally, it addresses memory allocation, potential issues with arrays, and the importance of dynamic memory management in programming.

Uploaded by

codingworld5879
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

CS-206 Data

Structures and
Algorithms
Dr. Mariam Nosheen
Presentation Agenda
Data Structures and Abstract Data Types
Data Structures, Abstract Data Types,
and Implementations
To illustrate the process of organizing and structuring data in a problem, let us consider an
example. Suppose that Trans-Fryslan Airlines operates a single l0-passenger airplane. TFA would
like to modernize its operations and, as a first step, needs a program that will determine for each
flight, which seats have been assigned and which are still available.
In this problem, it is clear that the basic object is a collection of 10 seats and, for each seat, some
indication of whether it has been assigned. We must be able to perform the following operations:

(1) Examine the collection of seats to determine which of them are available;
(2) reserve a seat; and
(3) cancel a seat assignment. To perform these operations, it is convenient to think of the
seats as organized in a list.
After organizing the data as a list of 10 seats and identifying the basic operations to be performed,
we can consider ways to implement this structure. A variety of implementations are possible. For
example, we might represent the list of seats by 10 simple variables of type char:
Data Structures, Abstract Data Types,
and Implementations
To upgrade the program that incorporates these algorithms for a
larger airplane with 25 seats

Algorithm for Canceling a Seat


Data Structures, Abstract Data Types,
and Implementations

Abstract data type (ADT) consists of

Implementation of an ADT s
Data Structures, Abstract Data Types,
and Implementations

Abstract data type (ADT) consists of


The terms abstract data type and data structure are often used
interchangeably. However, we will use abstract data type when
data is studied at a logical or conceptual level, independent of
any programming language or machine considerations. The
term data structure refers to a construct in some programming
language that can be used to store data
Static Arrays

In addition to simple data types to


store individual data items, most
high-level programming languages
also provide data structures that
can be used to build storage Thus, an array has a fixed number of elements and
these are arranged in a sequence so there is a first
structures for collections of data element, a second element, and so on. Also, the data
items collection in an array is homogeneous; that is, all the
array elements must be of the same type. For example,
we might have an array of integers, an array of
characters, or even an array of arrays.
Static Arrays

Direct or random access means that each array element can be accessed simply by
specifying its position in the array, so that the time required to access each element
in the array is the same for all elements, regardless of their positions in the array.

For example, in an array of 100 elements, the


time required to access the seventy-fifth
element is the same as that for the fifth. This is
quite different from sequential access, in which
one can access an element only by first
accessing all those that precede it. Clearly, in
this case, the time required to access the
seventy-fifth element would be considerably
greater than that needed for the fifth.
Static Arrays

A particular element of the array is then accessed by attaching to the array name one
or more indices (also called subscripts) that specify the position of that element in
the array. If only one index is used, the array is said to be one-dimensional; arrays
involving more than one index are called multidimensional arrays.

There are two categories of arrays: static arrays, for which the compiler
determines how memory will be allocated for the array, and dynamic arrays for
which memory allocation takes place during execution.
Static Arrays
Static Arrays
Static Arrays

The Subscript Operation


An array declaration instructs the compiler that a block of consecutive
memory locations must be reserved that is large enough to store the
elements of the array. The address of the first element is called the
base address of the array and the array variable is actually a pointer to
this element; that is, its value is the base address of the array
Static Arrays
The Subscript Operation
The addresses of other array elements are translated
into offsets from this base address. This address
translation is performed by the subscript operator [].
For example, if b is the base address of a and ints are
stored in four bytes, then the first integer a[0] will be
stored in the four bytes beginning at address b, the
second integer a [1] in the four bytes beginning at
address b + 4, the next integer a [2] in the four bytes
beginning at address b + 8, and in general, a [i] in
four bytes beginning at address b + 4;.
Static Arrays
Arrays as Parameters
Functions can be written that
accept arrays via parameters
and then operate on the
arrays by operating on
individual array elements. For
example, we can output the
elements of an int array with
a function like
Static Arrays
Arrays as Parameters
It is also important to know
that an array is passed to a
function by passing its base
address; that is, arrays are
always passed by reference,
which means that modifying
an array parameter will also
modify the corresponding
argument. To illustrate this,
consider the function read ()
Static Arrays
Static Arrays

Problems with Arrays

• Out-of-range errors
• The capacity of an array cannot change during
program execution.
• An array is not an object
Multidimensional Arrays

Two-dimensional arrays
Two-dimensional arrays are useful when processing data arranged in
rows and columns. Similarly, a three-dimensional array is appropriate
when the data can be arranged in rows, columns, and ranks. When
several characteristics are associated with the data, still higher
dimensions may be appropriate, with each dimension Corresponding
to one of these characteristics
Multidimensional Arrays

Two-dimensional arrays
Two-dimensional arrays are useful when
processing data arranged in rows and
columns. Similarly, a three-dimensional
array is appropriate when the data can be
arranged in rows, columns, and ranks.
When several characteristics are associated
with the data, still higher dimensions may
be appropriate, with each dimension
Corresponding to one of these
characteristics
Multidimensional Arrays

Higher-Dimensional Arrays
The two-dimensional array scoresTable
models one page of a gradebook. To model
the entire gradebook, we can use a three-
dimensional array:

gradeBook[2] [3] [4]


Multidimensional Arrays

Higher-Dimensional Arrays
In some problems, arrays with even more dimensions may be useful.
For example, consider an automobile dealership that characterizes the vehicles it
sells with five attributes: year, model code, make, style, and color. We might
five-dimensional
maintain an inventory of these vehicles with a _________________array inventory
declared by
Multidimensional Arrays

Memory Allocation for a Multidimensional Arrays


Multidimensional Arrays as Parameters

Home Work: Implement


Previous example through
functions
Dynamic Arrays

Such fixed-size arrays have two drawbacks:


Dynamic Arrays

What is needed are arrays whose capacities are specified during


execution. Such dynamic arrays can be constructed using the
mechanism c++ provides for dynamic memory allocation.

At its simplest, such a mechanism requires two operations:


Dynamic Arrays

The new Operation


Dynamic Arrays

The new Operation


Dynamic Arrays

The new Operation


Dynamic Arrays

The new Operation


Pointer Arithmetic Size of operator
Dynamic Arrays

The new Operation


Pointer Arithmetic
Dynamic Arrays

The new Operation


Pointer Arithmetic
Dynamic Arrays

Failures of new
When execution of a program begins, the program has available to it a
"storage pool" of unallocated memory locations, called the heap or free
store. The effect of the new operation is to request the operating system
to

The size of the heap is limited, however, and each execution of new causes
the pool of available memory to shrink. This means that eventually new may
request more memory than is available in the heap, so the operating system
will not be able to fill the request
Dynamic Arrays

Failures of new
Dynamic Arrays

Failures of new
Dynamic Arrays

The delete Operation


We have described in some detail how memory can be allocated
dynamically using the new operator. Now we look at an analogous
method to reclaim memory allocated by new. In C++, this can be
accomplished by using the delete operation. Just as new is a request by
the executing program for memory from the heap, the delete operation
is a request to return memory to the heap where it can be reused in
later allocations. The new and delete operations are thus
complementary.
Dynamic Arrays

The delete Operation


Memory Leaks

Home Work: what is


memory leak and why we
need it

You might also like