0% found this document useful (0 votes)
32 views28 pages

LEC Week 1

The document outlines the course policies, structure, and content for a Data Structures and Algorithms class taught by Waris Ali. It covers topics such as assignment guidelines, recommended readings, the importance of data structures, and the efficiency of algorithms. Additionally, it introduces arrays, their declaration, dynamic arrays, and multidimensional arrays, including examples and initialization methods.

Uploaded by

ziaa0880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views28 pages

LEC Week 1

The document outlines the course policies, structure, and content for a Data Structures and Algorithms class taught by Waris Ali. It covers topics such as assignment guidelines, recommended readings, the importance of data structures, and the efficiency of algorithms. Additionally, it introduces arrays, their declaration, dynamic arrays, and multidimensional arrays, including examples and initialization methods.

Uploaded by

ziaa0880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

2

LEC # 01
Data Structure & Algorithms

Waris Ali
Lecture 1 3 3

Course Policies, Schedule, Syllabus

• Lectures are electronic and will be available after class.

• The quizzes will be unannounced and normally last for ten


minutes.

• It will be the instructor’s will to choose the number of


quizzes for evaluations purposes.
4

Assignment work
• All work submitted by a group must be
solely the work of that group
• All work submitted by an individual must
solely be the work of that individual
• This is not to mean that you may not consult with
others, but:
If you receive any help, you must specifically
acknowledge that person in your submitted work
• If any student or group of students submits work which
is not their own, they will be disciplined according to
the University and Department policies
5

Recommended Reading

• Introduction to Algorithms, (MIT Press) by Thomas H. Cormen, Charles E.


Leiserson, Ronald L. Rivest and Clifford Stein.

• Data Structures and Algorithm Analysis in C++, Mark Allen Weiss.


• Data Structures using C++, Tenanbaum
6

Contact Information

• Instructor: Waris Ali


• Email: [email protected]
• Counseling hours: to be announced later
• Class group:
7

+
Data
Structure
=?
Algorithm

Program
8 8

Data Structures
• A data structures is a way to store and organize data in order to facilitate
access and modifications.

• No single data structure works well for all purposes

• Need to know the strengths and limitations of several of them.


9

Need for Data Structures


 Data structures organize data  more efficient programs.
 More powerful computers  more complex applications.
 More complex applications demand more calculations.
10

Organizing Data
 Any organization for a collection of records that can be
searched, processed in any order, or modified.
 The choice of data structure and algorithm can make the
difference between a program running in a few seconds
or many days.
11

Efficiency
 A solution is said to be efficient if it solves the problem
within its resource constraints.
 Space
 Time

 The cost of a solution is the amount of resources that the


solution consumes.
12

Selecting a Data Structure


Select a data structure as follows:
1. Analyze the problem to determine the resource
constraints a solution must meet.
2. Determine the basic operations that must be supported.
Quantify the resource constraints for each operation.
3. Select the data structure that best meets these
requirements.
13

Some Questions to Ask


• Are all data inserted into the data structure at the
beginning, or are insertions interspersed with other
operations?
• Can data be deleted?
• Are all data processed in some well-defined order, or is
random access allowed?
14

Data Structure Philosophy


 Each data structure has costs and benefits.
 Rarely is one data structure better than another in all
situations.
 A data structure requires:
• space for each data item it stores,
• time to perform each basic operation,
• programming effort.
15

Data Structure Philosophy (cont)


Each problem has constraints on available space and time.

Only after a careful analysis of problem characteristics can we


know the best data structure for the task.

Bank example:
• Start account: a few minutes
• Transactions: a few seconds
• Close account: overnight
16

Arrays
 Elementary data structure that exists as built-in
in most programming languages.

main( int argc)


{
int x[6];
int j;
for(j=0; j < 6; j++)
x[j] = 2*j;
}
17

Arrays
 Array declaration: int x[6];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive
integers.
 To access a cell, use the array name and an
index:
x[0], x[1], x[2], x[3], x[4], x[5]
18

Array Layout
x[0]
Array cells are
x[1]
contiguous in
computer memory x[2]

The memory can be x[3]


thought of as an x[4]
array
x[5]
19

What is Array Name?


 ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
 For example, if we have the code

int a, b;

then we can write

b = 2;
a = b;
a = 5;

But we cannot write

2 = a;
20

Array Name
 ‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
21

Dynamic Arrays
 You would like to use an array data structure
but you do not know the size of the array at
compile time.
 You find out when the program executes that
you need an integer array of size n=20.
 Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
22

Dynamic Arrays
 ‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
 It can be assigned a value. The new operator
returns as address that is stored in y.
 We can write:

y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
23

Dynamic Arrays
 ‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
 It can be assigned a value. The new operator
returns as address that is stored in y.
 We can write:

y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
24

Dynamic Arrays
 We must free the memory we got using the
new operator once we are done with the y
array.

delete[ ] y;

 We would not do this to the x array because we


did not use new to create it.
25 25

Problems with Arrays


1. The capacity of Array can NOT change during program
execution.
What is the problem?
Memory wastage
Out of range errors
2. Arrays are NOT self contained objects
What is the problem?
No way to find the last value stored.
26 26

Multidimensional Arrays
Most high level languages support arrays with more than one
dimension.
2D arrays are useful when data has to be arranged in tabular form.
Higher dimensional arrays appropriate when several characteristics
associated with data.
Example: A table of test scores for several different students
on several different tests.
Test 1 Test 2 Test 3 Test 4
Student 1 99.0 93.5 89.0 91.0
Student 2 66.0 68.0 84.5 82.0
Student 3 88.5 78.5 70.0 65.0
: : : : :
: : : : :
Student-n 100.0 99.5 100.0 99.0
For storage and processing, use a two-dimensional array.
27 27

Declaring Two-Dimensional Arrays


Standard form of declaration:
element_type array_name[NUM_ROWS][NUM_COLUMNS];
[0] [[1] [2] [3]
Example: [0]
[1]
const int NUM_ROWS = 30, [2]
[3]
..
NUM_COLUMNS = 4; .
[29]

double scoresTable[NUM_ROWS][NUM_COLUMNS];

Initialization
 List the initial values in braces, row by row;
 May use internal braces for each row to improve readability.
Example:
double rates[2][3] = {{0.50, 0.55, 0.53}, // first row
{0.63, 0.58, 0.55}}; // second row
28 28

Processing Two-Dimensional Arrays


 Remember: Rows (and) columns are numbered from zero!!

Counting
from 0
 Use doubly-indexed variables:
scoresTable[2][3] is the entry in row 3 and column 4

row index column index

 Use nested loops to vary the two indices, most often in a rowwise manner.

You might also like