SlideShare a Scribd company logo
Arrays
C++ Style Data Structures: Arrays(1)
• An ordered set (sequence) with a fixed
number of elements, all of the same
type,
where the basic operation is
direct access to each element in the
array so values can be retrieved from
or stored in this element.
C++ Style Data Structures: Arrays (2)
Properties:
Ordered so there is a first element, a second one, etc.
Fixed number of elements — fixed capacity
Elements must be the same type (and size);
∴ use arrays only for homogeneous data sets.
Direct access: Access an element by giving its location
The time to access each element is the same for all
elements, regardless of position.
in contrast to sequential access (where to access an
element, one must first access all those that precede it.)
Declaring arrays in C++
where
element_type is any type
array_name is the name of the array — any valid identifier
CAPACITY (a positive integer constant) is the number of elements
in the array
score[0]
score[1]
score[2]
score[3]
score[99]
.
.
.
.
.
.
element_type array_name[CAPACITY];
e.g., double score[100];
The elements (or positions) of the array are indexed 0, 1,
2, . . ., CAPACITY - 1.
Can't input the
capacity, Why?
The compiler reserves a block of “consecutive” memory
locations, enough to hold CAPACITY values of type
element_type.
indices numbered 0, 1, 2, . . ., CAPACITY - 1
How well does C/C++ implement an array ADT?
As an ADT In C++
ordered
fixed size
same type elements
direct access
element_type is the type of elements
CAPACITY specifies the capacity of the array
subscript operator []
an array literal
Array Initialization
Example:
double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21};
Note 1: If fewer values supplied than array's capacity, remaining elements
assigned 0.
double rate[5] = {0.11, 0.13, 0.16};
Note 2: It is an error if more values are supplied than the declared size of the array.
How this error is handled, however, will vary from one compiler to another.
rate
0 1 2 3 4
0.11 0.13 0.16 0 0
rate
0 1 2 3 4
0.11 0.13 0.16 0.18 0.21
In C++, arrays can be initialized when they are declared.
Numeric arrays:
element_type num_array[CAPACITY] = {list_of_initial_values};
Note 1: If fewer values are supplied than the declared size of the array,
the zeroes used to fill un-initialized elements are interpreted as
the null character '0' whose ASCII code is 0.
const int NAME_LENGTH = 10;
char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'};
vowel
0 1 2 3 4
A E I O U
char vowel[5] = {'A', 'E', 'I', 'O', 'U'};
Character Arrays:
Character arrays may be initialized in the same manner as numeric arrays.
declares vowel to be an array of 5 characters and initializes it as follows:
collegeName
0 1 2 3 4 5 6 7 8 9
C a l v i n 0 0 0 0
Addresses
When an array is declared, the address of the first byte (or word) in the block of
memory associated with the array is called the base address of the array.
Each array reference must be translated into an offset from this base address.
For example, if each element of array score will be stored in 8 bytes and the base
address of score is 0x1396. A statement such ascout << score[3] << endl;
requires that array reference score[3]
be translated into a memory address:
0x1396 + 3 * sizeof (double)
= 0x1396 + 3 * 8
= 0x13ae
The contents of the memory word with this address
0x13ae can then be retrieved and displayed.
An address translation like this is carried out each time
an array element is accessed.
score[3] →
[0]
[1]
[2]
[3]
[99]
.
.
.
.
.
.
score → 0x1396
0x13ae
What will be the
time complexity
The value of array_name is actually the base address of array_name
array_name + index is the address of array_name[index].
An array reference array_name[index]
is equivalent to
For example, the following statements of pseudocode are equivalent:
print score[3]
print *(score + 3)
Note: No bounds checking of indices is done!
* is the dereferencing operator
*ref returns the contents of the memory location with address ref
*(array_name + index)
What will happen incase
of going overboard
Problems with Arrays
1. The capacity of Array can NOT change during program execution.
What is the problem?
Memory wastage
Out of range errors
2. Arrays are NOT self contained objects
What is the problem?
No way to find the last value stored.
Not a self contained object as per OOP principles.
C++ Style Multidimensional Arrays
Most high level languages support arrays with more than one dimension.
2D arrays are useful when data has to be arranged in tabular form.
Higher dimensional arrays appropriate when several characteristics associated
with data.
Test 1 Test 2 Test 3 Test 4
Student 1 99.0 93.5 89.0 91.0
Student 2 66.0 68.0 84.5 82.0
Student 3 88.5 78.5 70.0 65.0
: : : : :
: : : : :
Student-n 100.0 99.5 100.0 99.0
For storage and processing, use a two-dimensional array.
Example: A table of test scores for several different students on
several different tests.
Declaring Two-Dimensional Arrays
Standard form of declaration:
element_type array_name[NUM_ROWS][NUM_COLUMNS];
Example:
const int NUM_ROWS = 30,
NUM_COLUMNS = 4;
double scoresTable[NUM_ROWS][NUM_COLUMNS];
Initialization
♦ List the initial values in braces, row by row;
♦ May use internal braces for each row to improve readability.
Example:
double rates[][] = {{0.50, 0.55, 0.53}, // first row
{0.63, 0.58, 0.55}}; // second row
[0]
[1]
[2]
[3]
[29]
[0] [1] [2] [3]
Processing Two-Dimensional Arrays
♦ Remember: Rows (and) columns are numbered from zero!!
♦ Use doubly-indexed variables:
scoresTable[2][3] is the entry in row 2 and column 3
↑ ↑
row index column index
♦ Use nested loops to vary the two indices, most often in a rowwise manner.
Counting
from 0
Higher-Dimensional Arrays
The methods for 2D arrays extend in the obvious way to 3D arrays.
Example: To store and process a table of test scores for several different
students on several different tests for several different semesters
const int SEMS = 10, STUDENTS = 30, TESTS = 4;
typedef double ThreeDimArray[SEMS][STUDENTS][TESTS];
ThreeDimArray gradeBook;
gradeBook[4][2][3] is the score of 4th
semester for student 2 on test 3
// number of semesters, students and tests all counted from zero!!
Arrays of Arrays
double scoresTable[30][4];
Declares scoresTable to be a one-dimensional array containing
30 elements, each of which is a one-dimensional array of 4 real numbers; that is,
scoresTable is a one-dimensional array of rows , each of which has 4
real values. We could declare it as
typedef double RowOfTable[4];
RowOfTable scoresTable[30];
[0]
[1]
[2]
[3]
[29]
[0] [[1] [2] [3]
[0] [[1] [2] [3]
[0]
[1]
[2]
[3]
[29]
scoresTable[i] is the i-th row of the table
Address Translation:Address Translation:
The array-of-arrays structure of multidimensional arrays explains
address translation.
Suppose the base address of scoresTable is 0x12348:
scoresTable[10] 0x12348 + 10*(sizeof RowOfTable)
In general, an n-dimensional array can be viewed (recursively) as a
one-dimensional array whose elements are (n - 1)-dimensional arrays.
In any case:
scoresTable[i][j] should be thought of as (scoresTable[i])[j]
that is, as finding the j-th element of scoresTable[i].
→
scoresTable[10]
[3] → base(scoresTable[10]) + 3*(sizeof double)
scoresTable[10]
[4]
[3]
[0]
[1]
[9]
[10]
= 0x12348 + 10 * (4 * 8) + 3 * 8
= 0x124a0
= 0x12348 + 10 * (4 * 8)
Implementing Multidimensional Arrays
More complicated than one dimensional arrays.
Memory is organized as a sequence of memory locations, and is thus 1D
How to use a 1D structure to store a MD structure?
A B C D
E F G H
I J K L
A character requires a single byte
Compiler instructed to reserve 12 consecutive bytes
Two ways to store consecutively i.e. rowwise and columnwise.
Implementing Multidimensional Arrays
A B C D
E F G H
I J K L
RowWise
A
B
C
D
E
F
G
H
I
J
K
L
ColumnWise
A
E
I
B
F
J
C
G
K
D
H
L
A B C D
E F G H
I J K L
A B C D
E F G H
I J K L

More Related Content

What's hot

C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
طارق بالحارث
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
arrays in c
arrays in carrays in c
arrays in c
vidhi mehta
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Arrays
ArraysArrays
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Array
ArrayArray
Array
PRN USM
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Array and string
Array and stringArray and string
Array and string
prashant chelani
 

What's hot (20)

C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
arrays in c
arrays in carrays in c
arrays in c
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
2D Array
2D Array 2D Array
2D Array
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays
ArraysArrays
Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array
ArrayArray
Array
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Array and string
Array and stringArray and string
Array and string
 
Arrays
ArraysArrays
Arrays
 

Viewers also liked

C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
Subhasis Nayak
 
Array
ArrayArray
ArrayHajar
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
Mohammad Shaker
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2IIUM
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
Terry Yoast
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentationNeveen Reda
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Arrays
ArraysArrays
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
Array in C
Array in CArray in C
Array in C
Kamal Acharya
 

Viewers also liked (13)

C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Array
ArrayArray
Array
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Arrays
ArraysArrays
Arrays
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Array in C
Array in CArray in C
Array in C
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to Algo>Arrays

Arrays
ArraysArrays
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Arrays
ArraysArrays
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
Array
ArrayArray
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
Arrays
ArraysArrays
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Md. Imran Hossain Showrov
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
Uma mohan
 

Similar to Algo>Arrays (20)

Arrays
ArraysArrays
Arrays
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays
ArraysArrays
Arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
2 arrays
2   arrays2   arrays
2 arrays
 
Ch08
Ch08Ch08
Ch08
 
Array
ArrayArray
Array
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Arrays
ArraysArrays
Arrays
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Arrays
ArraysArrays
Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 

More from Ain-ul-Moiz Khawaja

Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
Ain-ul-Moiz Khawaja
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
Ain-ul-Moiz Khawaja
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 

More from Ain-ul-Moiz Khawaja (17)

Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
Turn over
Turn overTurn over
Turn over
 
Attribution Theories
Attribution TheoriesAttribution Theories
Attribution Theories
 
Attribution Theory
Attribution TheoryAttribution Theory
Attribution Theory
 
Absenteeism
AbsenteeismAbsenteeism
Absenteeism
 
HRM Employee Turnover
HRM Employee TurnoverHRM Employee Turnover
HRM Employee Turnover
 

Recently uploaded

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 

Recently uploaded (20)

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 

Algo>Arrays

  • 2. C++ Style Data Structures: Arrays(1) • An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation is direct access to each element in the array so values can be retrieved from or stored in this element.
  • 3. C++ Style Data Structures: Arrays (2) Properties: Ordered so there is a first element, a second one, etc. Fixed number of elements — fixed capacity Elements must be the same type (and size); ∴ use arrays only for homogeneous data sets. Direct access: Access an element by giving its location The time to access each element is the same for all elements, regardless of position. in contrast to sequential access (where to access an element, one must first access all those that precede it.)
  • 4. Declaring arrays in C++ where element_type is any type array_name is the name of the array — any valid identifier CAPACITY (a positive integer constant) is the number of elements in the array score[0] score[1] score[2] score[3] score[99] . . . . . . element_type array_name[CAPACITY]; e.g., double score[100]; The elements (or positions) of the array are indexed 0, 1, 2, . . ., CAPACITY - 1. Can't input the capacity, Why? The compiler reserves a block of “consecutive” memory locations, enough to hold CAPACITY values of type element_type.
  • 5. indices numbered 0, 1, 2, . . ., CAPACITY - 1 How well does C/C++ implement an array ADT? As an ADT In C++ ordered fixed size same type elements direct access element_type is the type of elements CAPACITY specifies the capacity of the array subscript operator []
  • 6. an array literal Array Initialization Example: double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21}; Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0. double rate[5] = {0.11, 0.13, 0.16}; Note 2: It is an error if more values are supplied than the declared size of the array. How this error is handled, however, will vary from one compiler to another. rate 0 1 2 3 4 0.11 0.13 0.16 0 0 rate 0 1 2 3 4 0.11 0.13 0.16 0.18 0.21 In C++, arrays can be initialized when they are declared. Numeric arrays: element_type num_array[CAPACITY] = {list_of_initial_values};
  • 7. Note 1: If fewer values are supplied than the declared size of the array, the zeroes used to fill un-initialized elements are interpreted as the null character '0' whose ASCII code is 0. const int NAME_LENGTH = 10; char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'}; vowel 0 1 2 3 4 A E I O U char vowel[5] = {'A', 'E', 'I', 'O', 'U'}; Character Arrays: Character arrays may be initialized in the same manner as numeric arrays. declares vowel to be an array of 5 characters and initializes it as follows: collegeName 0 1 2 3 4 5 6 7 8 9 C a l v i n 0 0 0 0
  • 8. Addresses When an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the base address of the array. Each array reference must be translated into an offset from this base address. For example, if each element of array score will be stored in 8 bytes and the base address of score is 0x1396. A statement such ascout << score[3] << endl; requires that array reference score[3] be translated into a memory address: 0x1396 + 3 * sizeof (double) = 0x1396 + 3 * 8 = 0x13ae The contents of the memory word with this address 0x13ae can then be retrieved and displayed. An address translation like this is carried out each time an array element is accessed. score[3] → [0] [1] [2] [3] [99] . . . . . . score → 0x1396 0x13ae What will be the time complexity
  • 9. The value of array_name is actually the base address of array_name array_name + index is the address of array_name[index]. An array reference array_name[index] is equivalent to For example, the following statements of pseudocode are equivalent: print score[3] print *(score + 3) Note: No bounds checking of indices is done! * is the dereferencing operator *ref returns the contents of the memory location with address ref *(array_name + index) What will happen incase of going overboard
  • 10. Problems with Arrays 1. The capacity of Array can NOT change during program execution. What is the problem? Memory wastage Out of range errors 2. Arrays are NOT self contained objects What is the problem? No way to find the last value stored. Not a self contained object as per OOP principles.
  • 11. C++ Style Multidimensional Arrays Most high level languages support arrays with more than one dimension. 2D arrays are useful when data has to be arranged in tabular form. Higher dimensional arrays appropriate when several characteristics associated with data. Test 1 Test 2 Test 3 Test 4 Student 1 99.0 93.5 89.0 91.0 Student 2 66.0 68.0 84.5 82.0 Student 3 88.5 78.5 70.0 65.0 : : : : : : : : : : Student-n 100.0 99.5 100.0 99.0 For storage and processing, use a two-dimensional array. Example: A table of test scores for several different students on several different tests.
  • 12. Declaring Two-Dimensional Arrays Standard form of declaration: element_type array_name[NUM_ROWS][NUM_COLUMNS]; Example: const int NUM_ROWS = 30, NUM_COLUMNS = 4; double scoresTable[NUM_ROWS][NUM_COLUMNS]; Initialization ♦ List the initial values in braces, row by row; ♦ May use internal braces for each row to improve readability. Example: double rates[][] = {{0.50, 0.55, 0.53}, // first row {0.63, 0.58, 0.55}}; // second row [0] [1] [2] [3] [29] [0] [1] [2] [3]
  • 13. Processing Two-Dimensional Arrays ♦ Remember: Rows (and) columns are numbered from zero!! ♦ Use doubly-indexed variables: scoresTable[2][3] is the entry in row 2 and column 3 ↑ ↑ row index column index ♦ Use nested loops to vary the two indices, most often in a rowwise manner. Counting from 0
  • 14. Higher-Dimensional Arrays The methods for 2D arrays extend in the obvious way to 3D arrays. Example: To store and process a table of test scores for several different students on several different tests for several different semesters const int SEMS = 10, STUDENTS = 30, TESTS = 4; typedef double ThreeDimArray[SEMS][STUDENTS][TESTS]; ThreeDimArray gradeBook; gradeBook[4][2][3] is the score of 4th semester for student 2 on test 3 // number of semesters, students and tests all counted from zero!!
  • 15. Arrays of Arrays double scoresTable[30][4]; Declares scoresTable to be a one-dimensional array containing 30 elements, each of which is a one-dimensional array of 4 real numbers; that is, scoresTable is a one-dimensional array of rows , each of which has 4 real values. We could declare it as typedef double RowOfTable[4]; RowOfTable scoresTable[30]; [0] [1] [2] [3] [29] [0] [[1] [2] [3] [0] [[1] [2] [3] [0] [1] [2] [3] [29]
  • 16. scoresTable[i] is the i-th row of the table Address Translation:Address Translation: The array-of-arrays structure of multidimensional arrays explains address translation. Suppose the base address of scoresTable is 0x12348: scoresTable[10] 0x12348 + 10*(sizeof RowOfTable) In general, an n-dimensional array can be viewed (recursively) as a one-dimensional array whose elements are (n - 1)-dimensional arrays. In any case: scoresTable[i][j] should be thought of as (scoresTable[i])[j] that is, as finding the j-th element of scoresTable[i]. → scoresTable[10] [3] → base(scoresTable[10]) + 3*(sizeof double) scoresTable[10] [4] [3] [0] [1] [9] [10] = 0x12348 + 10 * (4 * 8) + 3 * 8 = 0x124a0 = 0x12348 + 10 * (4 * 8)
  • 17. Implementing Multidimensional Arrays More complicated than one dimensional arrays. Memory is organized as a sequence of memory locations, and is thus 1D How to use a 1D structure to store a MD structure? A B C D E F G H I J K L A character requires a single byte Compiler instructed to reserve 12 consecutive bytes Two ways to store consecutively i.e. rowwise and columnwise.
  • 18. Implementing Multidimensional Arrays A B C D E F G H I J K L RowWise A B C D E F G H I J K L ColumnWise A E I B F J C G K D H L A B C D E F G H I J K L A B C D E F G H I J K L