0% found this document useful (0 votes)
4 views24 pages

Chapter #5 Arrays

The document provides an overview of data structures, focusing on arrays, their characteristics, and operations such as insertion and deletion. It explains the concept of arrays, their fixed size, and compares them to dynamic arrays (vectors) in C++. Additionally, it covers C++ vector functionalities and operations, highlighting the advantages of using vectors over static arrays.

Uploaded by

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

Chapter #5 Arrays

The document provides an overview of data structures, focusing on arrays, their characteristics, and operations such as insertion and deletion. It explains the concept of arrays, their fixed size, and compares them to dynamic arrays (vectors) in C++. Additionally, it covers C++ vector functionalities and operations, highlighting the advantages of using vectors over static arrays.

Uploaded by

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

1

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

There are many, but we named a few. We’ll learn these


data structures in great detail!
Overview of 5
Arrays

 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

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers


What is an 6
Array?

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.

Integer index 0 Integer index 4


(arr[0]) (arr[4])

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers


7

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.

Type name[ ]; string cars[4];

the rank of the array variable


the name of the array

the element type of the array

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers


Array 8
Rank

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

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers


Accessing Array 9
Elements

Accessing Array
• Supply Elements
an integer index for each
rank.
• Indexes are zero-based.

long row[ ]; int grid[][];


... ...
row[3]; grid[1][2];

2
3
1

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers


Fixed Size (Arrays)
The size of an array in C++ is fixed, meaning •
you cannot add or remove elements after it is
.created
// An array with 3 elements
string cars[3] = {"Volvo", "BMW", "Ford"};

// Trying to add another element (a fourth element)


to the cars array will result in an error
cars[3] = "Tesla";
Comparing Arrays to 11
Collections

Arrays drawbacks

.An array cannot resize itself when full •


.An array is intended to store elements of one type •
.In general, arrays are faster but less flexible •

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers


How to delete an element from
12

?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

To delete the 5’th cell 15


1 2 3 4 5 6 7 8 9 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];

7. for (int k = 0; k < n; k++)


8. cout << " "+id[k];

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

13. for (int k = 0; k < n; k++)


14. cout<< " "+id[k]; // printing the array
How to insert a new element in
15

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

To insert value in order13

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"};

// Get the first element


cout << cars.front();

// Get the last element


;)(cout << cars.back

Get the second element //


;cout << cars.at(1)
Add and Remove Vector Elements
vector<string> cars =
;{"Volvo", "BMW", "Ford", "Mazda"}

add an element at the end of the vector//


;cars.push_back("Tesla")

removes an element from the end of the vector//


;)(cars.pop_back
Size and empty
functions
vector<string> cars =
{"Volvo", "BMW", "Ford", "Mazda"};
cout << cars.size(); // Outputs 4

.return 1 if it is empty and o if it has elements //


vector<string> cars;
cout << cars.empty(); // Outputs 1 (The vector is
empty)

You might also like