Data Structure Lecture 4
Data Structure Lecture 4
INTRODUCTION TO DATA
STRUCTURE
Lecture 4
Objectives In this chapter, you’ll
learn
To use the array data structure to represent lists and
tables of values.
To define an array, initialize an array and refer to
individual elements of an array.
To use arrays to store, sort and search lists and
tables of values.
Introduction
This chapter serves as an introduction to the
important topic of data structures. Arrays are data
structures consisting of related data items of the
same type.
in the others chapters we introduce dynamic data
structures such as lists, graph and trees that may
grow and shrink as programs execute.
Arrays
An array is a group of memory locations related by the fact that they
all have the same name and the same type.
Elements are stored at contiguous memory locations.
An index is always less than the total number of array items.
In terms of syntax, any variable that is declared as an array can store
multiple values.
Almost all languages have the same comprehension of arrays but have
different ways of declaring and initializing them.
However, three parts will always remain common in all the
initializations, i.e., array name, elements, and the data type of elements.
To refer to a particular location or element in the array, we specify the
name of the array and the position number of the particular element in
the array.
Arrays
an integer array called c. This array contains 12
elements. Any one of these elements may be
referred to by giving the name of the array
followed by the position number of the particular
element in square brackets ([]).
Arrays
The first element in every array is the zeroth
element. Thus, the first element of array c is
referred to as c[0], the second element of array c is
referred to as c[1], the seventh element of array c is
referred to as c[6],
Arrays
The position number contained within square
brackets is more formally called a subscript (or
index). A subscript must be an integer or an integer
expression. If a program uses an expression as a
subscript, then the expression is evaluated to
determine the subscript.
5.1 One-Dimensional Arrays
8
data_type array_name[size];
12345
56.981
A
Figure
11
Table
12
// This program asks the user for the number of hours worked
// by 6 employees. It uses a 6-element int array to store the
// values.
#include <iostream>
int main()
{
short hours[6];
16
// This program asks the user for the number of hours worked
// by 6 employees. It uses a 6-element short array to store the
// values.
#include<iostream>
using namespace std;
int main(){
short hours[6];
19
#include <iostream>
int main()
{
int days[12];
days[0] = 31; // January
days[1] = 28; // February
days[2] = 31; // March
days[3] = 30; // April
days[4] = 31; // May
days[5] = 30; // June
days[6] = 31; // July
Program continues
20
21
int main()
{
int days[12] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.\n";
}
}
Partial Array Initialization
22
#include <iostream.h>
void main(void)
{
int numbers[7] = {1, 2, 4, 8}; // Initialize the
// first 4 elements.
27
void main(void)
{
char name1[] = "Holly";
char name2[] = {'W', 'a', 'r', 'r', 'e', 'n'};
learned?
Answer: You would need either 30 10-element arrays or
10 30-element arrays.
This was the problem arrays were supposed to solve!
Two dimensional arrays can be though of having a table of
array!
Two-Dimensional Arrays
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
}
ARRAY ADT
Abstract Data type (ADT) is a type (or class) for
objects whose behavior is defined by a set of values
and a set of operations. The definition of ADT only
mentions what operations are to be performed but
not how these operations will be implemented.
Abstract Data Types (aka ADTs) are descriptions of
how a data type will work without implementation
details
ARRAY ADT
An abstract data type (ADT) is the specification of a
data type within some language, independent of an
implementation.
The interface for the ADT is defined in terms of a type
and a set of operations on that type.
The behavior of each operation is determined by its
inputs and outputs.
An ADT does not specify how the data type is
implemented. These implementation details are hidden
from the user of the ADT and protected from outside
access, a concept referred to as encapsulation.
ARRAY ADT
A data structure is the implementation for an ADT.
In an object-oriented language, an ADT and its
implementation together make up a class.
Each operation associated with the ADT is
implemented by a member function or method.
The variables that define the space required by a
data item are referred to as data members.
An object is an instance of a class, that is,
something that is created and takes up storage
during the execution of a computer program.
Basic Operations
Following are the basic operations supported by an
array.
Traverse − print all the array elements one by one.
Insertion − add an element at given index.
Deletion delete an element at given index.
Search − search an element using given index or by
value.
Basic Operations on Arrays
Arrays are an important data structure used to store
collections of data. Arrays can store multiple types of
data, such as integers, strings, floats, and objects. There
are several types of arrays in data structure:
Traverse − print all the array elements one by one.
Insertion − add an element at given index.
Deletion delete an element at given index.
Search − search an element using given index or by value.
Basic Operations on Arrays
Traversing
Traversing arrays involves looping through each element in the
array and processing each element one at a time. This allows you
to access all array elements and perform tasks such as printing,
copying, comparing, or sorting. Moreover, a Data Structure
Course can help you understand arrays and their applications.
Insertion
Insertion is the process of adding new elements into an existing
array. This can be done by providing an index for where the
insertion should occur and then shifting other elements in the
array to make space for the insertion.
Basic Operations on Arrays
Traversing
for(int i=0;i<length;i++)
cout<<a[i]<<" ";
cout<<endl;
Insertion
for(int i=length-1;i>=index;i--)
a[i+1]=a[i];
a[index]=x;
length++;
}
Basic Operations on Arrays
Deletion
Deletion is the opposite of insertion and involves removing elements
from an existing array. After deleting an element, all other elements
in the array must be shifted to fill any gaps left from deletion.
Searching
Looking for something specific? Searching is the answer. It's a
process of identifying an element from within an array by comparing
it to your desired value until you find a match. There are two distinct
types of searches: linear and binary search techniques, both offering
varying degrees of efficiency when used correctly. Linear search
compares each element one after another until a match is found or all
elements have been searched.
Basic Operations on Arrays
Deletion
if(index>=0 && index<length)
{
x=a[index];
for(int i=index;i<length-1;i++)
a[i]=a[i+1];
length--;
}
return x;
}
Basic Operations on Arrays
Sorting
Sorting is a process of arranging elements of an
array in either ascending or descending order.
Array sorting can be done using different
algorithms like bubble sort, insertion sort, selection
sort, and quick sort. Bubble sort swaps adjacent
elements if they are not ordered correctly, while
selection sort finds the smallest element and shifts
it to the beginning.
END
?