You are on page 1of 31

IMAM ABDULRAHMAN BIN FAISAL

UNIVERSITY
College of Computer Science & IT
Department of CS

Welcome to
CS 221:Fundamentals of Programming
Weeks (2): Arrays’ Storage Management
Instructors:
Dr. Malak Aljabri msaljabri@iau.edu.sa
Dr. Irfan Ullah Abdur rab iurab@iau.edu.sa
Dr. Garsah Al-Qarni gfalqarni@iau.edu.sa
Ms. Hanoof Algofari hmalgofari@iau.edu.sa
Mr. Saad Alharthi saalharthi@iau.edu.sa
Mr. Maamoun Ibrahim mmibrahim@iau.edu.sa
Objectives

• Arrays :
– Rotating/Shifting elements of an array
– Tracking storage in arrays

2
SHIFTING AND ROTATING
ELEMENETS

3
Rotating/Shifting Elements in Arrays
• Shifting/rotating elements in arrays is the concept of moving
(repositioning) array elements either up/down or right/left
based on the array dimensions.
– This may be accompanied with change of the cells’ own value.
– Array Rotation: means shifting the array elements to the left or
right of the array by specified number of positions. An array can
be rotated to the left(clockwise) or right(anti-clockwise) to the
given number of positions.
– Array Shifting means that the position of some values of the
array will be repositioned and some of them will have their
values changed and replaced with another values (zeros, empty
values, or any other values).
4
Shifting Elements in Arrays
• Shifting concept usually used to push array elements right,
left, up or down for all/some cells and fill some of the
shifted cells with other values.

5
Shifting Elements in Arrays
• For example, the number 13 is represented as the binary
number 00001101.
– Decimal numbers are represented in binary numbers of 8 bits.
Each bit location has its value.
Value of the binary 27 26 25 24 23 22 21 20
digit position
Index 7 6 5 4 3 2 1 0
Binary digit  0 0 0 0 1 1 0 1

– So, 13= 23 + 22 + 20 = 8 +4 + 1
– Some of the binary operations requires shift of values and
replacement of some other values. Let us see examples.

6
Shifting Elements in Arrays
• Example#1: decimal number (13)=binary number
(00001101)
– For example, to shift the binary number 2 cells to the right.
Shifting means pushing the elements to the right and filling
theBinary
rest of theofcells
value the with
27 zeros.
26 25 24 23 22 21 20
binary digit position
Index 7 6 5 4 3 2 1 0
Binary digit  0 0 0 0 1 1 0 1

After shifting 2 rights 0 0 0 0 0 0 1 1


to the right

– So, 21 + 20 = 2 + 1 = 3 (the value changed)


– The values on the right will be discarded. 7
Shifting Elements in Arrays
• Example#2: decimal number (146)=binary number
(10010010) based on 27 + 24 + 21 = 128 + 16 + 2 = 146
– For example, to shift the binary number 2 cells to the left.
Shifting means pushing the elements to the left and filling the
rest of the cells with zeros.
Binary value of the 27 26 25 24 23 22 21 20
binary digit position
Index 7 6 5 4 3 2 1 0
Binary digit  1 0 0 1 0 0 1 0
After shifting 2 rights to 0 1 0 0 1 0 0 0
the right

– So, 26 + 23 = 64 + 8 = 72 (the value changed)


– The values on the left will be discarded. 8
Rotating Elements in an Array
• Example#3:
– Assume that we have the following array:
string students [5]={“Omar”,”Mona”,”Layla”,”Saad”,”Rana”};
0 1 2 3 4
Omar Mona Layla Saad Rana

– Rotate the above array elements to the right by 3 cells, would


result :
0 1 2 3 4
Layla Saad Rana Omar Mona
– What is the code that we need to write in order to perform
this?
New Index = Previous Index + 3
9
Rotating Elements in an Array
• Example#3: What is the code that we need to write in order
to perform this?
Omar Mona Layla Saad Rana

Layla Saad Rana Omar Mona


main()
{
const int SIZE = 5;
string students[SIZE]={"Omar","Mona","Layla","Saad","Rana"};
for (int i=0; i<SIZE ;i++)
cout<<students[i]<<"\t";
cout<<endl;

10
Rotating Elements in an Array
string temp[SIZE];//create array with same size
int newPositionIndex;
for (int i=0; i<SIZE ;i++) Why do we use reminder % ?
{ newPositionIndex=i+3;
if (newPositionIndex>=SIZE)
newPositionIndex=newPositionIndex%SIZE;
temp[newPositionIndex]=students[i];
}

cout<<"New Order: (printing from the temp array)\n";


for (int i=0; i<SIZE ;i++)
cout<<temp[i]<<"\t";
}
We use the reminder to ensure the resulting index is within the
correct range from 0 to Size-1.
11
Rotating Elements in an Array
string temp[SIZE];//create array with same size
int newPositionIndex;
for (int i=0; i<SIZE ;i++)
{ newPositionIndex=i+3;
if (newPositionIndex>=SIZE)
newPositionIndex=newPositionIndex%SIZE;
temp[newPositionIndex]=students[i];
}

