0% found this document useful (0 votes)
15 views

Data Structure Lecture 4

The document provides an introduction to arrays as a data structure. It discusses how arrays can be used to store lists and tables of related data of the same type. The key points covered include defining and initializing arrays, accessing individual array elements using indexes, and using one-dimensional and two-dimensional arrays. It also introduces the concept of an abstract data type as it relates to arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data Structure Lecture 4

The document provides an introduction to arrays as a data structure. It discusses how arrays can be used to store lists and tables of related data of the same type. The key points covered include defining and initializing arrays, accessing individual array elements using indexes, and using one-dimensional and two-dimensional arrays. It also introduces the concept of an abstract data type as it relates to arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

DATA STRUCUTERE

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

 Suppose, you need to store years of 100 cars. Will you


define 100 variables?
int y1, y2,…, y100;
 An array is an indexed data structure to represent
several variables having the same data type: int
y[100];

y[0] y[1] y[2] … … y[98] y[99]


Definition and Initialization
9

 An array is defined using a declaration statement.

data_type array_name[size];

 allocates memory for size elements


 subscript of first element is 0
 subscript of last element is size
 size must be a constant
Example
10

int count Enough memory for 1 int

12345

float price Enough memory for 1 float

56.981

char letter Enough memory for 1 char

A
Figure
11
Table
12

Array Declaration Number of Size of Size of the


Elements Each Element Array

char letters[25]; 25 1 byte 25 bytes


short rings[100]; 100 2 bytes 200 bytes
int miles[84]; 84 4 bytes 336 bytes
float temp[12]; 12 4 bytes 48 bytes
doubledDistance[1000]; 1000 8 bytes 8000 bytes
Accessing Array elements
13

 The individual elements of an array are


assigned unique subscripts. These subscripts
are used to access the elements.
Program
14

// 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];

cout << "Enter the hours worked by six employees: ";


cin >> hours[0];
cin >> hours[1];
cin >> hours[2];
cin >> hours[3];
Program continues
15

cin >> hours[4];


cin >> hours[5];
cout << "The hours you entered are:";
cout << " " << hours[0];
cout << " " << hours[1];
cout << " " << hours[2];
cout << " " << hours[3];
cout << " " << hours[4];
cout << " " << hours[5] << endl;
}
Program

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];

cout << "Enter the hours worked by six employees.\n";


for (int count = 1; count <= 6; count++)
{
cout << "Employee " << count << ": ";
cin >> hours[count-1];
}
Program continues
17

for (count = 1; count <= 6; count++)


{
cout << "Employee " << count << ": ";
cout << hours[count] << endl;
}
}
Array Initialization
18

 Arrays may be initialized when they are declared.


Program

19

// This program displays the number of days in each month.


// It uses a 12-element int array.

#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

days[7] = 31; // August


days[8] = 30; // September
days[9] = 31; // October
days[10] = 30; // November
days[11] = 31; // December
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.\n";
}
}
Program

21

// This program displays the number of days in each month.


// It uses a 12-element int array.
#include <iostream>

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

 When an array is being initialized, C++ does not


require a value for every element.

int numbers[7] = {1, 2, 4, 8};


Program
23

// This program has a partially initialized array.

#include <iostream.h>

void main(void)
{
int numbers[7] = {1, 2, 4, 8}; // Initialize the
// first 4 elements.

cout << "Here are the contents of the array:\n";


for (int index = 0; index < 7; index++)
cout << numbers[index] << endl;
}
Implicit Array Sizing
24

 It is possible to declare an array without specifying


its size, as long as you provide an initialization list.

float ratings[] = {1.0, 1.5, 2.0, 2.5, 3.0};


Initializing With Strings
25

 When initializing a character array with a string,


simply enclose the string in quotation marks:

char name[] = “Warren”;


Figure
26
Program

27

// This program displays the contents of two char arrays.


#include <iostream.h>

void main(void)
{
char name1[] = "Holly";
char name2[] = {'W', 'a', 'r', 'r', 'e', 'n'};

cout << name1 << endl;


cout << name2 << endl;
}
Two-Dimensional Arrays

 Imagine now we want to keep 10 test scores for 30 students.


 How would we represent this in a program with what we’ve

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

rows and columns in memory.


 So, a better solution to the above question is what?
 Answer: Have a 30 x 10 or a 10 x 30 two dimensional

array!
Two-Dimensional Arrays

 A two- dimensional mXn array A is collection of m*n data


elements such that each element is specified by a pair of
integers ( such as I, J), called subscripts, with the property
that, 0≤I<m and 0≤ J<n.
 The elements if A with first subscript I and second
subscript j will be denoted by,
A[I , j] or A[i][j] ( c language/C++)
 Two- dimensional arrays are called matrices in
mathematics and tables in business applications; hence two
– dimensional arrays are sometimes are called matrix
arrays.
Program
#include <iostream>
int main () {
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ ) {

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
?

You might also like