0% found this document useful (0 votes)
9 views12 pages

Dsa L5

The document discusses dynamic arrays, their implementation, and applications in programming, particularly in managing game entries. It covers operations such as inserting and deleting elements, as well as sorting and searching algorithms. Additionally, it introduces multi-dimensional arrays and their representation in C++.

Uploaded by

Alkit Sharma
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)
9 views12 pages

Dsa L5

The document discusses dynamic arrays, their implementation, and applications in programming, particularly in managing game entries. It covers operations such as inserting and deleting elements, as well as sorting and searching algorithms. Additionally, it introduces multi-dimensional arrays and their representation in C++.

Uploaded by

Alkit Sharma
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/ 12

CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD

(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.


BITS-Pilani Hyderabad Campus
DYNAMIC ARRAYS hota[AT]hyderabad.bits-pilani.ac.in
ARRAYS: WHY?
Let us assume that we have to read, process and print 10 numbers (integers) and keep
those in the memory throughout the execution.

score0 Read score0


Print score0
Read score1 Definitely, not a
score1
.. good idea for
Print score1
. 10000 numbers.
score2 ..
. .
Read score10
. Print score10
. Process
score9
CONTINUED… An array of scores: Only one variable is enough.

score0 score[0]

score1 score[1]
… …
score9 score[9]

Subscripting Indexing

start 1 2

i=0 i=0
i++ i++
i < 10 Process i < 10
read score[i] write score[i]
1 2 stop
DYNAMIC ARRAYS
• What are they?

Applications of arrays: Maths (vectors, matrices, polynomials,…), databases,


compilers (control flow), dynamic memory allocations etc.
void Dynamic1DArray :: shrink() {

capacity >>= 1;

DYNAMIC ARRAYS EXAMPLE: LAB3 int *newArr = new int[capacity];

for (int i = 0; i < size; i++)


newArr[i] = arr[i];

• Let us understand the operations needed to // update the global array pointer
implement a dynamic array: insert, remove etc. }
arr = newArr;
(shrink)

(DynamicArray.cpp given in the next week’s lab sheet)


(Output)
USING ARRAYS: AN EXAMPLE

{An entries array of length 10 with 6 {Copying the new entry into the position.
GameEntry objects (maxExntries: 10, Scenario after addition}
numEntries: 6)}

{Preparing to add a new GameEntry {Removing an element at index i requires


object by shifting all the erntries with moving all the entries at indices higher than i
smaller scores to the right by one one position to the left}
position}
IMPLEMENTATION: STORING GAME ENTRIES
class GameEntry { class Scores {
public: public:
GameEntry ( const string &n = "", int s = 0); Scores(int maxEnt = 10);
string getName() const; ~Scores();
int getScore() const; void add(const GameEntry &e);
private: GameEntry remove(int i) ;
string name; void printAllScores();
int score; private:
}; int maxEntries; //maximum number of entries
int numEntries; //actual number of entries
( A Class representing a Game entry)
GameEntry *entries;
}; ( A Class for storing Game scores)
GameEntry::GameEntry(const string &n, int s) : name(n),
score(s) { } Scores::Scores(int maxEnt) {
string GameEntry::getName() const { return name; } maxEntries = maxEnt; // save the max size
int GameEntry::getScore() const { return score; } entries = new GameEntry[maxEntries];
numEntries = 0;
( Constructor and member functions) } Scores::~Scores() { delete[ ] entries; }
INSERTING INTO AND DELETING FROM ARRAY
void Scores::add(const GameEntry &e) {
int newScore = e.getScore(); // score to add
if (numEntries == maxEntries) { // the array is full
if (newScore <= entries[maxEntries - 1].getScore())
return; // not high enough - ignore GameEntry Scores::remove(int i)
} {
else numEntries++; // if not full, one more entry if ((i < 0) || (i >= numEntries)) // invalid index
throw("IndexOutOfBounds - Invalid index");
int i = numEntries - 2; // start with the next to last
while (i >= 0 && newScore > entries[i].getScore() ) { GameEntry e = entries[i]; // save the removed object
entries[i + 1] = entries[i]; // shift right if smaller for (int j = i + 1; j < numEntries; j++)
i--; entries[j - 1] = entries[j]; // shift entries left
} numEntries--; // one fewer entry
entries[i + 1] = e; // put e in the empty spot return e; // return the removed object
} }

(Inserting a Game entry object) (Removing a Game entry object)


DRIVER AND OTHER CLASSES FOR GAME ENTRY EX.

(Lab3: GameEntry.cpp)
LAB3 TASKS: GAME ENTRY

(How many number of entries are there for each player? Option 4)

(display unique entries for each player?)


(Display players in a score range: Option 6) (GameEntry_Unique.cpp)
SORTING & SEARCHING IN AN ARRAY
void Dynamic1DArray ::sort() int Dynamic1DArray
{ ::binarySearch(const int item)
for (int j = 1; j < size; j++) {
{ int low = 0, high = size - 1;
while (low <= high){
int key = arr[j]; int mid = low + ((high –
int i = j - 1; low) >> 1);
while (i > -1 && arr[i]>key) if (item == arr[mid])
{ return mid;
arr[i + 1] = arr[i]; if (item < arr[mid])
i = i - 1; high = mid - 1;
} else
arr[i + 1] = key; low = mid + 1;
} }
return -1; }
}
(Insertion Sort) More sorting & searching algos later… (Binary Search)
MULTI-DIMENSIONAL ARRAYS
Month
0 1 2 3 4 5 6 7 8 9 10 11 Arrays in C++ are one-dimensional.
0 30 40 75 95 130 220 210 185 135 80 40 45 However, we can define a 2D array
1 25 25 80 75 115 270 200 165 85 5 10 16
as “an array of arrays”.
Year

2 35 45 90 80 100 205 135 140 170 75 60 95


3 30 40 70 70 90 180 180 210 145 35 85 80
4 30 35 40 90 150 230 305 295 60 95 80 30
3-dimensional
Average Yearly Rainfall (in mm of Hyd)
0 1 2 3 4 5 6 7 8 9 10 11
0 20 60 75 95 130 220 210 185 135 80 40 45
Hyd
1 29 25 80 75 115 270 200 165 85 5 10 16
0 1 2 3 4 5 6 7 8 9 10 11
2 35 45 90 80 100 205 135 140 170 75 60 95
0 10
3 30 40 70
20 35 95 130 220 210 185 135 80 40
70 90 180 180 210 145 35 85 80
45 Delhi
1 5 17 9 8 115 270 200 165 85 5 10 16
4 30 35 40 90 0 1501 230
2 305
3 4295 5 60 695 80
7 30 8 9 10 11
2 35 45 90 80 100 205 135 140 170 75 60 95
0 10 20 35 95 130 220 210 185 135 80 40 45
3 30 40 70 70 90 180 180 210 145 35 85 80
1 5 17 9 8 115 270 200 165 85 5 10 16
4 30 35 40 90 150 230 305 295 60 95 80
2 35 45 90 80 100 205 135 140 170
30
75 60 95 Goa
3 30 40 70 70 90 180 180 210 145 35 85 80
4 30 35 40 90 150 230 305 295 60 95 80 30

You might also like