cout<<"New Order: (printing from the temp array)\n";


for (int i=0; i<SIZE ;i++)
cout<<temp[i]<<"\t";
}

After executing this program, does both arrays: students and


temp have the same order of the elements?
12
Rotating Elements in an Array
After executing this program, does both arrays: students and
temp have the same order of the elements?
• No.
• Students have the original order, whereas temp has the new order.
• So, to let students array have the new order we need to copy the new order to it
as following :
//to copy the elements to replace original elements
for (int i=0; i<SIZE ;i++)
students[i]=temp[i]; //copy element by element
cout<<endl;
cout<<"New Order:(printing from the students array)\
n";
for (int i=0; i<SIZE ;i++)
cout<<students[i]<<"\t";

13
Rotating Elements in an Array
Do we need always for another temporary array to rotate elements?
string temp; /*create single temp variable to hold any item of
your choice in the array and then start shifting from it.*/
temp=students[SIZE-1]; //we chose last item
by tracking it line by line we can write for this specific example:
temp= students[4];
students[4]= students[1]; 0 1 2 3 4
students[1]= students[3]; Omar Mona Layla Saad Rana
students[3]= students[0];
students[0]= students[2]; 0 1 2 3 4
students[2]= temp; Layla Saad Rana Omar Mona

• No we don’t need always for another array, but we need to ensure


not to lose value while repositioning the elements.

Can we generalize it in fewer line of code?


14
TRACKING STORAGE IN ARRAY

15
Tracking Storage in Arrays
• Tracking storage in arrays means that you need to maintain
basic operations on those arrays.
• Basic operations involve:
– Adding New Items: You keep track of where are you going to
store and add new items in your array(s).
– Printing Items: printing all existing items (not empty ones)
– Updating Existing Values: Update values of stored items
inside your arrays.
– Deleting Existing items: technically to do this in a fixed-size
array means that you need to free the location of the item you
want to delete and replace it with an empty cell.
• This operation is more flexible with use of linked lists and
dynamic storage. 16
Tracking Storage in Arrays
(Adding new items)
• To track an array let us assume that we have 2 empty
arrays of size 10 for example.
– string array for students’ names.
– double array of same size for students’ GPAs.
– Both empty at the start 0 0
1 1
– So number of items is 0
2 2
– So code will be: 3 3
const int SIZE = 10; 4 4
Names Array

GPA Array
string names[SIZE]; 5 5
double gpa[SIZE]; 6 6
int numberOfItems = 0; 7 7
8 8

17
9
9
Tracking Storage in Arrays
(Adding new items)
• So, to add new student (Ahmed with GPA 3.8) we do:
– names[numberOfItems]=“Ahmed”;
– gpa[numberOfItems]=3.8;
– numberOfItems++;  why do we need to increase it?

0 Ahmed 0 3.8
1 1
2 2
3 3
4 4
Names Array

GPA Array
5 5
6 6
7 7
8 8

18
9
9
Tracking Storage in Arrays
(Adding new items)
• Let us add one more new student (Sara with GPA 4.0) we do:
– names[numberOfItems]=“Sara”;
– gpa[numberOfItems]=4;
– numberOfItems++;

0 Ahmed 0 3.8
1 Sara 1 4
2 2
3 3
4 4
Names Array

GPA Array
5 5
6 6
7 7
8 8

19
9
9
Tracking Storage in Arrays
(Adding new items)
• Let us add one more new student (Khalid with GPA 4.2) we do:
– names[numberOfItems]=“Khalid”;
– gpa[numberOfItems]=4.2;
– numberOfItems++;

0 Ahmed 0 3.8
1 Sara 1 4
2 Khalid 2 4.2
3 3
4 4
Names Array

GPA Array
5 5
6 6
7 7
8 8

20
9
9
Tracking Storage in Arrays
(Adding new items)
• If we would like to print the number of existing items what shall we
write?
– cout<< numberOfItems;
• Where will the new item be stored ? Why?
– Will be stored at index of 0 Ahmed 0 3.8
numberOfItems 1 Sara 1 4
– Because we have incremented it 2 Khalid 2 4.2
and made it ready for new items 3 3
to be added. 4 4
Names Array

GPA Array
5 5
6 6
7 7
8 8

21
9
9
Tracking Storage in Arrays
(Adding new items)
• How can we check that the array can still accept adding new
items? (not exceeding its size)
– cout<<“Enter new name: ”;
0 Ahmed 0 3.8
cin>>newName;
1 Sara 1 4
cout<<“Enter new student GPA: ”;
2 Khalid 2 4.2
cin>>newGPA;
if (numberOfItems < SIZE) 3 3
{ 4 4

Names Array

GPA Array
names[numberOfItems]= newName; 5 5
gpa[numberOfItems]= newGPA; 6 6
numberOfItems++; 7 7
} 8 8

