Chapter #5 Arrays
Chapter #5 Arrays
CSC 1204
Data Structures and
Algorithms
Array
s
Dr. Asim Abdallah Elshiekh غفر الله له, University of Tabuk, Fac. of
?What is data
Data
A collection of facts from which conclusion may be drawn
e.g. Data: Temperature 35°C; Conclusion: It is hot.
Types of data
Textual: For example, your name (Mohammad)
Numeric: For example, your ID (090254)
Audio: For example, your voice
Video: For example, your voice and picture
Image …..
?What is data structure
A particular way of storing and organizing
data in a computer (Main Memory) so that it can
be used efficiently and effectively.
Data structure is the physical or mathematical
model of a particular organization of data.
A group of data elements grouped together
under one name.
For example, an array of integers
Types of Data Structures
Array
Linked List
Queue Stack
Tree
Overview of Arrays
• What is an Array?
• Array Notation
• Array Rank
• Fix array size
• Array drawbacks
• Delete array element
• Insert element inside array
• Operation on static array
• Dynamic Array Size (Vectors)
• C++ vector
• Vector functions
What is an
• An array is a Array?
sequence of homogeneous
elements.
All elements in an array have the same type.
Individual elements are accessed using integer
indexes.
• Structs can have elements of different types.
Array Notation
• You declare an array variable by
specifying:
The element type of the array.
The rank of the array.
The name of the array variable.
Array
• Rank: the
Rank
number of indexes associated with each
element.
• Rank is also known as the array dimension.
long row[ ]; int grid[][];
Rank 1: One-dimensional Rank 2: Two-dimensional
Single index associates with each Two indexes associate with each
long element int element
Accessing Array
• Supply Elements
an integer index for each
rank.
• Indexes are zero-based.
2
3
1
Arrays drawbacks
?array
0 1 2 3 4 5 6 7
10 11 12 13 14 8 9
array 9 9
2 7 9 12 15 23 30 45 70 77 80 88
0 9
0 1 2 3 4 5 6 7
10 11 12 13 14 8 9
array 9 9
2 7 9 12 15 23 30 45 70 77 80 88
0 9
Null
0 1 2 3 4 5 6 7
10 11 12 13 14 8 9
array 9 9
2 7 9 12 23 30 45 70 77 80 88 99
0 9
How to delete an element from
13
array?
1. void main() {
2. // where it’s location is known
3. int id[] = { 10, 12, 11, 45 };
int n = sizeof(id)/sizeof(id[0]);
4. // to delete the value of index 2
5. for (int i =2 ; i < n-1; i++)
6. id[i] = id[i + 1];
9.
10. }
How to delete an element from
14
1.array?
static void main(string[] args)
2. {
3. int[] id = { 10, 12, 11, 45 };
4. // to delete the value 12 where it’s location is
unknown
int n = sizeof(id)/sizeof(id[0]);
5. int i;
6. for ( i = 0; i < n; i++) {
7. if (id[i] == 12) {
8. break; // finding the location
9. }
10. }
11. for (int j =i ; j < n-1; j++)
12. id[j] = id[j + 1]; // replacing process
array?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 15 23 30 45 70 77 80 88 90 99
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
array 2 7 9 12 13 15 23 30 45 70 77 80 88 90 99
How to insert a new element in array?
16
1. void main()
2. {
3. int salary[6];
4. salary[0] = 1200;
5. salary[1] = 2000;
6. salary[2] = 1000;
7. salary[3] = 3210;
8. salary[4] = 4310;
9. int n = sizeof(salary)/sizeof(salary[0]);
10. // to add value 3000 in the position 3
11. for (int i =n - 1; i > 3; i--)
12. salary[i] = salary[i - 1];
13. salary[3] = 3000;
14. cout<< " === Values of Array after inserting === ";
15. for (int j = 0; j < n; j++)
16. cout<< " Value: "+salary[j];
17. }
How to insert a new element in array?
1. void main() {
2. int[] salary = new int[6];
17
3. salary[0] = 100;
4. salary[1] = 200;
5. salary[2] = 300;
6. salary[3] = 1210;
7. salary[4] = 2310;
8. int n = sizeof(salary)/sizeof(salary[0]);
9. // To add value 250
10. for (int i = 0; i < n; i++)
11. if (salary[i] > 250)
12. break;
13. for (int j = n - 1; j > i; j--)
14. salary[j] = salary[j - 1];
15. salary[i] = 250;
16. cout<< " === Values of Array after inserting === ";
17. for (int j = 0; j < n; j++)
18. cout<< " Value: " + salary[j];
19. }
Operation on static array
:Given sorted static array, we have the following run times
Dynamic Array Size
(Vectors)
For operations that require adding and •
removing array elements, C++
provides vectors, which are resizable
.arrays
The size of a vector is dynamic, •
meaning it can grow and shrink as
.needed
Both vectors and arrays are data •
structures used to store multiple
.elements of the same data type
C++ Vector
To use a vector, you have to include the <vector> •
:header file
>include <vector#
• To create a vector, use the vector keyword, and
specify the type of values it should store within angle
brackets <> and then the name of the vector,
like: vector<type> vectorName
vector<string> cars;
• If you want to add elements at the time of declaration,
place them in a comma-separated list, inside curly
braces {}, just like with arrays:
vector<string> cars =
{"Volvo", "BMW", "Ford", "Mazda"};
//f0reach
C++ Vector
One advantage of using the vector library, is that it
:includes many useful functions such as
)(front •
)(back •
)(at •
)(push_back •
)(Pop_back •
)(size •
)(empty •
Example of front, back and at
functions
Create a vector called cars that will store strings //
vector<string> cars =
{"Volvo", "BMW", "Ford", "Mazda"};