Lecture 1
Lecture 1
Lecture No.01
1
Need for Data Structures
2
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.
3
Efficiency
A solution is said to be efficient if it solves
the problem within its resource constraints.
– Space
– Time
4
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.
5
Arrays
• A list of values with the same data type
that are stored in contiguous locations
using a single group name (array
name).
6
One-Dimensional Arrays
7
One-Dimensional Arrays (cont.)
8
One-Dimensional Arrays (cont.)
element 3
Start here
9
1D Array Initialization
• Arrays can be initialized during their declaration
int arr[5] = {98, 87, 92, 79, 85};
int arr[5] = {98, 87} - what happens in this case??
• What is the difference between the following two
declarations ?
char codes[] = {'s', 'a', 'm', 'p', 'l', 'e'};
char codes[] = "sample";
codes[0] codes[1] codes[2] codes[3] codes[4] codes[5] codes[6]
s a m p l e \0
10
Two-dimensional Arrays
11
Two-dimensional Arrays (cont.)
13
1D Arrays as Arguments
14
1D Arrays as Arguments (cont.)
#include <iostream>
using namespace std;
float find_average(int[], int);
int main()
{
const int numElems = 5;
int arr[numElems] = {2, 18, 1, 27, 16};
cout << "The average is " << find_average(arr, numElems) << endl;
return 0;
}
float find_average(int vals[], int n)
{
int i;
float avg=0;
for(i=0; i<n; i++)
avg += vals[i];
avg = avg/n;
return avg; 15
}
1D Arrays as Arguments (cont.)
17
2D Arrays as Arguments (cont.)
#include <iostream>
using namespace std;
float find_average(int vals[][3], int, int);
int main()
{
const int Rows = 2;
const int Cols = 3;
int arr2D[Rows][Cols] = {5, 15, 11, 10,7,3};
float average;
average = find_average(arr2D, Rows, Cols);
cout << "The average is " << average << endl;
return 0;
}
18
2D Arrays as Arguments (cont.)
float find_average(int vals[][3], int n, int m)
{
int i, j;
float avg=0;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
avg += vals[i][j];
avg = avg/(n*m);
return avg;
}
19
2D Arrays as Arguments (cont.)
20
Pointers
21
Getting the address of a variable
22
Declaring Pointers
23
Dereferencing pointers
• To obtain the contents of the variable pointed to by
a pointer, we need to use the dereferencing
operator *, followed by the name of the pointer.
#include <iostream>
using namespace std;
int main()
{
int *p;
int x, y;
x = 22;
p = &x;
cout << "Address stored in p is " << p << endl;
cout << "Value pointed to by p is " << *p << endl;
y = 158;
p = &y;
cout << "Address stored in p is " << p << endl;
cout << "Value pointed to by p is " << *p << endl;
return 0; 24
}
Passing a pointer variable to a
function (cont.)
#include <iostream>
using namespace std;
void newval(int *, int *); // function prototype
int main()
{
int x, y;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the value of y: ";
cin >> y;
newval(&x, &y); // pass the address explicitly !!
cout <<"the value of x is "<< x << endl;
cout<<"the value of y is "<< y << endl;
return 0;
}
void newval(int *xnum, int *ynum)
{
*xnum = *xnum + 5; // dereferencing is required !!
*ynum = *ynum + 5; 25
}
Array names as pointers
26
Array names as pointers (cont.)
28
Array names as pointers (cont.)
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5;
int i, arr[SIZE] = {98, 87, 92, 79, 85};
int *p=arr;//&arr[0]
for(i=0; i<SIZE; i++)
cout << arr[i] <<"\t"<< *(arr + i) <<"\t"<< *(p + i) << endl;
cout<<"printing reverse array\n"; //printing reverse array using pointer
p=&arr[4];
while(p>=&arr[0]) {
cout << *p << endl; p--;
}
return 0; 29
}
Static Array Allocation
• Static 2D arrays are stored in the memory as
1D arrays..
30
Dynamic Array Allocation
• To avoid wasting memory, array allocation or
deallocation can take place at run time.
• To allocate memory, we need to use the new
operator
Reserves the number of bytes requested by the
declaration. Returns the address of the first reserved
location or NULL if sufficient memory is not available.
• To deallocate memory (which has previously been
allocated using the new operator) we need to use
the delete operator.
Releases a block of bytes previously reserved. The
address of the first reserved location is passed as an
argument to the function. 31
case of 1D arrays
cout << "Enter array size: ";
cin >> SIZE;
int *arr;
arr = new int[SIZE]; // allocation
32
case of 2D arrays
cout << "Enter numRows and numCols: ";
cin >> numRows >> numCols;
int **arr2D;
arr2D = new int* [numRows]; // allocation
for(i=0; i<numRows; i++)
arr2D[i] = new int[numCols];
33
case of 2D arrays (cont.)
38
Passing a 2D array to a function
#include <iostream>
#include <stdlib.h>
using namespace std;
float find_average(int**, int, int);
int main()
{
int **arr2D, i, j, numRows=2, numCols=3;
float average;
cout << "Enter numRows and numCols: ";
cin >> numRows >> numCols;
arr2D = new int*[numRows];
for(i=0; i<numRows; i++)
arr2D[i] = new int[numCols];
for(i=0; i<numRows; i++)
{
for(j=0; j<numCols; j++)
{
39
Passing a 2D array to a function (cont.)
arr2D[i][j] = rand()%100;
cout<<arr2D[i][j]<<"\t";
}
cout<<endl;
}
average = find_average(arr2D, numRows, numCols);
cout << "The average is " << average << endl;
return 0;
}
float find_average(int **vals, int n, int m)
{
int i, j;
float avg=0.0;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
avg += vals[i][j];
avg = avg/(n*m);
return avg;
}
40