9
9

22
Tracking Storage in Arrays
(Printing Existing items)
• How can we print array’s existing items?
– cout<<“Name \t GPA \n ”;
for(int i=0;i<numberOfItems; i++)
cout<< names[i]<<“\ 0 Ahmed 0 3.8
t”<<gpa[i]<<endl; 1 Sara 1 4
• Why we don’t let the condition to be i<= 2 Khalid 2 4.2
3 3
numberOfItems?
4 4
– Because numberOfItems indicates

Names Array

GPA Array
5 5
where the new item will be added (not 6 6
filled yet with values). 7 7
8 8

9
9

23
Tracking Storage in Arrays
(Updating Existing Item)
• Let us update student (Sara to have spelling of Sara Omar and to
have GPA 4.5) we need to:
– Find the index of the Sara then update it.
Names[1]= “Sara Omar” 0 Ahmed 0 3.8
gpa[1]=4.5; 1
1
Sara 1 4.5
4
2 Omar
Khalid 2 4.2
2 Khalid
3 3
3
4 4

Array

GPA Array
4

NamesArray
5 5
5
6 6
6

Names
7 7
7
8 8
8

9
9
9 24
Tracking Storage in Arrays
(Updating Existing Item)
• Let us generalize the solution for any update:
cout<<“Enter current student name: ”;
cin>>sName; Here we are :
for (int i=0;i< numberOfItems;i++) • assuming each name is
{ unique in our search.
if (names[i]== sName){ • So, if there are multiple
cout<<“Enter new name: “; students with same
cin>>updatedName; name we need to find a
names[i]= updatedName; better way (we will
cout<<“Enter new GPA: “; discuss it later)
cin>>updatedGpa; • When found and
gpa[i]= updatedGpa; updated we will (break)
break; to optimize our code not
} to continue search the
} whole array after items
has been found.
25
Tracking Storage in Arrays
(Deleting Existing Item)
• Let us delete student (Khalid) we need to:
– Find the index of the Khalid then delete it.
– This can be done by shifting all elements after
Khalid to override Khalid records and decrease
0 Ahmed 0 3.8
the number of items. 1 Sara 1 4
– Khalid exists at index 2 2 Khalid
Saad 2 4.2
3.4
– So, to shift elements we write: 3 Maher
Saad 3 3.4
4.4
for (int i=2;i< numberOfItems-1;i++) 4 Maher
Nora 4 4.4
4.8

Names Array

GPA Array
{names[i]=names[i+1]; 5 Nora 5 4.8
gpa[i]=gpa[i+1];} 6 6
-- numberOfItems;  why ? 7 7
• To reduced number of elements after deleting 8 8
Khalid.
9
9
26
Tracking Storage in Arrays
(Deleting Existing Item)
• Why we subtract one in i< numberOfItems-1?
– So, to shift elements we write:
for (int i=2;i< numberOfItems-1;i++)
{names[i]=names[i+1];
gpa[i]=gpa[i+1];
}
-- numberOfItems;
– Let us track it:
i Executed Code
2 names[2]=names[3]; gpa[2]=gpa[3];
3 names[3]=names[4]; gpa[3]=gpa[4];
4 names[4]=names[5]; gpa[4]=gpa[5];

We need to stop at one element before the end, otherwise will access garbage
values of items outside the array ( numberOfItems -1 ) 27
Tracking Storage in Arrays
(Deleting Existing Item)
• Let us generalize the solution for any delete:
cout<<“Enter student name to delete: ”; Here we are :
cin>>sName; • assuming each
for (int i=0;i< numberOfItems;i++){ name is unique
if (names[i]== sName){ in our search.
for (int j=i; j< numberOfItems-1; j++) • When found and
{ delete we will
names[j]=names[j+1]; (break) to
gpa[j]=gpa[j+1]; optimize our .
} • Have you
numberOfItems--; thought if the
break; student name
} didn’t exist?
} How can we
check?

28
Tracking Storage in Arrays
(Deleting Existing Item)
• How can we check that the array is not empty
before deleting?
if(numberOfItems > 0)
{cout<<“Enter student name to delete: ”;
cin>>sName;
for (int i=0;i< numberOfItems;i++){ Why do we need
if (names[i]== sName){ nested loops?
for (int j=i; j< numberOfItems-1; j++)
{
names[j]=names[j+1];
gpa[j]=gpa[j+1];
}
numberOfItems--;
break;
}
}}
29
Questions?
Thank you for listening 

30
References

• https://www.geeksforgeeks.org/array-rotation/
• https://www.youtube.com/watch?v=G9lQQlVeMao

31

You might also like